-
Notifications
You must be signed in to change notification settings - Fork 0
/
Logging.cs
238 lines (215 loc) · 8.76 KB
/
Logging.cs
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
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Reflection;
using System.Threading;
namespace Flux.Utilities
{
/// <summary>
/// This class handles all logging done for Fluxbot. From simple output, to writing to files, to logging exceptions
/// and user messages. It is event based, and thread safe.
/// Created by redskull & Apoc MMOwned.com
/// </summary>
/// <example>Logging.WriteDebug("Oh noes! We failed again!");</example>
public static class Logging
{
#region Delegates
/// <summary>
///
/// </summary>
public delegate void DebugDelegate(string message, Color col);
/// <summary>
///
/// </summary>
public delegate void WriteDelegate(string message, Color col);
#endregion
private static readonly Queue<Color> DebugColorQueue = new Queue<Color>();
private static readonly Queue<string> DebugQueue = new Queue<string>();
private static readonly Queue<string> LogQueue = new Queue<string>();
private static readonly Thread QueueThread;
private static readonly Queue<Color> WriteColorQueue = new Queue<Color>();
private static readonly Queue<string> WriteQueue = new Queue<string>();
static Logging()
{
LogOnWrite = true;
QueueThread = new Thread(_WriteQueue) { IsBackground = true };
QueueThread.Start();
}
/// <summary>
/// Gets or sets a value indicating whether every Write action should also be logged.
/// </summary>
/// <value><c>true</c> if [log on write]; otherwise, <c>false</c>.</value>
public static bool LogOnWrite { get; set; }
private static string TimeStamp { get { return string.Format("[{0}] ", DateTime.Now.ToLongTimeString()); } }
private static string LogDate { get { return DateTime.Now.ToShortDateString().Replace("/", "-"); } }
public static string ApplicationPath { get { return Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); } }
/// <summary>
/// Occurs when [on write].
/// </summary>
public static event WriteDelegate OnWrite;
/// <summary>
/// Occurs when [on debug].
/// </summary>
public static event DebugDelegate OnDebug;
private static void InvokeOnWrite(string message, Color col)
{
if (OnWrite != null)
{
OnWrite(message, col);
}
}
private static void InvokeOnDebug(string message, Color col)
{
if (OnDebug != null)
{
OnDebug(message, col);
}
}
private static void _WriteQueue()
{
if (!Directory.Exists(string.Format("{0}\\Logs", ApplicationPath)))
{
Directory.CreateDirectory(string.Format("{0}\\Logs", ApplicationPath));
}
while (true)
{
try
{
using (
TextWriter tw =
new StreamWriter(string.Format("{0}\\Logs\\{1} Log.txt", ApplicationPath, LogDate), true))
{
while (LogQueue.Count != 0)
{
tw.WriteLine(LogQueue.Dequeue());
}
string[] tmp = WriteQueue.ToArray();
Color[] ctmp = WriteColorQueue.ToArray();
for (int i = 0; i < tmp.Length; i++)
{
InvokeOnWrite(tmp[i], ctmp[i]);
WriteQueue.Dequeue();
WriteColorQueue.Dequeue();
}
tmp = DebugQueue.ToArray();
ctmp = DebugColorQueue.ToArray();
for (int i = 0; i < tmp.Length; i++)
{
InvokeOnDebug(tmp[i], ctmp[i]);
DebugQueue.Dequeue();
DebugColorQueue.Dequeue();
}
}
Thread.Sleep(500);
}
catch
{
break;
}
}
}
/// <summary>
/// Writes the specified message to the message queue.
/// </summary>
/// <param name="format">The format.</param>
/// <param name="args">The args.</param>
/// 1/14/2009 7:33 PM
public static void Write(string format, params object[] args)
{
Write(Color.Black, format, args);
}
/// <summary>
/// Writes the specified message to the message queue.
/// </summary>
/// <param name="color">The color to write the message in.</param>
/// <param name="format">The format.</param>
/// <param name="args">The args.</param>
/// 1/14/2009 7:33 PM
public static void Write(Color color, string format, params object[] args)
{
string s = TimeStamp + string.Format(format, args);
WriteQueue.Enqueue(s);
WriteColorQueue.Enqueue(color);
if (LogOnWrite)
{
LogQueue.Enqueue(s);
}
}
/// <summary>
/// Writes the specified debug message to the message queue. [Default Color: Red]
/// Debug messages are not shown to the end user by default. (They can be turned on via settings)
/// </summary>
/// <param name="format">The format.</param>
/// <param name="args">The args.</param>
/// 1/14/2009 7:40 PM
public static void WriteDebug(string format, params object[] args)
{
WriteDebug(Color.Red, format, args);
}
/// <summary>
/// Writes the debug message in the specific color.
/// Debug messages are not shown to the end user by default. (They can be turned on via settings)
/// </summary>
/// <param name="color">The color to write in.</param>
/// <param name="format"></param>
/// <param name="args"></param>
public static void WriteDebug(Color color, string format, params object[] args)
{
string s = TimeStamp + string.Format(format, args);
DebugQueue.Enqueue(s);
DebugColorQueue.Enqueue(color);
if (LogOnWrite)
{
LogQueue.Enqueue(s);
}
}
/// <summary>
/// Writes the specified exception to the message queue. [Default Color: Red]
/// Debug messages are not shown to the end user by default. (They can be turned on via settings)
/// </summary>
/// <param name="ex">The exception that will be logged.</param>
public static void WriteException(Exception ex)
{
WriteException(Color.Blue, ex);
}
/// <summary>
/// Writes the specified exception to the message queue.
/// Debug messages are not shown to the end user by default. (They can be turned on via settings)
/// </summary>
/// <param name="color">The color to write the message in.</param>
/// <param name="ex">The exception that will be logged.</param>
public static void WriteException(Color color, Exception ex)
{
string s = string.Format("{0}{1} ({5}) - {4} - From: {2}\n{3}", TimeStamp, ex.Message, ex.Source, ex.StackTrace,
ex.InnerException, ex.GetType());
DebugQueue.Enqueue(s);
DebugColorQueue.Enqueue(color);
if (LogOnWrite)
{
LogQueue.Enqueue(s);
}
}
/// <summary>
/// Logs the specified message to the logging queue. This will not fire any
/// message events. It will go straight to the log file without question.
/// </summary>
/// <param name="format">The format param of string.Format</param>
/// <param name="args">The args param of string.Format</param>
public static void LogMessage(string format, params object[] args)
{
string s = TimeStamp + string.Format(format, args);
LogQueue.Enqueue(s);
}
/// <summary>
/// Logs the specified exception into the log queue.
/// </summary>
/// <param name="ex">The exception to be logged.</param>
public static void LogException(Exception ex)
{
string s = string.Format("{0}{1} - {4} - From: {2}\n{3}", TimeStamp, ex.Message, ex.Source, ex.StackTrace,
ex.InnerException);
LogQueue.Enqueue(s);
}
}
}