Browse Source

Enabled nullability for StandardLogger.cs

pull/94/head
Anairkoen Schno 3 years ago
parent
commit
7818ff8a19
Signed by: DaNike GPG Key ID: BEFB74D5F3FC4387
1 changed files with 25 additions and 24 deletions
  1. +25
    -24
      IPA.Loader/Logging/StandardLogger.cs

+ 25
- 24
IPA.Loader/Logging/StandardLogger.cs View File

@ -1,4 +1,6 @@
using IPA.Config;
#nullable enable
using IPA.Config;
using IPA.Logging.Printers; using IPA.Logging.Printers;
using IPA.Utilities; using IPA.Utilities;
using System; using System;
@ -23,7 +25,7 @@ namespace IPA.Logging
/// </remarks> /// </remarks>
public class StandardLogger : Logger public class StandardLogger : Logger
{ {
private static readonly List<LogPrinter> defaultPrinters = new List<LogPrinter>()
private static readonly List<LogPrinter> defaultPrinters = new()
{ {
new GlobalLogFilePrinter() new GlobalLogFilePrinter()
}; };
@ -108,10 +110,10 @@ namespace IPA.Logging
private static bool showTrace = false; private static bool showTrace = false;
private static volatile bool syncLogging = false; private static volatile bool syncLogging = false;
private readonly List<LogPrinter> printers = new List<LogPrinter>();
private readonly StandardLogger parent;
private readonly List<LogPrinter> printers = new();
private readonly StandardLogger? parent;
private readonly Dictionary<string, StandardLogger> children = new Dictionary<string, StandardLogger>();
private readonly Dictionary<string, StandardLogger> children = new();
/// <summary> /// <summary>
/// Configures internal debug settings based on the config passed in. /// Configures internal debug settings based on the config passed in.
@ -218,7 +220,7 @@ namespace IPA.Logging
Sync = threadSync Sync = threadSync
}); });
if (sync) threadSync.Wait();
if (sync) threadSync!.Wait();
} }
catch (InvalidOperationException) catch (InvalidOperationException)
{ {
@ -227,7 +229,7 @@ namespace IPA.Logging
} }
[ThreadStatic] [ThreadStatic]
private static ManualResetEventSlim threadSync;
private static ManualResetEventSlim? threadSync;
/// <inheritdoc /> /// <inheritdoc />
/// <summary> /// <summary>
@ -263,7 +265,7 @@ namespace IPA.Logging
public StandardLogger Logger; public StandardLogger Logger;
public string Message; public string Message;
public DateTime Time; public DateTime Time;
public ManualResetEventSlim Sync;
public ManualResetEventSlim? Sync;
} }
[ThreadStatic] [ThreadStatic]
@ -272,13 +274,13 @@ namespace IPA.Logging
/// Whether or not the calling thread is the logger thread. /// Whether or not the calling thread is the logger thread.
/// </summary> /// </summary>
/// <value><see langword="true"/> if the current thread is the logger thread, <see langword="false"/> otherwise</value> /// <value><see langword="true"/> if the current thread is the logger thread, <see langword="false"/> otherwise</value>
public static bool IsOnLoggerThread => isOnLoggerThread ??= Thread.CurrentThread.ManagedThreadId == logThread.ManagedThreadId;
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;
private static readonly ManualResetEventSlim logWaitEvent = new(true);
private static readonly BlockingCollection<LogMessage> logQueue = new();
private static Thread? logThread;
private static StandardLogger loggerLogger;
private static StandardLogger? loggerLogger;
private const int LogCloseTimeout = 250; private const int LogCloseTimeout = 250;
@ -323,7 +325,7 @@ namespace IPA.Logging
if (!started.Contains(printer)) if (!started.Contains(printer))
{ // start printer if not started { // start printer if not started
printer.StartPrint(); printer.StartPrint();
started.Add(printer);
_ = started.Add(printer);
} }
// update last use time and print // update last use time and print
@ -355,13 +357,13 @@ namespace IPA.Logging
{ // aggregate loggers in the process { // aggregate loggers in the process
var messageLogger = message.Logger; var messageLogger = message.Logger;
foreach (var print in messageLogger.printers) foreach (var print in messageLogger.printers)
prints.Add(print);
_ = prints.Add(print);
do do
{ {
messageLogger = messageLogger.parent; messageLogger = messageLogger.parent;
if (messageLogger != null) if (messageLogger != null)
foreach (var print in messageLogger.printers) foreach (var print in messageLogger.printers)
prints.Add(print);
_ = prints.Add(print);
} while (messageLogger != null); } while (messageLogger != null);
message.Sync?.Set(); message.Sync?.Set();
@ -397,7 +399,7 @@ namespace IPA.Logging
Console.WriteLine($"printer errored: {e}"); Console.WriteLine($"printer errored: {e}");
} }
started.Remove(printer);
_ = started.Remove(printer);
} }
} }
} }
@ -432,7 +434,7 @@ namespace IPA.Logging
internal static void StopLogThread() internal static void StopLogThread()
{ {
logQueue.CompleteAdding(); logQueue.CompleteAdding();
logThread.Join();
logThread!.Join();
} }
} }
@ -448,11 +450,10 @@ namespace IPA.Logging
/// <param name="name">the name of the child</param> /// <param name="name">the name of the child</param>
/// <returns>the child logger</returns> /// <returns>the child logger</returns>
public static Logger GetChildLogger(this Logger logger, string name) public static Logger GetChildLogger(this Logger logger, string name)
{
if (logger is StandardLogger standardLogger)
return standardLogger.GetChild(name);
throw new InvalidOperationException();
}
=> logger switch
{
StandardLogger l => l.GetChild(name),
_ => throw new InvalidOperationException()
};
} }
} }

Loading…
Cancel
Save