forked from LukeZurg22/SKSSL
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDustLogger.cs
More file actions
184 lines (158 loc) · 6.88 KB
/
DustLogger.cs
File metadata and controls
184 lines (158 loc) · 6.88 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Console;
namespace SKSSL;
/// <summary>
/// A built-in error handler coming from the DustToDust project. This follows Microsoft's guidelines for
/// performance-efficient error logging. The errors type's here are generalized for use elsewhere.
/// </summary>
public static partial class DustLogger
{
private static readonly ILogger logger;
/// <summary>
/// Enumerable containing available error codes which are used in the <see cref="DustLogger"/>.
/// <code>
/// [ERROR CODE ENTRY] [CODE]
/// INFORMATIONAL_PRINT = 0x0,
/// -warnings-
/// GENERAL_WARNING = 0x1,
/// META_DATA_WARNING = 0x2,
/// FILE_WARNING = 0x3,
/// SYSTEM_WARNING = 0x4,
/// -errors-
/// GENERAL_ERROR = 0x5,
/// META_DATA_ERROR = 0x6,
/// FILE_ERROR = 0x7,
/// SYSTEM_ERROR = 0x8,
/// </code>
/// </summary>
public enum LOG : byte
{
// Info
INFORMATIONAL_PRINT = 0x0,
// Warnings
/// General warning. Not unimportant enough to be INFO.
/// Default to this if generally unsure.
GENERAL_WARNING = 0x1,
/// Warning concerning invalid metadata.
META_DATA_WARNING = 0x2,
/// Warning concerning [de]serialization.
FILE_WARNING = 0x3,
/// Warning of possible system or operating system issue.
SYSTEM_WARNING = 0x4,
// Errors
/// Error with no specific root cause. Default to this if unsure, but there is definitely a problem.
GENERAL_ERROR = 0x5,
/// Error involving file metadata.
META_DATA_ERROR = 0x6,
/// Error involving a file's contents.
FILE_ERROR = 0x7,
/// Error involving system failure.
SYSTEM_ERROR = 0x8,
}
#pragma warning disable CA2017
internal static readonly Action<ILogger, string, Exception?> INFO_PRINT =
LoggerMessage.Define<string>(LogLevel.Information,
new EventId((byte)LOG.INFORMATIONAL_PRINT, nameof(INFO_PRINT)), "[INFO]: {Message}");
internal static readonly Action<ILogger, string, Exception?> GENERAL_WARNING =
LoggerMessage.Define<string>(LogLevel.Warning, new EventId((byte)LOG.GENERAL_WARNING, nameof(GENERAL_WARNING)),
"[GENERAL WARNING]: {Message}");
internal static readonly Action<ILogger, string, Exception?> META_WARNING =
LoggerMessage.Define<string>(LogLevel.Warning, new EventId((byte)LOG.META_DATA_WARNING, nameof(META_WARNING)),
"[METADATA WARNING]: {Message}");
internal static readonly Action<ILogger, string, Exception?> FILE_WARNING =
LoggerMessage.Define<string>(LogLevel.Warning, new EventId((byte)LOG.FILE_WARNING, nameof(FILE_WARNING)),
"[FILE WARNING]: {Message}");
internal static readonly Action<ILogger, string, Exception?> SYSTEM_WARNING =
LoggerMessage.Define<string>(LogLevel.Warning, new EventId((byte)LOG.SYSTEM_WARNING, nameof(SYSTEM_WARNING)),
"[SYSTEM WARNING]: {Message}");
internal static readonly Action<ILogger, string, Exception?> GENERAL_ERROR =
LoggerMessage.Define<string>(LogLevel.Error, new EventId((byte)LOG.GENERAL_ERROR, nameof(GENERAL_ERROR)),
"[GENERAL ERROR]: {Message}");
internal static readonly Action<ILogger, string, Exception?> META_ERROR =
LoggerMessage.Define<string>(LogLevel.Error, new EventId((byte)LOG.META_DATA_ERROR, nameof(META_ERROR)),
"[METADATA ERROR]: {Message}");
internal static readonly Action<ILogger, string, Exception?> FILE_ERROR =
LoggerMessage.Define<string>(LogLevel.Error, new EventId((byte)LOG.FILE_ERROR, nameof(FILE_ERROR)),
"[FILE ERROR]: {Message}");
internal static readonly Action<ILogger, string, Exception?> SYSTEM_ERROR =
LoggerMessage.Define<string>(LogLevel.Error, new EventId((byte)LOG.SYSTEM_ERROR, nameof(SYSTEM_ERROR)),
"[SYSTEM ERROR]: {Message}");
#pragma warning restore CA2017
static DustLogger()
{
using ILoggerFactory loggerFactory = LoggerFactory.Create(builder
=> builder.SetMinimumLevel(LogLevel.Debug).AddConsole(options =>
{
_ = new ConsoleFormatterOptions
{
IncludeScopes = false,
TimestampFormat = "HH:mm:ss"
};
}).AddSimpleConsole(options =>
{
options.IncludeScopes = false;
options.SingleLine = true;
options.TimestampFormat = "HH:mm:ss ";
})); // Should work with console.
logger = loggerFactory.CreateLogger("SKSSL");
}
/// <inheritdoc cref="Log(string,SKSSL.DustLogger.LOG,bool)"/>
/// Overload using enum, which is cast to byte.
public static void Log(string message, LOG log, bool outputToFile = false) => Log(message, (byte)log, outputToFile);
public static void WriteToFile(string message)
{
// IMPL: append message to dedicated log file. do not erase, nor override.
}
/// <summary>
/// <seealso cref="LOG"/>
/// </summary>
/// <param name="message">The message that is being output to console.</param>
/// <param name="level">Logging level and type. Defaults to 0 (INFO).</param>
/// <param name="outputToFile">Dictates if this message should be logged.</param> // TODO: File-logging is not implemented yet!
public static void Log(string message, int level = 0, bool outputToFile = false)
{
var e = (LOG)level; // cast to internal enum
var exception = new Exception(message)
{
Source = "SKSSL"
};
switch (e)
{
// Errors
case LOG.GENERAL_ERROR:
GENERAL_ERROR(logger, string.Empty, exception);
break;
case LOG.META_DATA_ERROR:
META_ERROR(logger, string.Empty, exception);
break;
case LOG.FILE_ERROR:
FILE_ERROR(logger, string.Empty, exception);
break;
case LOG.SYSTEM_ERROR:
SYSTEM_ERROR(logger, string.Empty, exception);
break;
// Warnings
case LOG.META_DATA_WARNING:
META_WARNING(logger, message, null);
break;
case LOG.GENERAL_WARNING:
GENERAL_WARNING(logger, message, null);
break;
case LOG.FILE_WARNING:
FILE_WARNING(logger, message, null);
break;
case LOG.SYSTEM_WARNING:
SYSTEM_WARNING(logger, message, null);
break;
// Info
case LOG.INFORMATIONAL_PRINT:
default:
INFO_PRINT(logger, message, null);
break;
}
if (outputToFile)
{
WriteToFile(message);
}
}
}