-
Notifications
You must be signed in to change notification settings - Fork 1
/
Logging.cs
299 lines (244 loc) · 11.8 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
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
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;
namespace IceChat
{
public class Logging : IDisposable
{
//a logging class to handle window logging
private System.IO.FileStream logFile;
private int lastDayWritten;
private ConsoleTab _consoleTab = null;
private IceTabPage _tabPage = null;
public Logging(ConsoleTab consoleTab)
{
this._consoleTab = consoleTab;
CreateConsoleLog();
}
private void CreateConsoleLog()
{
string logFolder = FormMain.Instance.LogsFolder;
string fileExtension = null;
if (FormMain.Instance.IceChatOptions.LogFormat == "Plain Text" || FormMain.Instance.IceChatOptions.LogFormat == "Colored Text")
fileExtension = ".log";
else if (FormMain.Instance.IceChatOptions.LogFormat == "HTML")
fileExtension = ".html";
if (!Directory.Exists(logFolder))
Directory.CreateDirectory(logFolder);
logFolder += Path.DirectorySeparatorChar + _consoleTab.Connection.ServerSetting.ServerName;
//replace any invalid characters in the log folder
foreach (char c in Path.GetInvalidPathChars())
logFolder = logFolder.Replace(c, '_');
string date = "-" + System.DateTime.Now.ToString("yyyy-MM-dd");
try
{
if (!Directory.Exists(logFolder))
Directory.CreateDirectory(logFolder);
}
catch (NotSupportedException)
{
//invalid directory name, try display name instead
logFolder = FormMain.Instance.LogsFolder + Path.DirectorySeparatorChar + _consoleTab.Connection.ServerSetting.DisplayName;
foreach (char c in Path.GetInvalidPathChars())
logFolder = logFolder.Replace(c, '_');
if (!Directory.Exists(logFolder))
Directory.CreateDirectory(logFolder);
}
if (FormMain.Instance.IceChatOptions.SeperateLogs)
logFile = new FileStream(logFolder + Path.DirectorySeparatorChar + "Console" + date + fileExtension, System.IO.FileMode.Append, System.IO.FileAccess.Write, System.IO.FileShare.ReadWrite);
else
logFile = new FileStream(logFolder + Path.DirectorySeparatorChar + "Console" + fileExtension, System.IO.FileMode.Append, System.IO.FileAccess.Write, System.IO.FileShare.ReadWrite);
lastDayWritten = DateTime.Now.Day;
if (logFile.Length == 0)
AddFileHeader();
}
public Logging(IceTabPage tabPage)
{
this._tabPage = tabPage;
CreateStandardLog();
}
private void CreateStandardLog()
{
string logFolder = FormMain.Instance.LogsFolder;
string date = "-" + System.DateTime.Now.ToString("yyyy-MM-dd");
string fileName = _tabPage.TabCaption;
string fileExtension = null;
if (FormMain.Instance.IceChatOptions.LogFormat == "Plain Text" || FormMain.Instance.IceChatOptions.LogFormat == "Colored Text")
fileExtension = ".log";
else if (FormMain.Instance.IceChatOptions.LogFormat == "HTML")
fileExtension = ".html";
//replace any invalid characters in the file name
foreach (char c in Path.GetInvalidFileNameChars())
fileName = fileName.Replace(c, '_');
if (!Directory.Exists(logFolder))
Directory.CreateDirectory(logFolder);
//set the log folder to the server name
if (_tabPage.WindowStyle != IceTabPage.WindowType.Debug)
logFolder += Path.DirectorySeparatorChar + _tabPage.Connection.ServerSetting.ServerName;
//replace any invalid characters in the log folder
foreach (char c in Path.GetInvalidPathChars())
logFolder = logFolder.Replace(c, '_');
if (_tabPage.WindowStyle == IceTabPage.WindowType.Channel)
{
logFolder += Path.DirectorySeparatorChar + "Channel";
if (!Directory.Exists(logFolder))
Directory.CreateDirectory(logFolder);
if (FormMain.Instance.IceChatOptions.SeperateLogs)
logFile = new FileStream(logFolder + Path.DirectorySeparatorChar + fileName + date + fileExtension, System.IO.FileMode.Append, System.IO.FileAccess.Write, System.IO.FileShare.ReadWrite);
else
logFile = new FileStream(logFolder + Path.DirectorySeparatorChar + fileName + fileExtension, System.IO.FileMode.Append, System.IO.FileAccess.Write, System.IO.FileShare.ReadWrite);
}
else if (_tabPage.WindowStyle == IceTabPage.WindowType.Query)
{
logFolder += Path.DirectorySeparatorChar + "Query";
if (!Directory.Exists(logFolder))
Directory.CreateDirectory(logFolder);
if (FormMain.Instance.IceChatOptions.SeperateLogs)
logFile = new FileStream(logFolder + Path.DirectorySeparatorChar + fileName + date + fileExtension, System.IO.FileMode.Append, System.IO.FileAccess.Write, System.IO.FileShare.ReadWrite);
else
logFile = new FileStream(logFolder + Path.DirectorySeparatorChar + fileName + fileExtension, System.IO.FileMode.Append, System.IO.FileAccess.Write, System.IO.FileShare.ReadWrite);
}
else if (_tabPage.WindowStyle == IceTabPage.WindowType.Debug)
{
if (!Directory.Exists(logFolder))
Directory.CreateDirectory(logFolder);
logFile = new FileStream(FormMain.Instance.LogsFolder + Path.DirectorySeparatorChar + "debug.log", System.IO.FileMode.Append, System.IO.FileAccess.Write, System.IO.FileShare.ReadWrite);
}
lastDayWritten = DateTime.Now.Day;
if (logFile.Length == 0)
AddFileHeader();
}
internal void WriteLogFile(string message)
{
if (logFile != null)
{
//check if we need to make a new log file for a new day
if (DateTime.Now.Day != lastDayWritten && FormMain.Instance.IceChatOptions.SeperateLogs)
{
logFile.Close();
logFile.Dispose();
if (_consoleTab != null)
CreateConsoleLog();
else if (_tabPage != null)
CreateStandardLog();
}
if (FormMain.Instance.IceChatOptions.LogFormat == "Plain Text")
{
message = StripCodes(message);
logFile.Write(System.Text.Encoding.Default.GetBytes(message + "\r\n"), 0, message.Length + 2);
}
else if (FormMain.Instance.IceChatOptions.LogFormat == "Colored Text")
{
logFile.Write(System.Text.Encoding.Default.GetBytes(message + "\r\n"), 0, message.Length + 2);
}
else if (FormMain.Instance.IceChatOptions.LogFormat == "HTML")
{
message = ReplaceColorCodes(message);
logFile.Write(System.Text.Encoding.Default.GetBytes(message), 0, message.Length);
}
logFile.Flush();
}
}
internal void AddFileHeader()
{
if (_tabPage != null)
{
WriteLogFile("Session Start: " + DateTime.Now.ToString("ddd MMM dd hh:mm:ss yyyy"));
if (_tabPage.WindowStyle == IceTabPage.WindowType.Channel)
WriteLogFile("Session Ident: " + _tabPage.TabCaption);
}
else if (_consoleTab != null)
{
WriteLogFile("Session Start: " + DateTime.Now.ToString("ddd MMM dd hh:mm:ss yyyy"));
}
}
public void Dispose()
{
if (_tabPage != null)
System.Diagnostics.Debug.WriteLine("Dispose Logging Class TabPage:" + _tabPage.TabCaption);
else if (_consoleTab != null)
System.Diagnostics.Debug.WriteLine("Dispose Logging Class Console:" + _consoleTab.Connection.ServerSetting.ServerName);
if (logFile != null)
{
try
{
logFile.Flush();
logFile.Close();
logFile.Dispose();
}
catch
{
}
}
}
//replace color codes with HTML codes
private string ReplaceColorCodes(string line)
{
//replace certain characters with HTML friendly chars
line = Regex.Replace(line, "&", "&");
line = Regex.Replace(line, "'", "'");
line = Regex.Replace(line, "\"", """);
line = Regex.Replace(line, @"<", "<");
line = Regex.Replace(line, @">", ">");
//line = Regex.Replace(line, "\\", """);
line = "<div style='float:left; display:block;'>" + line;
bool inColor = false;
//parse color codes
Regex parseColors = new Regex("\xFF03[0-9]{4}");
Match m = parseColors.Match(line);
while (m.Success)
{
int color = Convert.ToInt32(m.Value.Substring(1,2));
string c = System.Drawing.ColorTranslator.ToHtml(IrcColor.colors[color]);
if (inColor)
line = line.Replace(m.Value, "</span><span style='color:" + c + ";'>");
else
line = line.Replace(m.Value, "<span style='color:" + c + ";'>");
m = m.NextMatch();
inColor = true;
}
//parse bold codes
Regex parseBold = new Regex(((char)2).ToString());
Match b = parseBold.Match(line);
bool isBold = false;
while (b.Success)
{
//problem here, replaces all the bold chars, not 1 by 1
if (isBold)
line = line.Replace(b.Value, "</b>");
else
line = line.Replace(b.Value, "<b>");
isBold = !isBold;
b = b.NextMatch();
}
//parse underline codes
Regex parseUnderline = new Regex(((char)3).ToString());
Match u = parseUnderline.Match(line);
bool isUnderline = false;
while (u.Success)
{
if (isUnderline)
line = line.Replace(u.Value, "</u>");
else
line = line.Replace(u.Value, "<u>");
isUnderline = !isUnderline;
u = u.NextMatch();
}
if (isBold)
line = line + "</b>";
if (isUnderline)
line = line + "</u>";
if (inColor)
line = line + "</span>";
return line + "</div><br />\r\n";
}
//strip all the colors codes out
private string StripCodes(string line)
{
Regex parseStuff = new Regex("\xFF03[0-9]{4}");
return parseStuff.Replace(line, "");
}
}
}