From c38fed67e7eef2d17ce9f06bca9ca0c7abf44ca2 Mon Sep 17 00:00:00 2001 From: Anairkoen Schno Date: Sun, 29 Mar 2020 19:14:12 -0500 Subject: [PATCH] Added synchronous logging to the logger --- IPA.Loader/Config/SelfConfig.cs | 3 +++ IPA.Loader/Logging/StandardLogger.cs | 28 ++++++++++++++++++++++++++-- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/IPA.Loader/Config/SelfConfig.cs b/IPA.Loader/Config/SelfConfig.cs index 86207c50..d80b6296 100644 --- a/IPA.Loader/Config/SelfConfig.cs +++ b/IPA.Loader/Config/SelfConfig.cs @@ -129,6 +129,9 @@ namespace IPA.Config // LINE: ignore 2 public static bool ShowTrace_ => (Instance?.Debug?.ShowTrace ?? false) || CommandLineValues.Debug.ShowTrace; + public virtual bool SyncLogging { get; set; } = false; + // LINE: ignore + public static bool SyncLogging_ => Instance?.Debug?.SyncLogging ?? false; } // LINE: ignore diff --git a/IPA.Loader/Logging/StandardLogger.cs b/IPA.Loader/Logging/StandardLogger.cs index c0279588..bdf89587 100644 --- a/IPA.Loader/Logging/StandardLogger.cs +++ b/IPA.Loader/Logging/StandardLogger.cs @@ -106,6 +106,7 @@ namespace IPA.Logging /// the global filter level public static LogLevel PrintFilter { get; internal set; } = LogLevel.All; private static bool showTrace = false; + private static volatile bool syncLogging = false; private readonly List printers = new List(); private readonly StandardLogger parent; @@ -120,6 +121,7 @@ namespace IPA.Logging showSourceClass = SelfConfig.Debug_.ShowCallSource_; PrintFilter = SelfConfig.Debug_.ShowDebug_ ? LogLevel.All : LogLevel.InfoUp; showTrace = SelfConfig.Debug_.ShowTrace_; + syncLogging = SelfConfig.Debug_.SyncLogging_; } private StandardLogger(StandardLogger parent, string subName) @@ -200,13 +202,23 @@ namespace IPA.Logging logWaitEvent.Wait(); try { + var sync = syncLogging && !IsOnLoggerThread; + if (sync) + { + threadSync ??= new ManualResetEventSlim(); + threadSync.Reset(); + } + logQueue.Add(new LogMessage { Level = level, Message = message, Logger = this, - Time = Utils.CurrentTime() + Time = Utils.CurrentTime(), + Sync = threadSync }); + + if (sync) threadSync.Wait(); } catch (InvalidOperationException) { @@ -214,6 +226,9 @@ namespace IPA.Logging } } + [ThreadStatic] + private static ManualResetEventSlim threadSync; + /// /// /// An override to which shows the method that called it. @@ -248,9 +263,14 @@ namespace IPA.Logging public StandardLogger Logger; public string Message; public DateTime Time; + public ManualResetEventSlim Sync; } - private static ManualResetEventSlim logWaitEvent = new ManualResetEventSlim(true); + [ThreadStatic] + private static bool? isOnLoggerThread = null; + public static bool IsOnLoggerThread => isOnLoggerThread ??= Thread.CurrentThread.ManagedThreadId == logThread.ManagedThreadId; + + private static readonly ManualResetEventSlim logWaitEvent = new ManualResetEventSlim(true); private static readonly BlockingCollection logQueue = new BlockingCollection(); private static Thread logThread; @@ -314,6 +334,8 @@ namespace IPA.Logging } } + msg.Sync?.Set(); + var debugConfig = SelfConfig.Instance?.Debug; if (debugConfig != null && debugConfig.HideMessagesForPerformance @@ -337,6 +359,8 @@ namespace IPA.Logging foreach (var print in messageLogger.printers) prints.Add(print); } while (messageLogger != null); + + message.Sync?.Set(); } // print using logging subsystem to all logger printers