Skip to content

Commit

Permalink
Added try/catch to log thread (YARC-Official#199)
Browse files Browse the repository at this point in the history
  • Loading branch information
EliteAsian123 authored Jul 30, 2024
1 parent f34023f commit 3059e63
Showing 1 changed file with 57 additions and 31 deletions.
88 changes: 57 additions & 31 deletions YARG.Core/Logging/YargLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,30 +69,16 @@ public static void RemoveLogListener(BaseYargLogListener listener)
public static void KillLogger()
{
_isLoggingEnabled = false;
FlushLogQueue();

lock (LogQueue)
// Dispose of all listeners
lock (Listeners)
{
while (LogQueue.TryDequeue(out var item))
foreach (var listener in Listeners)
{
using (item)
{
// Send it to all listeners that are currently registered
lock (Listeners)
{
foreach (var listener in Listeners)
{
_logBuilder.Clear();
listener.FormatLogItem(ref _logBuilder, item);
listener.WriteLogItem(ref _logBuilder, item);
}
}
}
listener.Dispose();
}
}

foreach (var listener in Listeners)
{
listener.Dispose();
Listeners.Clear();
}
}

Expand All @@ -102,26 +88,66 @@ private static void LogOutputter()
// In the event logging is disabled, we still want to process all remaining log items
while (_isLoggingEnabled || LogQueue.Count > 0)
{
// Lock the queue and process all items
lock (LogQueue)
FlushLogQueue();

// Sleep for a short time. Logs will process at most every LOG_INTERVAL milliseconds
Thread.Sleep(LOG_INTERVAL);
}
}

private static void FlushLogQueue()
{
lock (LogQueue)
{
lock (Listeners)
{
while (LogQueue.TryDequeue(out var item))
{
using (item)
{
// Send it to all listeners that are currently registered
foreach (var listener in Listeners)
{
_logBuilder.Clear();
listener.FormatLogItem(ref _logBuilder, item);
listener.WriteLogItem(ref _logBuilder, item);
}
WriteLogItemToListeners(item);
}
}
}
}
}

// Sleep for a short time. Logs will process at most every LOG_INTERVAL milliseconds
Thread.Sleep(LOG_INTERVAL);
private static void WriteLogItemToListeners(LogItem item)
{
// Send it to all listeners that are currently registered
foreach (var listener in Listeners)
{
try
{
_logBuilder.Clear();
listener.FormatLogItem(ref _logBuilder, item);
listener.WriteLogItem(ref _logBuilder, item);
}
catch (Exception e)
{
// In the event formatting the log fails, print an error message with the exception
try
{
using var exceptionLog = FormatLogItem.MakeItem(
"Failed to format the log on this line! Refer to the exception below.\n{0}", e);
exceptionLog.Level = LogLevel.Error;

// Make sure to pass down the source information so the position
// of the original log is known.
exceptionLog.Source = item.Source;
exceptionLog.Method = item.Method;
exceptionLog.Line = item.Line;
exceptionLog.Time = item.Time;

_logBuilder.Clear();
listener.FormatLogItem(ref _logBuilder, exceptionLog);
listener.WriteLogItem(ref _logBuilder, exceptionLog);
}
catch
{
// If that fails too, just skip this log
}
}
}
}

Expand Down

0 comments on commit 3059e63

Please sign in to comment.