-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDebug.cs
More file actions
133 lines (110 loc) · 3.86 KB
/
Debug.cs
File metadata and controls
133 lines (110 loc) · 3.86 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
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
using System.Runtime.CompilerServices;
namespace WhoisNET
{
/// <summary>
/// Writes debugging to the console.
/// </summary>
public static class Debug
{
/// <summary>
/// Defines the default level of debugging to use.
/// </summary>
private static volatile LogLevel _logLevel = LogLevel.Off;
/// <summary>
/// Writes to the console with specified message.
/// </summary>
/// <param name="msg">Message to write</param>
/// <param name="method">(optional) Source method</param>
/// <param name="color">(optional) Color of console text</param>
public static void Write(string msg, [CallerMemberName] string? method = null, ConsoleColor? color = null)
{
if (_logLevel == LogLevel.Off)
return;
Console.ResetColor();
if (color is not null)
Console.ForegroundColor = color.GetValueOrDefault();
else
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine($"({DateTime.Now}) [{method}]: {msg}");
Console.ResetColor();
}
/// <summary>
/// Writes debug message to the console with specified message.
/// </summary>
/// <param name="msg">Message to write</param>
/// <param name="method">(optional) Source method</param>
public static void WriteDebug(string msg, [CallerMemberName] string? method = null)
{
if (_logLevel != LogLevel.Debug)
return;
Write(msg, method, ConsoleColor.Yellow);
}
/// <summary>
/// Writes debug message to the console with specified message.
/// </summary>
/// <param name="msg">Message to write</param>
/// <param name="method">(optional) Source method</param>
public static void WriteVerbose(string msg, [CallerMemberName] string? method = null)
{
if (_logLevel != LogLevel.Verbose)
return;
Write(msg, method, ConsoleColor.Magenta);
}
/// <summary>
/// Writes exception to the console with specified message.
/// </summary>
/// <param name="msg">Message to write</param>
/// <param name="method">(optional) Source method</param>
public static void WriteException(string msg, [CallerMemberName] string? method = null)
{
if (_logLevel != LogLevel.Exception && _logLevel != LogLevel.Debug)
return;
Write(msg, method, ConsoleColor.DarkRed);
}
/// <summary>
/// Throws an exception.
/// </summary>
/// <param name="msg">Message to write</param>
/// <param name="method">(optional) Source method</param>
/// <param name="exception">(optional) The thrown exception</param>
public static void ThrowException(string msg, [CallerMemberName] string? method = null, Exception? exception = null)
{
WriteException(msg, method);
if (exception is null)
throw new Exception(msg);
throw exception;
}
/// <summary>
/// Sets the default logging level.
/// </summary>
public static LogLevel SetLogLevel
{
set
{
_logLevel = value;
}
}
}
/// <summary>
/// Defines the log level.
/// </summary>
public enum LogLevel
{
/// <summary>
/// Logging is off.
/// </summary>
Off = 0,
/// <summary>
/// Only log exceptions
/// </summary>
Exception = 1,
/// <summary>
/// Log debug messages
/// </summary>
Debug = 2,
/// <summary>
/// Log verbose messages
/// </summary>
Verbose = 3
}
}