-
Notifications
You must be signed in to change notification settings - Fork 0
/
Log.cs
153 lines (139 loc) · 5.33 KB
/
Log.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
using System;
using System.Collections.Concurrent;
using System.Threading;
namespace AudioReader
{
internal static class Log
{
public enum LogLevel
{
Verbose, Debug, Info, Warn, Error, Off
}
private class LogEntry
{
public LogLevel LogLevel;
public string Tag;
public string Message;
public LogEntry(LogLevel logLevel, string tag, string message)
{
LogLevel = logLevel;
Tag = tag;
Message = message;
}
public void LogToConsole()
{
if (_changeConsoleColor(LogLevel, out var consoleColor))
{
Console.ForegroundColor = consoleColor;
}
Console.WriteLine(_getLogLevelString(LogLevel) + " " + Tag.PadRight(TagLength).Substring(0, TagLength) + " : " + Message);
Console.ResetColor();
}
private static string _getLogLevelString(LogLevel logLevel)
{
switch (logLevel)
{
case LogLevel.Verbose:
return "[Vrb]";
case LogLevel.Debug:
return "[Dbg]";
case LogLevel.Info:
return "[Inf]";
case LogLevel.Warn:
return "[Wrn]";
case LogLevel.Error:
return "[Err]";
default:
return "[N/A]";
}
}
private static bool _changeConsoleColor(LogLevel logLevel, out ConsoleColor consoleColor)
{
consoleColor = ConsoleColor.White;
switch (logLevel)
{
case LogLevel.Warn:
consoleColor = ConsoleColor.Yellow;
return true;
case LogLevel.Error:
consoleColor = ConsoleColor.Red;
return true;
default:
return false;
}
}
}
private static BlockingCollection<LogEntry> _queue = new BlockingCollection<LogEntry>();
private static Thread _logLoopThread = new Thread(_logLoop);
private static bool _runLogLoop = true;
private static object _logLoopLock = new object();
private static LogLevel _level = LogLevel.Off;
public static int TagLength = 15;
private static void _exitHandler(object sender, EventArgs e) => Disable();
private static void _logLoop()
{
while (true)
{
lock (_logLoopLock)
{
if (!_runLogLoop)
{
return;
}
}
_queue.Take().LogToConsole();
}
}
private static void _add(LogLevel logLevel, string tag, string message)
{
if (logLevel >= _level)
_queue.Add(new LogEntry(logLevel, tag, message));
}
public static void Enable(LogLevel logLevel)
{
_level = logLevel;
if (!_logLoopThread.IsAlive && logLevel < LogLevel.Off)
{
_logLoopThread.Start();
AppDomain.CurrentDomain.ProcessExit += _exitHandler;
}
}
public static void Enable(string logLevelString)
{
LogLevel logLevel;
try
{
logLevel = (LogLevel)System.Enum.Parse(typeof(LogLevel), logLevelString);
}
catch (Exception ex)
{
if (ex is ArgumentNullException
|| ex is ArgumentException
|| ex is OverflowException)
logLevel = LogLevel.Info;
else
throw;
}
Enable(logLevel);
}
public static void Disable()
{
_level = LogLevel.Off;
lock (_logLoopLock)
{
_runLogLoop = false;
}
_logLoopThread.Join();
}
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
public static void Verbose(string tag, string message) => _add(LogLevel.Verbose, tag, message);
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
public static void Debug(string tag, string message) => _add(LogLevel.Debug, tag, message);
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
public static void Info(string tag, string message) => _add(LogLevel.Info, tag, message);
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
public static void Warn(string tag, string message) => _add(LogLevel.Warn, tag, message);
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
public static void Error(string tag, string message) => _add(LogLevel.Error, tag, message);
}
}