fix: fix for logging deadlock, use mutexes, add logging for last message sent.

This commit is contained in:
Jack Kanarish
2019-07-31 16:22:05 -04:00
parent 1be852685d
commit 7c238b9fef

View File

@@ -8,35 +8,44 @@ namespace ICD.Common.Utils
{ {
public static class IcdErrorLog public static class IcdErrorLog
{ {
private static readonly SafeCriticalSection s_LoggingSection; private const int MUTEX_TIMEOUT = 30 * 1000;
private static readonly SafeMutex s_SafeMutex;
private static string s_LastMessage;
/// <summary> /// <summary>
/// Static constructor. /// Static constructor.
/// </summary> /// </summary>
static IcdErrorLog() static IcdErrorLog()
{ {
s_LoggingSection = new SafeCriticalSection(); s_SafeMutex = new SafeMutex();
} }
[PublicAPI] [PublicAPI]
public static void Error(string message) public static void Error(string message)
{ {
s_LoggingSection.Enter(); if(s_SafeMutex.WaitForMutex(MUTEX_TIMEOUT))
try
{ {
try
{
s_LastMessage = message;
#if SIMPLSHARP #if SIMPLSHARP
message = FormatConsoleColor(message, ConsoleColorExtensions.CONSOLE_RED); message = FormatConsoleColor(message, ConsoleColorExtensions.CONSOLE_RED);
ErrorLog.Error(message); ErrorLog.Error(message);
#else #else
System.Console.ForegroundColor = ConsoleColor.Red; System.Console.ForegroundColor = ConsoleColor.Red;
System.Console.Error.WriteLine(message); System.Console.Error.WriteLine(message);
System.Console.ResetColor(); System.Console.ResetColor();
#endif #endif
}
finally
{
s_SafeMutex.ReleaseMutex();
}
} }
finally else
{ {
s_LoggingSection.Leave(); IcdConsole.PrintLine(eConsoleColor.YellowOnRed, "Deadlock in Error Logging, last logged message is:");
IcdConsole.PrintLine(eConsoleColor.YellowOnRed, StringUtils.ToMixedReadableHexLiteral(s_LastMessage));
} }
} }
@@ -49,22 +58,29 @@ namespace ICD.Common.Utils
[PublicAPI] [PublicAPI]
public static void Warn(string message) public static void Warn(string message)
{ {
s_LoggingSection.Enter(); if (s_SafeMutex.WaitForMutex(MUTEX_TIMEOUT))
try
{ {
try
{
s_LastMessage = message;
#if SIMPLSHARP #if SIMPLSHARP
message = FormatConsoleColor(message, ConsoleColorExtensions.CONSOLE_YELLOW); message = FormatConsoleColor(message, ConsoleColorExtensions.CONSOLE_YELLOW);
ErrorLog.Warn(message); ErrorLog.Warn(message);
#else #else
System.Console.ForegroundColor = ConsoleColor.Yellow; System.Console.ForegroundColor = ConsoleColor.Yellow;
System.Console.Error.WriteLine(message); System.Console.Error.WriteLine(message);
System.Console.ResetColor(); System.Console.ResetColor();
#endif #endif
}
finally
{
s_SafeMutex.ReleaseMutex();
}
} }
finally else
{ {
s_LoggingSection.Leave(); IcdConsole.PrintLine(eConsoleColor.YellowOnRed, "Deadlock in Error Logging, last logged message is:");
IcdConsole.PrintLine(eConsoleColor.YellowOnRed, StringUtils.ToMixedReadableHexLiteral(s_LastMessage));
} }
} }
@@ -77,22 +93,29 @@ namespace ICD.Common.Utils
[PublicAPI] [PublicAPI]
public static void Notice(string message) public static void Notice(string message)
{ {
s_LoggingSection.Enter(); if (s_SafeMutex.WaitForMutex(MUTEX_TIMEOUT))
try
{ {
s_LastMessage = message;
try
{
#if SIMPLSHARP #if SIMPLSHARP
message = FormatConsoleColor(message, ConsoleColorExtensions.CONSOLE_BLUE); message = FormatConsoleColor(message, ConsoleColorExtensions.CONSOLE_BLUE);
ErrorLog.Notice(message); ErrorLog.Notice(message);
#else #else
System.Console.ForegroundColor = ConsoleColor.Blue; System.Console.ForegroundColor = ConsoleColor.Blue;
System.Console.Error.WriteLine(message); System.Console.Error.WriteLine(message);
System.Console.ResetColor(); System.Console.ResetColor();
#endif #endif
}
finally
{
s_SafeMutex.ReleaseMutex();
}
} }
finally else
{ {
s_LoggingSection.Leave(); IcdConsole.PrintLine(eConsoleColor.YellowOnRed, "Deadlock in Error Logging, last logged message is:");
IcdConsole.PrintLine(eConsoleColor.YellowOnRed, StringUtils.ToMixedReadableHexLiteral(s_LastMessage));
} }
} }
@@ -105,22 +128,29 @@ namespace ICD.Common.Utils
[PublicAPI] [PublicAPI]
public static void Ok(string message) public static void Ok(string message)
{ {
s_LoggingSection.Enter(); if (s_SafeMutex.WaitForMutex(MUTEX_TIMEOUT))
try
{ {
s_LastMessage = message;
try
{
#if SIMPLSHARP #if SIMPLSHARP
message = FormatConsoleColor(message, ConsoleColorExtensions.CONSOLE_GREEN); message = FormatConsoleColor(message, ConsoleColorExtensions.CONSOLE_GREEN);
ErrorLog.Ok(message); ErrorLog.Ok(message);
#else #else
System.Console.ForegroundColor = ConsoleColor.Green; System.Console.ForegroundColor = ConsoleColor.Green;
System.Console.Error.WriteLine(message); System.Console.Error.WriteLine(message);
System.Console.ResetColor(); System.Console.ResetColor();
#endif #endif
}
finally
{
s_SafeMutex.ReleaseMutex();
}
} }
finally else
{ {
s_LoggingSection.Leave(); IcdConsole.PrintLine(eConsoleColor.YellowOnRed, "Deadlock in Error Logging, last logged message is:");
IcdConsole.PrintLine(eConsoleColor.YellowOnRed, StringUtils.ToMixedReadableHexLiteral(s_LastMessage));
} }
} }
@@ -133,13 +163,14 @@ namespace ICD.Common.Utils
[PublicAPI] [PublicAPI]
public static void Exception(Exception ex, string message) public static void Exception(Exception ex, string message)
{ {
s_LoggingSection.Enter(); if (s_SafeMutex.WaitForMutex(MUTEX_TIMEOUT))
try
{ {
s_LastMessage = message;
try
{
#if SIMPLSHARP #if SIMPLSHARP
message = FormatConsoleColor(message, ConsoleColorExtensions.CONSOLE_YELLOW_ON_RED_BACKGROUND); message = FormatConsoleColor(message, ConsoleColorExtensions.CONSOLE_YELLOW_ON_RED_BACKGROUND);
ErrorLog.Exception(message, ex); ErrorLog.Exception(message, ex);
#else #else
System.Console.ForegroundColor = ConsoleColor.Yellow; System.Console.ForegroundColor = ConsoleColor.Yellow;
System.Console.BackgroundColor = ConsoleColor.Red; System.Console.BackgroundColor = ConsoleColor.Red;
@@ -147,10 +178,16 @@ namespace ICD.Common.Utils
System.Console.ResetColor(); System.Console.ResetColor();
System.Console.Error.WriteLine(ex.StackTrace); System.Console.Error.WriteLine(ex.StackTrace);
#endif #endif
}
finally
{
s_SafeMutex.ReleaseMutex();
}
} }
finally else
{ {
s_LoggingSection.Leave(); IcdConsole.PrintLine(eConsoleColor.YellowOnRed, "Deadlock in Error Logging, last logged message is:");
IcdConsole.PrintLine(eConsoleColor.YellowOnRed, StringUtils.ToMixedReadableHexLiteral(s_LastMessage));
} }
} }
@@ -164,22 +201,29 @@ namespace ICD.Common.Utils
[PublicAPI] [PublicAPI]
public static void Info(string message) public static void Info(string message)
{ {
s_LoggingSection.Enter(); if (s_SafeMutex.WaitForMutex(MUTEX_TIMEOUT))
try
{ {
s_LastMessage = message;
try
{
#if SIMPLSHARP #if SIMPLSHARP
message = FormatConsoleColor(message, ConsoleColorExtensions.CONSOLE_CYAN); message = FormatConsoleColor(message, ConsoleColorExtensions.CONSOLE_CYAN);
ErrorLog.Info(message); ErrorLog.Info(message);
#else #else
System.Console.ForegroundColor = ConsoleColor.Cyan; System.Console.ForegroundColor = ConsoleColor.Cyan;
System.Console.Error.WriteLine(message); System.Console.Error.WriteLine(message);
System.Console.ResetColor(); System.Console.ResetColor();
#endif #endif
}
finally
{
s_SafeMutex.ReleaseMutex();
}
} }
finally else
{ {
s_LoggingSection.Leave(); IcdConsole.PrintLine(eConsoleColor.YellowOnRed, "Deadlock in Error Logging, last logged message is:");
IcdConsole.PrintLine(eConsoleColor.YellowOnRed, StringUtils.ToMixedReadableHexLiteral(s_LastMessage));
} }
} }