forked from marcosvf132/TibiaAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogger.cs
More file actions
242 lines (213 loc) · 6.64 KB
/
Logger.cs
File metadata and controls
242 lines (213 loc) · 6.64 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
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Threading;
namespace OXGaming.TibiaAPI.Utilities
{
public class Logger : IDisposable
{
public enum LogLevel
{
Debug,
Info,
Warning,
Error,
Disabled
}
public enum LogOutput
{
Console,
File
}
private readonly Dictionary<LogLevel, string> _logLevelMap = new Dictionary<LogLevel, string>()
{
{ LogLevel.Debug, "DEBUG" },
{ LogLevel.Info, "INFO" },
{ LogLevel.Warning, "WARNING" },
{ LogLevel.Error, "ERROR" },
{ LogLevel.Disabled, "DISABLED" }
};
private readonly ConcurrentQueue<(LogLevel, string)> _logQueue = new ConcurrentQueue<(LogLevel, string)>();
private readonly object _logLock = new object();
private StreamWriter _outputFile = null;
private Thread _loggingThread;
private LogOutput _output = LogOutput.Console;
private bool _isLogging = false;
public LogLevel Level { get; set; } = LogLevel.Disabled;
public LogOutput Output
{
get
{
return _output;
}
set
{
lock (_logLock)
{
if (value == LogOutput.File && _outputFile == null)
{
var utcNow = DateTime.UtcNow;
var filename = $"{utcNow.Day}_{utcNow.Month}_{utcNow.Year}__{utcNow.Hour}_{utcNow.Minute}_{utcNow.Second}.log";
_outputFile = new StreamWriter(File.OpenWrite(filename));
}
_output = value;
}
}
}
public static LogLevel ConvertToLogLevel(string level)
{
if (level.Equals("debug", StringComparison.CurrentCultureIgnoreCase))
{
return LogLevel.Debug;
}
else if (level.Equals("info", StringComparison.CurrentCultureIgnoreCase))
{
return LogLevel.Info;
}
else if (level.Equals("warning", StringComparison.CurrentCultureIgnoreCase))
{
return LogLevel.Warning;
}
else if (level.Equals("error", StringComparison.CurrentCultureIgnoreCase))
{
return LogLevel.Error;
}
else if (level.Equals("disabled", StringComparison.CurrentCultureIgnoreCase))
{
return LogLevel.Disabled;
}
else
{
throw new ArgumentException($"[Logger.ConvertToLogLevel] Invalid input: {level}");
}
}
public static LogOutput ConvertToLogOutput(string output)
{
if (output.Equals("output", StringComparison.CurrentCultureIgnoreCase))
{
return LogOutput.Console;
}
else if (output.Equals("file", StringComparison.CurrentCultureIgnoreCase))
{
return LogOutput.File;
}
else
{
throw new ArgumentException($"[Logger.ConvertToLogOutput] Invalid input: {output}");
}
}
private void Log(LogLevel level, string text)
{
if (Level == LogLevel.Disabled || level < Level)
{
return;
}
_logQueue.Enqueue((level, text));
if (!_isLogging)
{
try
{
_isLogging = true;
_loggingThread = new Thread(new ThreadStart(LogQueue));
_loggingThread.Start();
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
}
private void LogQueue()
{
try
{
while (_logQueue.TryDequeue(out var data))
{
var (level, text) = data;
if (level == LogLevel.Disabled || string.IsNullOrEmpty(text))
{
break;
}
text = $"{_logLevelMap[level]} {text}";
lock (_logLock)
{
if (level == LogLevel.Error)
{
Console.WriteLine(text);
if (_output == LogOutput.File && _outputFile != null)
{
_outputFile.WriteLine(text);
}
}
else if (_output == LogOutput.Console)
{
Console.WriteLine(text);
}
else if (_output == LogOutput.File)
{
_outputFile.WriteLine(text);
}
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
finally
{
_isLogging = false;
}
}
public void Debug(string text)
{
Log(LogLevel.Debug, text);
}
public void Info(string text)
{
Log(LogLevel.Info, text);
}
public void Warning(string text)
{
Log(LogLevel.Warning, text);
}
public void Error(string text)
{
Log(LogLevel.Error, text);
}
#region IDisposable Support
private bool disposedValue = false;
protected virtual void Dispose(bool disposing)
{
if (!disposedValue)
{
if (disposing)
{
if (_loggingThread != null)
{
_loggingThread.Join();
}
if (_outputFile != null)
{
_outputFile.Close();
}
}
disposedValue = true;
}
}
~Logger()
{
Dispose(false);
}
/// <summary>
/// Releases all the managed resources used by the <see cref="Logger"/>.
/// </summary>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
#endregion
}
}