diff --git a/ProjectLighthouse/Logging/Logger.cs b/ProjectLighthouse/Logging/Logger.cs index 9c9f3af1..5af2deb0 100644 --- a/ProjectLighthouse/Logging/Logger.cs +++ b/ProjectLighthouse/Logging/Logger.cs @@ -1,4 +1,5 @@ #nullable enable +using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Diagnostics; @@ -36,20 +37,6 @@ public static class Logger const int depth = 5; const int skipDepth = depth - 2; -// // Get the stacktrace for logging... -// string trace = Environment.StackTrace -// .Split('\n', depth + extraTraceLines, StringSplitOptions.RemoveEmptyEntries) -// .Skip(skipDepth + extraTraceLines) -// .First(); -// -// trace = trace.TrimEnd('\r'); -// trace = trace.Substring(trace.LastIndexOf(Path.DirectorySeparatorChar) + 1); // Try splitting by the filename. -// if (trace.StartsWith(" at ")) // If we still havent split properly... -// { -// trace = trace.Substring(trace.LastIndexOf('.') + 1); // Try splitting by the last dot. -// } -// trace = trace.Replace(".cs:line ", ":"); - StackTrace stackTrace = new(true); StackFrame? frame = stackTrace.GetFrame(skipDepth + extraTraceLines); Debug.Assert(frame != null); @@ -118,6 +105,33 @@ public static class Logger } } ); + + // Flush the log queue when we're exiting. + AppDomain.CurrentDomain.UnhandledException += Flush; + AppDomain.CurrentDomain.ProcessExit += Flush; + } + + /// + /// Logs everything in the queue to all loggers immediately. + /// This is a helper function to allow for this function to be easily added to events. + /// + public static void Flush(object? _, EventArgs __) + { + Flush(); + } + + /// + /// Logs everything in the queue to all loggers immediately. + /// + public static void Flush() + { + while (logQueue.TryDequeue(out LogLine line)) + { + foreach (ILogger logger in loggers) + { + logger.Log(line); + } + } } ///