forked from ConfuzzedCat/TerrariaInjector
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogger.cs
More file actions
236 lines (199 loc) · 7.17 KB
/
Logger.cs
File metadata and controls
236 lines (199 loc) · 7.17 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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
namespace Core
{
public enum LogLevel
{
Debug,
Information,
Warning,
Error,
}
public sealed class Logger
{
public static bool LogToConsole = false;
public static bool LogErrorsToConsole = true;
public static bool HasErrors = false;
public static bool HasWarnings = false;
public static int BatchInterval = 1000;
public readonly string LogId;
public static FileInfo TargetLogFile { get; private set; }
public static DirectoryInfo TargetDirectory { get { return TargetLogFile?.Directory; } }
public static bool Listening { get; private set; }
private static readonly Timer Timer = new Timer(Tick);
private static readonly StringBuilder LogQueue = new StringBuilder();
public static EventHandler<LogMessageInfo> LogMessageAdded;
public Logger(Type t) : this(t.Name)
{
}
public Logger(string logId)
{
LogId = logId;
}
public static void Start(FileInfo targetLogFile = null, bool overwrite = true, string logDirectory = null)
{
if (Listening)
{
return;
}
if (targetLogFile != null)
{
TargetLogFile = targetLogFile;
}
else
{
var assembly = new FileInfo(Assembly.GetExecutingAssembly().Location);
string logDir = string.IsNullOrEmpty(logDirectory) ? assembly.DirectoryName : logDirectory;
TargetLogFile = new FileInfo(Path.Combine(logDir, Path.GetFileNameWithoutExtension(assembly.Name) + ".log"));
}
if (overwrite && File.Exists(TargetLogFile.FullName))
{
File.Delete(TargetLogFile.FullName);
}
Listening = true;
VerifyTargetDirectory();
Timer.Change(BatchInterval, Timeout.Infinite); // A one-off tick event that is reset every time.
Log("Log started.");
}
public static void Shutdown()
{
if (!Listening)
{
return;
}
Log("Log stopped.");
Listening = false;
Timer.Dispose();
Tick(null); // Flush.
}
private static void VerifyTargetDirectory()
{
if (TargetDirectory == null)
{
throw new DirectoryNotFoundException("Target logging directory not found.");
}
TargetDirectory.Refresh();
if (!TargetDirectory.Exists)
{
TargetDirectory.Create();
}
}
private static void Tick(object state)
{
try
{
string logMessage;
lock (LogQueue)
{
logMessage = LogQueue.ToString();
LogQueue.Length = 0;
}
if (string.IsNullOrEmpty(logMessage))
{
return;
}
VerifyTargetDirectory(); // File may be deleted after initialization.
File.AppendAllText(TargetLogFile.FullName, logMessage);
}
finally
{
if (Listening)
{
Timer.Change(BatchInterval, Timeout.Infinite); // Reset timer for next tick.
}
}
}
[Conditional("DEBUG")]
public void Debug(string message)
{
Log($"<{Assembly.GetCallingAssembly().GetName().Name}> " + message, LogLevel.Debug);
}
public void Info(string message)
{
Log($"<{Assembly.GetCallingAssembly().GetName().Name}> " + message, LogLevel.Information);
}
public void Warning(string message)
{
HasWarnings = true;
Log($"<{Assembly.GetCallingAssembly().GetName().Name}> " + message, LogLevel.Warning);
}
public void Error(string message, Exception exception = null)
{
HasErrors = true;
var list = new List<Exception>();
while (exception != null)
{
list.Add(exception);
exception = exception.InnerException;
}
//if (list.Count > 0)
// exception = list[list.Count - 1];
Log($"<{Assembly.GetCallingAssembly().GetName().Name}> " + message, LogLevel.Error, list.LastOrDefault());
/*
do
{
Log($"<{Assembly.GetCallingAssembly().GetName().Name}> " + message, LogLevel.Error, exception);
exception = exception?.InnerException;
} while (exception != null);
*/
}
public void Log(string message, LogLevel level = LogLevel.Information, Exception exception = null)
{
Log(message, LogId, level, exception);
}
public static void Log(string message, string logger = null, LogLevel level = LogLevel.Information, Exception exception = null)
{
if (!Listening)
{
throw new Exception("Logging has not been started.");
}
if (exception != null)
{
message += $"\r\n{exception.Message}\r\n{exception.StackTrace}";
}
var info = new LogMessageInfo(level, logger, message);
var msg = info.ToString();
lock (LogQueue)
{
LogQueue.AppendLine(msg);
if (LogToConsole || (LogErrorsToConsole && level >= LogLevel.Warning))
Console.WriteLine(msg);
}
var evnt = LogMessageAdded;
evnt?.Invoke(null, info); // Block caller. evnt?.Invoke(this, info);
}
}
public sealed class LogMessageInfo : EventArgs
{
public readonly DateTime Timestamp;
public readonly string ThreadId;
public readonly LogLevel LogLevel;
public readonly string LogId;
public readonly string Message;
public LogMessageInfo(LogLevel level, string logId, string message)
{
Timestamp = DateTime.UtcNow;
var thread = Thread.CurrentThread;
ThreadId = string.IsNullOrEmpty(thread.Name) ? thread.ManagedThreadId.ToString() : thread.Name;
LogLevel = level;
LogId = logId;
Message = message;
}
public bool IsInformation => LogLevel == LogLevel.Information;
public bool IsDebug => LogLevel == LogLevel.Debug;
public bool IsWarning => LogLevel == LogLevel.Warning;
public bool IsError => LogLevel == LogLevel.Error;
public override string ToString()
{
var logId = string.IsNullOrEmpty(LogId) ? "" : $"[{LogId}]";
var level = IsInformation ? "" : $"[{LogLevel}]";
return $"[{Timestamp:yyyy/MM/dd HH:mm:ss.fff}][{ThreadId}]{logId}{level} {Message}";
}
}
}