Skip to content

Commit

Permalink
Added semaphore to make Debug Logging thread-safe
Browse files Browse the repository at this point in the history
  • Loading branch information
JimmahDean committed Aug 17, 2023
1 parent c27417b commit 9d06c57
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 27 deletions.
54 changes: 30 additions & 24 deletions client/DebugLog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@ public static class DebugLog
private static bool Started = false;
private static DateTime LogStartTime { get; set; }
private const string LogFilePath = "specify_debug.log";
private const string LogFailureFilePath = "specify_log_failure.log";
private static readonly bool[] RegionStarted = new bool[5];
private static readonly bool[] RegionCompleted = new bool[5];
private static readonly DateTime[] RegionStartTime = new DateTime[5];
private static Dictionary<string, DateTime>[] OpenTasks = new Dictionary<string, DateTime>[5];

private static SemaphoreSlim logSemaphore = new(1, 1);

public enum Region
{
Main = 0,
Expand Down Expand Up @@ -177,32 +180,34 @@ public static async Task LogEventAsync(string message, Region region = Region.Mi
{
debugString = CreateDebugString($"Logging attempted on uninitialized region - {message}", region, EventType.ERROR);
}
var timeout = 5;
var currentTime = DateTime.Now;
while (true)

if (Settings.EnableDebug)
{
if (Settings.EnableDebug)
await logSemaphore.WaitAsync();
int retryCount = 0;
while (true)
{
try
{
await Task.Run(() => File.AppendAllText(LogFilePath, debugString));
var writer = new StreamWriter(LogFilePath, true);
await writer.WriteAsync(debugString);
writer.Close();
break;
}
catch
catch (Exception ex)
{
await Task.Delay(30);
if ((DateTime.Now - currentTime).TotalSeconds > timeout)
if(retryCount > 10)
{
File.WriteAllText(LogFailureFilePath, ex.ToString());
Settings.EnableDebug = false;
break;
}
await Task.Delay(30);
retryCount++;
continue;
}
}
else
{
break;
}
logSemaphore.Release();
}
LogText += debugString;
}
Expand All @@ -218,32 +223,33 @@ public static void LogEvent(string message, Region region = Region.Misc, EventTy
{
debugString = CreateDebugString($"Logging attempted on uninitialized region - {message}", region, EventType.ERROR);
}
var timeout = 5;
var currentTime = DateTime.Now;
while (true)
if (Settings.EnableDebug)
{
if (Settings.EnableDebug)
logSemaphore.Wait();
int retryCount = 0;
while (true)
{
try
{
File.AppendAllText(LogFilePath, debugString);
var writer = new StreamWriter(LogFilePath, true);
writer.Write(debugString);
writer.Close();
break;
}
catch
catch (Exception ex)
{
Thread.Sleep(30);
if ((DateTime.Now - currentTime).TotalSeconds > timeout)
if (retryCount > 10)
{
File.WriteAllText(LogFailureFilePath, ex.ToString());
Settings.EnableDebug = false;
break;
}
Thread.Sleep(30);
retryCount++;
continue;
}
}
else
{
break;
}
logSemaphore.Release();
}
LogText += debugString;
}
Expand Down
6 changes: 3 additions & 3 deletions client/data/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -173,17 +173,17 @@ public static Browser.Extension ParseChromiumExtension(string path)
}
catch (FileNotFoundException)
{
DebugLog.LogEvent($"Chromium extension files could not be found.", DebugLog.Region.System, DebugLog.EventType.ERROR);
DebugLog.LogEvent($"Chromium extension files could not be found.", DebugLog.Region.System, DebugLog.EventType.WARNING);
return null;
}
catch (JsonException)
{
DebugLog.LogEvent($"Chromium extension json files corrupt or invalid.", DebugLog.Region.System, DebugLog.EventType.ERROR);
DebugLog.LogEvent($"Chromium extension json files corrupt or invalid.", DebugLog.Region.System, DebugLog.EventType.WARNING);
return null;
}
catch (ArgumentOutOfRangeException)
{
DebugLog.LogEvent($"Chromium extension path invalid: {path}", DebugLog.Region.System, DebugLog.EventType.ERROR);
DebugLog.LogEvent($"Chromium extension path invalid: {path}", DebugLog.Region.System, DebugLog.EventType.WARNING);
return null;
}
catch (Exception e)
Expand Down

0 comments on commit 9d06c57

Please sign in to comment.