Flush the error log before exit

Currently, some logs can be missing when a fatal error occurs (especially GuestBrokeExecutionException).

This MR attempts to flush logs to console and file before process exit.
This commit is contained in:
Coxxs 2025-10-13 17:40:15 -05:00 committed by KeatonTheBot
parent 66a5719e10
commit f5822c16af
5 changed files with 59 additions and 1 deletions

View file

@ -187,6 +187,17 @@ namespace Ryujinx.Common.Logging
}
}
public static void Flush()
{
foreach (ILogTarget target in _logTargets)
{
if (target is AsyncLogTargetWrapper asyncTarget)
{
asyncTarget.Flush();
}
}
}
public static void Shutdown()
{
Updated = null;

View file

@ -27,6 +27,17 @@ namespace Ryujinx.Common.Logging.Targets
private readonly int _overflowTimeout;
private sealed class FlushEventArgs : LogEventArgs
{
public readonly ManualResetEventSlim SignalEvent;
public FlushEventArgs(ManualResetEventSlim signalEvent)
: base(LogLevel.Notice, TimeSpan.Zero, string.Empty, string.Empty)
{
SignalEvent = signalEvent;
}
}
string ILogTarget.Name => _target.Name;
public AsyncLogTargetWrapper(ILogTarget target, int queueLimit = -1, AsyncLogTargetOverflowAction overflowAction = AsyncLogTargetOverflowAction.Block)
@ -41,7 +52,15 @@ namespace Ryujinx.Common.Logging.Targets
{
try
{
_target.Log(this, _messageQueue.Take());
LogEventArgs item = _messageQueue.Take();
if (item is FlushEventArgs flush)
{
flush.SignalEvent.Set();
continue;
}
_target.Log(this, item);
}
catch (InvalidOperationException)
{
@ -68,6 +87,26 @@ namespace Ryujinx.Common.Logging.Targets
}
}
public void Flush()
{
if (_messageQueue.Count == 0 || _messageQueue.IsAddingCompleted)
{
return;
}
using var signal = new ManualResetEventSlim(false);
try
{
_messageQueue.Add(new FlushEventArgs(signal));
}
catch (InvalidOperationException)
{
return;
}
signal.Wait();
}
public void Dispose()
{
GC.SuppressFinalize(this);

View file

@ -1084,6 +1084,8 @@ namespace Ryujinx.HLE.HOS.Kernel.Process
Logger.Error?.Print(LogClass.Cpu, $"Invalid memory access at virtual address 0x{va:X16}.");
Logger.Flush();
return false;
}
@ -1092,6 +1094,8 @@ namespace Ryujinx.HLE.HOS.Kernel.Process
KernelStatic.GetCurrentThread().PrintGuestStackTrace();
KernelStatic.GetCurrentThread()?.PrintGuestRegisterPrintout();
Logger.Flush();
throw new UndefinedInstructionException(address, opCode);
}

View file

@ -1885,6 +1885,9 @@ namespace Ryujinx.HLE.HOS.Kernel.SupervisorCall
return;
}
Logger.Error?.Print(LogClass.KernelSvc, "The guest program broke execution!");
Logger.Flush();
// TODO: Debug events.
currentThread.Owner.TerminateCurrentProcess();

View file

@ -272,6 +272,7 @@ namespace Ryujinx.Ava
if (isTerminating)
{
Logger.Flush();
Exit();
}
}