Repository navigation
Expand file tree
/
Copy pathILogManager.cs
More file actions
345 lines (299 loc) · 15.1 KB
/
Copy pathILogManager.cs
File metadata and controls
345 lines (299 loc) · 15.1 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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
using System;
using System.Runtime.CompilerServices;
using System.Text;
namespace SS.Core.ComponentInterfaces
{
/// <summary>
/// Severity levels of logs.
/// </summary>
public enum LogLevel
{
/// <summary>
/// Unimportant info. Useful for investigation during development.
/// </summary>
Drivel = 1,
/// <summary>
/// Informative info.
/// </summary>
Info,
/// <summary>
/// Info about a event that is potentially malicious, usually about the client-side misbehaving.
/// </summary>
Malicious,
/// <summary>
/// Info about an abnormal or unexpected event, of low severity.
/// </summary>
Warn,
/// <summary>
/// Info about a failure.
/// </summary>
Error,
}
/// <summary>
/// Log codes for <see cref="LogLevel"/>.
/// </summary>
public enum LogCode
{
/// <summary><see cref="LogLevel.Drivel"/></summary>
D = 1,
/// <summary><see cref="LogLevel.Info"/></summary>
I,
/// <summary><see cref="LogLevel.Malicious"/></summary>
M,
/// <summary><see cref="LogLevel.Warn"/></summary>
W,
/// <summary><see cref="LogLevel.Error"/></summary>
E,
}
public static class LogLevelExtensions
{
/// <summary>
/// Converts a <see cref="LogLevel"/> to a character code used in the default logging format.
/// </summary>
/// <param name="level"></param>
/// <returns></returns>
public static char ToChar(this LogLevel level) => level switch
{
LogLevel.Drivel => 'D',
LogLevel.Info => 'I',
LogLevel.Malicious => 'M',
LogLevel.Warn => 'W',
LogLevel.Error => 'E',
_ => '?'
};
}
/// <summary>
/// Information for a single log entry.
/// </summary>
public readonly struct LogEntry
{
/// <summary>
/// The severity of the log.
/// </summary>
public readonly required LogLevel Level { get; init; }
/// <summary>
/// The name of the module that the log originate from.
/// </summary>
public readonly string? Module { get; init; }
/// <summary>
/// The arena the log is related to.
/// </summary>
public readonly Arena? Arena { get; init; }
/// <summary>
/// The name of the player the log is related to.
/// </summary>
public readonly string? PlayerName { get; init; }
/// <summary>
/// The ID of the player the log is related to.
/// </summary>
public readonly int? PlayerId { get; init; }
/// <summary>
/// Text of the log, in the format:
/// <code>LogCode <module> {arena} [player] message</code>
/// LogCode is a single character representing the <see cref="Level"/>.
/// Module, arena, and player are optional.
/// </summary>
public readonly required StringBuilder LogText { get; init; }
}
/// <summary>
/// Interface for a logging service.
/// </summary>
/// <remarks>
/// Use the LogM, LogA, and LogP methods to automatically format a log entry.
/// </remarks>
public interface ILogManager : IComponentInterface
{
#region Log
/// <summary>
/// Writes a log entry.
/// </summary>
/// <param name="level">The severity of the log to write.</param>
/// <param name="handler">
/// The message to write, in the format:
/// <code>"<module> {arena} [player] did something"</code>
/// Module, arena, and player are optional.
/// </param>
void Log(LogLevel level, [InterpolatedStringHandlerArgument("")] ref StringBuilderBackedInterpolatedStringHandler handler);
/// <summary>
/// Writes a log entry.
/// </summary>
/// <param name="level">The severity of the log to write.</param>
/// <param name="provider">An object that supplies culture-specific formatting information.</param>
/// <param name="handler">
/// The message to write, in the format:
/// <code>"<module> {arena} [player] did something"</code>
/// Module, arena, and player are optional.
/// </param>
void Log(LogLevel level, IFormatProvider provider, [InterpolatedStringHandlerArgument("", nameof(provider))] ref StringBuilderBackedInterpolatedStringHandler handler);
/// <summary>
/// Writes a log entry.
/// </summary>
/// <param name="level">The severity of the log to write.</param>
/// <param name="message">
/// The message to write, in the format:
/// <code>"<module> {arena} [player] did something"</code>
/// Module, arena, and player are optional.
/// </param>
void Log(LogLevel level, ReadOnlySpan<char> message);
/// <summary>
/// Writes a log entry.
/// </summary>
/// <param name="level">The severity of the log to write.</param>
/// <param name="message">
/// The message to write, in the format:
/// <code>"<module> {arena} [player] did something"</code>
/// Module, arena, and player are optional.
/// </param>
void Log(LogLevel level, string message);
/// <summary>
/// Writes a log entry.
/// </summary>
/// <param name="level">The severity of the log to write.</param>
/// <param name="message">
/// The message to write, in the format:
/// <code>"<module> {arena} [player] did something"</code>
/// Module, arena, and player are optional.
/// </param>
void Log(LogLevel level, StringBuilder message);
#endregion
#region Log Module
/// <summary>
/// Writes a log entry for a specified <paramref name="module"/>.
/// </summary>
/// <param name="level">The severity of the log to write.</param>
/// <param name="module">The name of the module writing the log.</param>
/// <param name="handler">The message to write.</param>
void LogM(LogLevel level, string module, [InterpolatedStringHandlerArgument("")] ref StringBuilderBackedInterpolatedStringHandler handler);
/// <summary>
/// Writes a log entry for a specified <paramref name="module"/>.
/// </summary>
/// <param name="level">The severity of the log to write.</param>
/// <param name="module">The name of the module writing the log.</param>
/// <param name="provider">An object that supplies culture-specific formatting information.</param>
/// <param name="handler">The message to write.</param>
void LogM(LogLevel level, string module, IFormatProvider provider, [InterpolatedStringHandlerArgument("", nameof(provider))] ref StringBuilderBackedInterpolatedStringHandler handler);
/// <summary>
/// Writes a log entry for a specified <paramref name="module"/>.
/// </summary>
/// <param name="level">The severity of the log to write.</param>
/// <param name="module">The name of the module writing the log.</param>
/// <param name="message">The message to write.</param>
void LogM(LogLevel level, string module, ReadOnlySpan<char> message);
/// <summary>
/// Writes a log entry for a specified <paramref name="module"/>.
/// </summary>
/// <param name="level">The severity of the log to write.</param>
/// <param name="module">The name of the module writing the log.</param>
/// <param name="message">The message to write.</param>
void LogM(LogLevel level, string module, string message);
/// <summary>
/// Writes a log entry for a specified <paramref name="module"/>.
/// </summary>
/// <param name="level">The severity of the log to write.</param>
/// <param name="module">The name of the module writing the log.</param>
/// <param name="message">The message to write.</param>
void LogM(LogLevel level, string module, StringBuilder message);
#endregion
#region Log Arena
/// <summary>
/// Writes a log entry for a specified <paramref name="module"/> and <paramref name="arena"/>.
/// Adds a line to the server log, specialized for arena-specific messages.
/// </summary>
/// <param name="level">The severity of the log to write.</param>
/// <param name="module">The name of the module writing the log.</param>
/// <param name="arena">The arena that the log entry is associated with.</param>
/// <param name="handler">The message to write.</param>
void LogA(LogLevel level, string module, Arena arena, [InterpolatedStringHandlerArgument("")] ref StringBuilderBackedInterpolatedStringHandler handler);
/// <summary>
/// Writes a log entry for a specified <paramref name="module"/> and <paramref name="arena"/>.
/// </summary>
/// <param name="level">The severity of the log to write.</param>
/// <param name="module">The name of the module writing the log.</param>
/// <param name="arena">The arena that the log entry is associated with.</param>
/// <param name="provider">An object that supplies culture-specific formatting information.</param>
/// <param name="handler">The message to write.</param>
void LogA(LogLevel level, string module, Arena arena, IFormatProvider provider, [InterpolatedStringHandlerArgument("", nameof(provider))] ref StringBuilderBackedInterpolatedStringHandler handler);
/// <summary>
/// Writes a log entry for a specified <paramref name="module"/> and <paramref name="arena"/>.
/// </summary>
/// <param name="level">The severity of the log to write.</param>
/// <param name="module">The name of the module writing the log.</param>
/// <param name="arena">The arena that the log entry is associated with.</param>
/// <param name="message">The message to write.</param>
void LogA(LogLevel level, string module, Arena arena, ReadOnlySpan<char> message);
/// <summary>
/// Writes a log entry for a specified <paramref name="module"/> and <paramref name="arena"/>.
/// </summary>
/// <param name="level">The severity of the log to write.</param>
/// <param name="module">The name of the module writing the log.</param>
/// <param name="arena">The arena that the log entry is associated with.</param>
/// <param name="message">The message to write.</param>
void LogA(LogLevel level, string module, Arena arena, string message);
/// <summary>
/// Writes a log entry for a specified <paramref name="module"/> and <paramref name="arena"/>.
/// </summary>
/// <param name="level">The severity of the log to write.</param>
/// <param name="module">The name of the module writing the log.</param>
/// <param name="arena">The arena that the log entry is associated with.</param>
/// <param name="message">The message to write.</param>
void LogA(LogLevel level, string module, Arena arena, StringBuilder message);
#endregion
#region Log Player
/// <summary>
/// Writes a log entry for a specified <paramref name="module"/> and <paramref name="player"/>.
/// </summary>
/// <param name="level">The severity of the log to write.</param>
/// <param name="module">The name of the module writing the log.</param>
/// <param name="player">The player that the log entry is associated with.</param>
/// <param name="handler">The message to write.</param>
void LogP(LogLevel level, string module, Player player, [InterpolatedStringHandlerArgument("")] ref StringBuilderBackedInterpolatedStringHandler handler);
/// <summary>
/// Writes a log entry for a specified <paramref name="module"/> and <paramref name="player"/>.
/// </summary>
/// <param name="level">The severity of the log to write.</param>
/// <param name="module">The name of the module writing the log.</param>
/// <param name="player">The player that the log entry is associated with.</param>
/// <param name="provider">An object that supplies culture-specific formatting information.</param>
/// <param name="handler">The message to write.</param>
void LogP(LogLevel level, string module, Player player, IFormatProvider provider, [InterpolatedStringHandlerArgument("", nameof(provider))] ref StringBuilderBackedInterpolatedStringHandler handler);
/// <summary>
/// Writes a log entry for a specified <paramref name="module"/> and <paramref name="player"/>.
/// </summary>
/// <param name="level">The severity of the log to write.</param>
/// <param name="module">The name of the module writing the log.</param>
/// <param name="player">The player that the log entry is associated with.</param>
/// <param name="message">The message to write.</param>
void LogP(LogLevel level, string module, Player player, ReadOnlySpan<char> message);
/// <summary>
/// Writes a log entry for a specified <paramref name="module"/> and <paramref name="player"/>.
/// </summary>
/// <param name="level">The severity of the log to write.</param>
/// <param name="module">The name of the module writing the log.</param>
/// <param name="player">The player that the log entry is associated with.</param>
/// <param name="message">The message to write.</param>
void LogP(LogLevel level, string module, Player player, string message);
/// <summary>
/// Writes a log entry for a specified <paramref name="module"/> and <paramref name="player"/>.
/// </summary>
/// <param name="level">The severity of the log to write.</param>
/// <param name="module">The name of the module writing the log.</param>
/// <param name="player">The player that the log entry is associated with.</param>
/// <param name="message">The message to write.</param>
void LogP(LogLevel level, string module, Player player, StringBuilder message);
#endregion
/// <summary>
/// Checks whether a log entry should be logged by a specific handler.
/// </summary>
/// <remarks>
/// Log handlers can optionally call this function to support filtering of the log entries that they're processing.
/// The filters are be defined by an administrator in global.conf.
/// <para>
/// The <paramref name="logModuleName"/>s match the names used in ASSS, to stay compatible with ASSS conf files.
/// </para>
/// </remarks>
/// <param name="logEntry">The log entry to check.</param>
/// <param name="logModuleName">The module name of the log handler (e.g., log_file).</param>
/// <returns><see langword="true"/> if the message should be logged, otherwise <see langword="false"/>.</returns>
bool FilterLog(ref readonly LogEntry logEntry, string logModuleName);
}
}