Repository navigation
Expand file tree
/
Copy pathLog.cs
More file actions
104 lines (89 loc) · 3.85 KB
/
Copy pathLog.cs
File metadata and controls
104 lines (89 loc) · 3.85 KB
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
using System;
using System.Collections.Generic;
namespace MiSideMultiplayer.RelayServer
{
internal enum LogLevel { Debug, Info, Warn, Error, Fatal }
internal static class Log
{
private static readonly object ConsoleLock = new object();
private static readonly List<Action<LogLevel, string, string>> Handlers =
new List<Action<LogLevel, string, string>>();
public static LogLevel MinimumLevel = LogLevel.Info;
public static void AddHandler(Action<LogLevel, string, string> handler)
{
lock (Handlers) Handlers.Add(handler);
}
public static void Debug(string source, string message) => Write(LogLevel.Debug, source, message);
public static void Info(string source, string message) => Write(LogLevel.Info, source, message);
public static void Warn(string source, string message) => Write(LogLevel.Warn, source, message);
public static void Error(string source, string message) => Write(LogLevel.Error, source, message);
public static void Fatal(string source, string message, Exception ex)
{
Write(LogLevel.Fatal, source,
message + Environment.NewLine + " " + ex.GetType().Name + ": " + ex.Message +
(ex.StackTrace != null ? Environment.NewLine + ex.StackTrace : string.Empty));
}
private static void Write(LogLevel level, string source, string message)
{
if (level < MinimumLevel) return;
string timestamp = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
string levelTag = GetLevelTag(level);
ConsoleColor color = GetLevelColor(level);
lock (ConsoleLock)
{
ConsoleColor previous = Console.ForegroundColor;
try
{
Console.ForegroundColor = ConsoleColor.DarkGray;
Console.Write("[" + timestamp + "] ");
Console.ForegroundColor = color;
Console.Write(levelTag + " ");
Console.ForegroundColor = ConsoleColor.DarkCyan;
Console.Write("[" + source + "] ");
Console.ForegroundColor = previous;
Console.WriteLine(message);
Console.Out.Flush();
}
catch
{
// never let logging take down the thread
}
finally
{
try { Console.ForegroundColor = previous; } catch { }
}
}
lock (Handlers)
{
foreach (var h in Handlers)
{
try { h(level, source, message); } catch { }
}
}
}
private static string GetLevelTag(LogLevel level)
{
switch (level)
{
case LogLevel.Debug: return "[DEBUG]";
case LogLevel.Info: return "[INFO] ";
case LogLevel.Warn: return "[WARN] ";
case LogLevel.Error: return "[ERROR]";
case LogLevel.Fatal: return "[FATAL]";
default: return "[LOG] ";
}
}
private static ConsoleColor GetLevelColor(LogLevel level)
{
switch (level)
{
case LogLevel.Debug: return ConsoleColor.Gray;
case LogLevel.Info: return ConsoleColor.Cyan;
case LogLevel.Warn: return ConsoleColor.Yellow;
case LogLevel.Error: return ConsoleColor.Red;
case LogLevel.Fatal: return ConsoleColor.Magenta;
default: return ConsoleColor.White;
}
}
}
}