Skip to content

Commit

Permalink
Minor log library optimisation
Browse files Browse the repository at this point in the history
  • Loading branch information
Sparronator9999 committed Dec 6, 2024
1 parent ac93a2d commit 69992ba
Showing 1 changed file with 12 additions and 14 deletions.
26 changes: 12 additions & 14 deletions YAMDCC.Logs/Logger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,9 @@ public sealed class Logger : IDisposable

/// <summary>
/// Used with <see langword="lock"/> to prevent more
/// than one thread writing to the file at once.
/// than one thread writing to the console at once.
/// </summary>
private readonly object
fileLock = new(),
consoleLock = new();
private readonly object consoleLock = new();

/// <summary>
/// The newline characters to split provided log message lines by.
Expand Down Expand Up @@ -75,12 +73,12 @@ private static string LogString(string text, LogLevel level, bool showDate) =>
/// <summary>
/// Should the log time be shown in console logs?
/// </summary>
public bool ShowTimeInConsole { get; set; }
public bool LogTimeToConsole { get; set; }

/// <summary>
/// Should the log time be shown in logs written to disk?
/// </summary>
public bool ShowTimeInFile { get; set; } = true;
public bool LogTimeToFile { get; set; } = true;

/// <summary>
/// Writes a Debug event to the <see cref="Logger"/>.
Expand Down Expand Up @@ -263,16 +261,16 @@ public void DeleteArchivedLogs()

private void WriteFile(string message, LogLevel level)
{
lock (fileLock)
if (LogWriter is null)
{
if (LogWriter is null || LogWriter == null || LogWriter.Equals(null))
{
InitLogFile();
}
InitLogFile();
}

lock (LogWriter)
{
foreach (string str in message.Split(NewlineChars, StringSplitOptions.RemoveEmptyEntries))
{
LogWriter.WriteLine(LogString(str, level, ShowTimeInFile));
LogWriter.WriteLine(LogString(str, level, LogTimeToFile));
}
}
}
Expand All @@ -288,7 +286,7 @@ private void WriteConsole(string message, LogLevel level)
{
case LogLevel.Fatal:
Console.BackgroundColor = ConsoleColor.Yellow;
Console.ForegroundColor = ConsoleColor.DarkBlue;
Console.ForegroundColor = ConsoleColor.DarkRed;
break;
case LogLevel.Error:
Console.ForegroundColor = ConsoleColor.Red;
Expand All @@ -306,7 +304,7 @@ private void WriteConsole(string message, LogLevel level)

foreach (string str in message.Split(NewlineChars, StringSplitOptions.RemoveEmptyEntries))
{
Console.WriteLine(LogString(str, level, ShowTimeInConsole));
Console.WriteLine(LogString(str, level, LogTimeToConsole));
}

Console.BackgroundColor = bgColour;
Expand Down

0 comments on commit 69992ba

Please sign in to comment.