Browse Source

Added synchronous logging to the logger

pull/46/head
Anairkoen Schno 4 years ago
parent
commit
c38fed67e7
2 changed files with 29 additions and 2 deletions
  1. +3
    -0
      IPA.Loader/Config/SelfConfig.cs
  2. +26
    -2
      IPA.Loader/Logging/StandardLogger.cs

+ 3
- 0
IPA.Loader/Config/SelfConfig.cs View File

@ -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


+ 26
- 2
IPA.Loader/Logging/StandardLogger.cs View File

@ -106,6 +106,7 @@ namespace IPA.Logging
/// <value>the global filter level</value>
public static LogLevel PrintFilter { get; internal set; } = LogLevel.All;
private static bool showTrace = false;
private static volatile bool syncLogging = false;
private readonly List<LogPrinter> printers = new List<LogPrinter>();
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;
/// <inheritdoc />
/// <summary>
/// An override to <see cref="M:IPA.Logging.Logger.Debug(System.String)" /> 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<LogMessage> logQueue = new BlockingCollection<LogMessage>();
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


Loading…
Cancel
Save