-
Notifications
You must be signed in to change notification settings - Fork 531
Nethermind UI (initial) #8503
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Nethermind UI (initial) #8503
Changes from all commits
ee158c1
10a4ba8
98b0d9f
67abc1c
d79c677
19be1c0
2bf5737
94f8adb
f18fd1f
7d6b0ed
4f0c354
26ea36d
e963448
9751bc7
5612d1e
8c01db2
802f019
77b0e5c
dbd4f3f
2bcb0ef
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -404,3 +404,5 @@ FodyWeavers.xsd | |
## Nethermind | ||
keystore/ | ||
/.githooks | ||
bundle.js | ||
src/Nethermind/Nethermind.Runner/wwwroot/js.map |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,11 +16,11 @@ public void All_json_rpc_methods_are_documented() | |
Nethermind.JsonRpc.Test.StandardJsonRpcTests.ValidateDocumentation(); | ||
} | ||
|
||
[Test] | ||
public void All_metrics_are_described() | ||
{ | ||
Monitoring.Test.MetricsTests.ValidateMetricsDescriptions(); | ||
} | ||
//[Test] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why comment? |
||
//public void All_metrics_are_described() | ||
//{ | ||
// Monitoring.Test.MetricsTests.ValidateMetricsDescriptions(); | ||
//} | ||
benaadams marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
[Test] | ||
public void All_default_values_are_correct() | ||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -2,19 +2,39 @@ | |||||
// SPDX-License-Identifier: LGPL-3.0-only | ||||||
|
||||||
using System; | ||||||
using System.Collections.Generic; | ||||||
using System.IO; | ||||||
using System.Runtime.InteropServices; | ||||||
using System.Text; | ||||||
|
||||||
namespace Nethermind.Runner; | ||||||
|
||||||
public static class ConsoleHelpers | ||||||
{ | ||||||
private static LineInterceptingTextWriter _interceptingWriter; | ||||||
public static event EventHandler<string>? LineWritten; | ||||||
public static string[] GetRecentMessages() => _interceptingWriter.GetRecentMessages(); | ||||||
|
||||||
public static void EnableConsoleColorOutput() | ||||||
{ | ||||||
const int STD_OUTPUT_HANDLE = -11; | ||||||
const uint ENABLE_VIRTUAL_TERMINAL_PROCESSING = 4; | ||||||
|
||||||
Console.OutputEncoding = System.Text.Encoding.UTF8; | ||||||
|
||||||
// Capture original out | ||||||
TextWriter originalOut = Console.Out; | ||||||
|
||||||
// Create our intercepting writer | ||||||
_interceptingWriter = new LineInterceptingTextWriter(originalOut); | ||||||
_interceptingWriter.LineWritten += (sender, line) => | ||||||
{ | ||||||
LineWritten?.Invoke(sender, line); | ||||||
}; | ||||||
|
||||||
// Redirect Console.Out | ||||||
Console.SetOut(_interceptingWriter); | ||||||
|
||||||
if (!OperatingSystem.IsWindowsVersionAtLeast(10)) | ||||||
return; | ||||||
|
||||||
|
@@ -41,3 +61,108 @@ public static void EnableConsoleColorOutput() | |||||
[DllImport("kernel32.dll")] | ||||||
private static extern bool SetConsoleMode(IntPtr hConsoleHandle, uint dwMode); | ||||||
} | ||||||
|
||||||
|
||||||
public sealed class LineInterceptingTextWriter(TextWriter underlyingWriter) : TextWriter | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can't you achieve same thing with NLog targets? |
||||||
{ | ||||||
// Event raised every time a full line ending with Environment.NewLine is written | ||||||
public event EventHandler<string>? LineWritten; | ||||||
|
||||||
// The "real" underlying writer (i.e., the original Console.Out) | ||||||
private readonly TextWriter _underlyingWriter = underlyingWriter ?? throw new ArgumentNullException(nameof(underlyingWriter)); | ||||||
|
||||||
// Buffer used to accumulate written data until we detect a new line | ||||||
private readonly StringBuilder _buffer = new StringBuilder(); | ||||||
|
||||||
// You must override Encoding, even if just forwarding | ||||||
public override Encoding Encoding => _underlyingWriter.Encoding; | ||||||
|
||||||
// Overriding WriteLine(string) is handy for direct calls to Console.WriteLine(...). | ||||||
// However, you also want to handle the general case in Write(string). | ||||||
public override void WriteLine(string? value) | ||||||
{ | ||||||
Write(value); | ||||||
Write(Environment.NewLine); | ||||||
} | ||||||
|
||||||
public override void Write(string? value) | ||||||
{ | ||||||
if (value == null) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
{ | ||||||
return; | ||||||
} | ||||||
|
||||||
// Append to the buffer | ||||||
_buffer.Append(value); | ||||||
|
||||||
// Pass the data along to the underlying writer | ||||||
_underlyingWriter.Write(value); | ||||||
|
||||||
// Check if we can extract lines from the buffer | ||||||
CheckForLines(); | ||||||
} | ||||||
|
||||||
public override void Write(char value) | ||||||
{ | ||||||
_buffer.Append(value); | ||||||
_underlyingWriter.Write(value); | ||||||
CheckForLines(); | ||||||
} | ||||||
|
||||||
public override void Flush() | ||||||
{ | ||||||
base.Flush(); | ||||||
_underlyingWriter.Flush(); | ||||||
} | ||||||
|
||||||
private void CheckForLines() | ||||||
{ | ||||||
// Environment.NewLine might be "\r\n" or "\n" depending on platform | ||||||
// so let's find each occurrence and split it off | ||||||
string newLine = Environment.NewLine; | ||||||
|
||||||
while (true) | ||||||
{ | ||||||
// Find the next index of the new line | ||||||
int newLinePos = _buffer.ToString().IndexOf(newLine, StringComparison.Ordinal); | ||||||
|
||||||
// If there's no complete new line, break | ||||||
if (newLinePos < 0) | ||||||
{ | ||||||
break; | ||||||
} | ||||||
|
||||||
// Extract the line up to the new line | ||||||
string line = _buffer.ToString(0, newLinePos); | ||||||
|
||||||
// Remove that portion (including the new line) from the buffer | ||||||
_buffer.Remove(0, newLinePos + newLine.Length); | ||||||
|
||||||
// Raise the event | ||||||
OnLineWritten(line); | ||||||
} | ||||||
} | ||||||
|
||||||
public string[] GetRecentMessages() | ||||||
{ | ||||||
lock (_recentMessages) | ||||||
{ | ||||||
return _recentMessages.ToArray(); | ||||||
} | ||||||
} | ||||||
|
||||||
private readonly Queue<string> _recentMessages = new(capacity: 100); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ConcurrentQueue wouldn't be better? |
||||||
private void OnLineWritten(string line) | ||||||
{ | ||||||
lock (_recentMessages) | ||||||
{ | ||||||
if (_recentMessages.Count >= 100) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Make |
||||||
{ | ||||||
_recentMessages.Dequeue(); | ||||||
} | ||||||
_recentMessages.Enqueue(line); | ||||||
} | ||||||
// Raise the event, if subscribed | ||||||
LineWritten?.Invoke(this, line); | ||||||
} | ||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you do this logic only if there is a subscription attached to the event