Skip to content

Commit

Permalink
Actually wait until FlushToDisk is done.
Browse files Browse the repository at this point in the history
  • Loading branch information
CptMoore committed Dec 13, 2024
1 parent 7a6440c commit a6c4d78
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
25 changes: 20 additions & 5 deletions ModTek/Features/Logging/LoggingFeature.cs
Original file line number Diff line number Diff line change
Expand Up @@ -163,19 +163,27 @@ internal static void LogAtLevel(string loggerName, LogLevel logLevel, object mes

internal static void Flush()
{
var flushEvent = new ManualResetEventSlim(false);

// this cannot guarantee everything is flushed during a shutdown, chances are good though
if (!IsDispatchAvailable(out _))
{
var messageDto = new MTLoggerMessageDto();
messageDto.FlushToDisk = true;
messageDto.FlushToDiskPostEvent = flushEvent;
ProcessLoggerMessage(ref messageDto);
return;
}

DispatchStopWatch.Start();
ref var updateDto = ref _queue.AcquireUncommitedOrWait();
updateDto.FlushToDiskPostEvent = flushEvent;
DispatchStopWatch.Stop();

updateDto.Commit();

// always wait
// usually caller wants to flush to guarantee debug information on disk
flushEvent.Wait();
}

private static bool IsDispatchAvailable(out int currentThreadId)
Expand Down Expand Up @@ -207,12 +215,19 @@ private static DiagnosticsStackTrace GrabStackTrace()

private static void ProcessLoggerMessage(ref MTLoggerMessageDto messageDto)
{
_consoleLog?.Append(ref messageDto);
try
{
_consoleLog?.Append(ref messageDto);

_mainLog.Append(ref messageDto);
foreach (var logAppender in _logsAppenders)
_mainLog.Append(ref messageDto);
foreach (var logAppender in _logsAppenders)
{
logAppender.Append(ref messageDto);
}
}
finally
{
logAppender.Append(ref messageDto);
messageDto.FlushToDiskPostEvent?.Set();
}
}

Expand Down
4 changes: 3 additions & 1 deletion ModTek/Features/Logging/MTLoggerMessageDto.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Diagnostics;
using System.Threading;
using HBS.Logging;
using UnityEngine;

Expand Down Expand Up @@ -29,7 +30,8 @@ internal static void GetTimings(out long stopwatchTimestamp, out DateTime dateTi
internal IStackTrace Location;
internal int ThreadId;
// or this is set
internal bool FlushToDisk;
internal bool FlushToDisk => FlushToDiskPostEvent != null;
internal ManualResetEventSlim FlushToDiskPostEvent;

public MTLoggerMessageDto()
{
Expand Down

0 comments on commit a6c4d78

Please sign in to comment.