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
{
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>
/// Static constructor.
/// </summary>
static IcdErrorLog()
{
s_LoggingSection = new SafeCriticalSection();
s_SafeMutex = new SafeMutex();
}
[PublicAPI]
public static void Error(string message)
{
s_LoggingSection.Enter();
try
if(s_SafeMutex.WaitForMutex(MUTEX_TIMEOUT))
{
try
{
s_LastMessage = message;
#if SIMPLSHARP
message = FormatConsoleColor(message, ConsoleColorExtensions.CONSOLE_RED);
ErrorLog.Error(message);
message = FormatConsoleColor(message, ConsoleColorExtensions.CONSOLE_RED);
ErrorLog.Error(message);
#else
System.Console.ForegroundColor = ConsoleColor.Red;
System.Console.Error.WriteLine(message);
System.Console.ResetColor();
#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]
public static void Warn(string message)
{
s_LoggingSection.Enter();
try
if (s_SafeMutex.WaitForMutex(MUTEX_TIMEOUT))
{
try
{
s_LastMessage = message;
#if SIMPLSHARP
message = FormatConsoleColor(message, ConsoleColorExtensions.CONSOLE_YELLOW);
ErrorLog.Warn(message);
message = FormatConsoleColor(message, ConsoleColorExtensions.CONSOLE_YELLOW);
ErrorLog.Warn(message);
#else
System.Console.ForegroundColor = ConsoleColor.Yellow;
System.Console.Error.WriteLine(message);
System.Console.ResetColor();
#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]
public static void Notice(string message)
{
s_LoggingSection.Enter();
try
if (s_SafeMutex.WaitForMutex(MUTEX_TIMEOUT))
{
s_LastMessage = message;
try
{
#if SIMPLSHARP
message = FormatConsoleColor(message, ConsoleColorExtensions.CONSOLE_BLUE);
ErrorLog.Notice(message);
message = FormatConsoleColor(message, ConsoleColorExtensions.CONSOLE_BLUE);
ErrorLog.Notice(message);
#else
System.Console.ForegroundColor = ConsoleColor.Blue;
System.Console.Error.WriteLine(message);
System.Console.ResetColor();
#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]
public static void Ok(string message)
{
s_LoggingSection.Enter();
try
if (s_SafeMutex.WaitForMutex(MUTEX_TIMEOUT))
{
s_LastMessage = message;
try
{
#if SIMPLSHARP
message = FormatConsoleColor(message, ConsoleColorExtensions.CONSOLE_GREEN);
ErrorLog.Ok(message);
message = FormatConsoleColor(message, ConsoleColorExtensions.CONSOLE_GREEN);
ErrorLog.Ok(message);
#else
System.Console.ForegroundColor = ConsoleColor.Green;
System.Console.Error.WriteLine(message);
System.Console.ResetColor();
#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]
public static void Exception(Exception ex, string message)
{
s_LoggingSection.Enter();
try
if (s_SafeMutex.WaitForMutex(MUTEX_TIMEOUT))
{
s_LastMessage = message;
try
{
#if SIMPLSHARP
message = FormatConsoleColor(message, ConsoleColorExtensions.CONSOLE_YELLOW_ON_RED_BACKGROUND);
ErrorLog.Exception(message, ex);
message = FormatConsoleColor(message, ConsoleColorExtensions.CONSOLE_YELLOW_ON_RED_BACKGROUND);
ErrorLog.Exception(message, ex);
#else
System.Console.ForegroundColor = ConsoleColor.Yellow;
System.Console.BackgroundColor = ConsoleColor.Red;
@@ -147,10 +178,16 @@ namespace ICD.Common.Utils
System.Console.ResetColor();
System.Console.Error.WriteLine(ex.StackTrace);
#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]
public static void Info(string message)
{
s_LoggingSection.Enter();
try
if (s_SafeMutex.WaitForMutex(MUTEX_TIMEOUT))
{
s_LastMessage = message;
try
{
#if SIMPLSHARP
message = FormatConsoleColor(message, ConsoleColorExtensions.CONSOLE_CYAN);
ErrorLog.Info(message);
message = FormatConsoleColor(message, ConsoleColorExtensions.CONSOLE_CYAN);
ErrorLog.Info(message);
#else
System.Console.ForegroundColor = ConsoleColor.Cyan;
System.Console.Error.WriteLine(message);
System.Console.ResetColor();
#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));
}
}