Skip to content

Commit

Permalink
Logging async / locking / flushing tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
Banane9 committed Oct 2, 2024
1 parent a9115cb commit 4844d5b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
5 changes: 3 additions & 2 deletions MonkeyLoader/Logging/ConsoleLoggingHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ public static async Task<bool> ConnectAsync(CancellationToken cancellationToken)
_consoleHostProcess = process;
_pipeClient = pipeClient;
_writer = new(_pipeClient);
_writer.AutoFlush = true;

return true;
}
Expand Down Expand Up @@ -202,8 +203,8 @@ public void Log(string message, string textHighlight = NORMAL)
if (!Connected)
return;

_writer.WriteLine($"{NORMAL + GRAY}[{DateTime.UtcNow:HH:mm:ss:ffff}]{textHighlight} {message}{NORMAL + GRAY}");
_writer.Flush();
lock (_writer)
_writer.WriteLine($"{NORMAL + GRAY}[{DateTime.UtcNow:HH:mm:ss:ffff}]{textHighlight} {message}{NORMAL + GRAY}");
}

/// <inheritdoc/>
Expand Down
29 changes: 19 additions & 10 deletions MonkeyLoader/Logging/LoggingController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,35 +122,44 @@ internal void LogInternal(LoggingLevel level, string identifier, Func<object> me
if (!ShouldLog(level))
return;

LogLevelToLogger(level)(MakeMessageProducer(level, identifier, messageProducer));
Task.Run(() =>
{
LogLevelToLogger(level)(MakeMessageProducer(level, identifier, messageProducer));

HandleAutoFlush(level);
HandleAutoFlush(level);
});
}

internal void LogInternal(LoggingLevel level, string identifier, IEnumerable<Func<object>> messageProducers)
{
if (!ShouldLog(level))
return;

var logger = LogLevelToLogger(level);
Task.Run(() =>
{
var logger = LogLevelToLogger(level);

foreach (var messageProducer in messageProducers)
logger(MakeMessageProducer(level, identifier, messageProducer));
foreach (var messageProducer in messageProducers)
logger(MakeMessageProducer(level, identifier, messageProducer));

HandleAutoFlush(level);
HandleAutoFlush(level);
});
}

internal void LogInternal(LoggingLevel level, string identifier, IEnumerable<object> messages)
{
if (!ShouldLog(level))
return;

var logger = LogLevelToLogger(level);
Task.Run(() =>
{
var logger = LogLevelToLogger(level);

foreach (var message in messages)
logger(MakeMessageProducer(level, identifier, message));
foreach (var message in messages)
logger(MakeMessageProducer(level, identifier, message));

HandleAutoFlush(level);
HandleAutoFlush(level);
});
}

private static string LogLevelToString(LoggingLevel level) => level switch
Expand Down

0 comments on commit 4844d5b

Please sign in to comment.