Skip to content

Commit

Permalink
Tweak MulticastLoggingHandler to only send messages to connected hand…
Browse files Browse the repository at this point in the history
…lers and forcefully reset Console.Out when unity changes it
  • Loading branch information
Banane9 committed Aug 26, 2024
1 parent 1c27d4c commit d34dfbb
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
9 changes: 7 additions & 2 deletions MonkeyLoader/Logging/ConsoleLoggingHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ public sealed class ConsoleLoggingHandler : LoggingHandler
{
private static bool _hasConsole = false;

private static StreamWriter? _writer;

/// <summary>
/// Gets the instance of the <see cref="ConsoleLoggingHandler"/>.
/// </summary>
Expand Down Expand Up @@ -44,9 +46,9 @@ private ConsoleLoggingHandler()
_hasConsole = true;

var output = Console.OpenStandardOutput();
var writer = new StreamWriter(output) { AutoFlush = true };
_writer = new StreamWriter(output) { AutoFlush = true };

Console.SetOut(writer);
Console.SetOut(_writer);
}
catch
{
Expand Down Expand Up @@ -93,6 +95,9 @@ public override void Info(Func<object> messageProducer)
/// <param name="message">The message to write.</param>
public void Log(string message)
{
if (Console.Out != _writer)
Console.SetOut(_writer);

Console.WriteLine($"[{DateTime.UtcNow:HH:mm:ss:ffff}] {message}");
Console.ForegroundColor = ConsoleColor.Gray;
}
Expand Down
16 changes: 8 additions & 8 deletions MonkeyLoader/Logging/MulticastLoggingHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public sealed class MulticastLoggingHandler : LoggingHandler, IEnumerable<Loggin
private readonly LoggingHandler[] _loggingHandlers;

/// <inheritdoc/>
public override bool Connected => _loggingHandlers.Any(IsConnected);
public override bool Connected => ConnectedHandlers.Any();

/// <summary>
/// Gets the currently <see cref="LoggingHandler.Connected">connected</see> logging handlers that this one delegates messages to.
Expand Down Expand Up @@ -55,7 +55,7 @@ public override void Debug(Func<object> messageProducer)
{
messageProducer = PreloadMessage(messageProducer);

foreach (var loggingHandler in _loggingHandlers)
foreach (var loggingHandler in ConnectedHandlers)
loggingHandler.Debug(messageProducer);
}

Expand All @@ -64,7 +64,7 @@ public override void Error(Func<object> messageProducer)
{
messageProducer = PreloadMessage(messageProducer);

foreach (var loggingHandler in _loggingHandlers)
foreach (var loggingHandler in ConnectedHandlers)
loggingHandler.Error(messageProducer);
}

Expand All @@ -73,14 +73,14 @@ public override void Fatal(Func<object> messageProducer)
{
messageProducer = PreloadMessage(messageProducer);

foreach (var loggingHandler in _loggingHandlers)
foreach (var loggingHandler in ConnectedHandlers)
loggingHandler.Fatal(messageProducer);
}

/// <inheritdoc/>
public override void Flush()
{
foreach (var loggingHandler in _loggingHandlers)
foreach (var loggingHandler in ConnectedHandlers)
loggingHandler.Flush();
}

Expand All @@ -97,7 +97,7 @@ public override void Info(Func<object> messageProducer)
{
messageProducer = PreloadMessage(messageProducer);

foreach (var loggingHandler in _loggingHandlers)
foreach (var loggingHandler in ConnectedHandlers)
loggingHandler.Info(messageProducer);
}

Expand All @@ -106,7 +106,7 @@ public override void Trace(Func<object> messageProducer)
{
messageProducer = PreloadMessage(messageProducer);

foreach (var loggingHandler in _loggingHandlers)
foreach (var loggingHandler in ConnectedHandlers)
loggingHandler.Trace(messageProducer);
}

Expand All @@ -115,7 +115,7 @@ public override void Warn(Func<object> messageProducer)
{
messageProducer = PreloadMessage(messageProducer);

foreach (var loggingHandler in _loggingHandlers)
foreach (var loggingHandler in ConnectedHandlers)
loggingHandler.Warn(messageProducer);
}

Expand Down

0 comments on commit d34dfbb

Please sign in to comment.