/
ConsoleLog.cs
485 lines (411 loc) · 20.7 KB
/
ConsoleLog.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
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
using System;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Windows.Forms;
namespace SideSoftware.Log
{
public partial class ConsoleLog: UserControl
{
private string _headerDivider;
private readonly RichTextBox _textBox;
public ConsoleLog()
{
InitializeComponent();
_textBox = textBox;
ConsoleBackColor = Color.FromArgb(51, 51, 51);
ConsoleFont = new Font("Consolas", 12F, FontStyle.Regular, GraphicsUnit.Point, 0);
StandardForeColor = Color.FromArgb(225, 213, 112);
StandardBackColor = Color.FromArgb(51, 51, 51);
InfoForeColor = Color.FromArgb(99, 211, 234);
InfoBackColor = Color.FromArgb(51, 51, 51);
WarningForeColor = Color.FromArgb(51, 51, 51);
WarningBackColor = Color.FromArgb(255, 252, 0);
ErrorForeColor = Color.White;
ErrorBackColor = Color.Tomato;
SuccessForeColor = Color.FromArgb(163, 233, 45);
SuccessBackColor = Color.FromArgb(51, 51, 51);
StandoutForeColor = Color.FromArgb(250, 1, 194);
StandoutBackColor = Color.FromArgb(51, 51, 51);
SubtleForeColor = Color.FromArgb(156, 156, 156);
SubtleBackColor = Color.FromArgb(51, 51, 51);
EventForeColor = Color.FromArgb(78, 201, 176);
EventBackColor = Color.FromArgb(51, 51, 51);
EventWarningForeColor = Color.White;
EventWarningBackColor = Color.FromArgb(78, 201, 176);
}
#region Public Properties
[Description("Header divider characters"), Category("Console Log")]
public string HeaderDivider
{
get
{
return string.IsNullOrEmpty(_headerDivider) ? "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++" : _headerDivider;
}
set
{
if (!string.IsNullOrEmpty(value))
_headerDivider = value;
}
}
[Description("Total lines in the console"), Category("Console Log")]
public long TotalLines
{
get { return textBox.Lines.LongCount(); }
}
[Description("Console back color"), Category("Console Log")]
public Color ConsoleBackColor
{
get { return _textBox.BackColor; }
set { _textBox.BackColor = value; }
}
[Description("Console back color"), Category("Console Log")]
public Font ConsoleFont
{
get { return _textBox.Font; }
set { _textBox.Font = value; }
}
[Description("Default fore color"), Category("Console Log")]
public Color StandardForeColor { get; set; }
[Description("Default Back color"), Category("Console Log")]
public Color StandardBackColor { get; set; }
[Description("Info fore color"), Category("Console Log")]
public Color InfoForeColor { get; set; }
[Description("Info Back color"), Category("Console Log")]
public Color InfoBackColor { get; set; }
[Description("Warning fore color"), Category("Console Log")]
public Color WarningForeColor { get; set; }
[Description("Warning back color"), Category("Console Log")]
public Color WarningBackColor { get; set; }
[Description("Error fore color"), Category("Console Log")]
public Color ErrorForeColor { get; set; }
[Description("Error back color"), Category("Console Log")]
public Color ErrorBackColor { get; set; }
[Description("Success fore color"), Category("Console Log")]
public Color SuccessForeColor { get; set; }
[Description("Success back color"), Category("Console Log")]
public Color SuccessBackColor { get; set; }
[Description("Standout fore color"), Category("Console Log")]
public Color StandoutForeColor { get; set; }
[Description("Standout back color"), Category("Console Log")]
public Color StandoutBackColor { get; set; }
[Description("Subtle fore color"), Category("Console Log")]
public Color SubtleForeColor { get; set; }
[Description("Subtle back color"), Category("Console Log")]
public Color SubtleBackColor { get; set; }
[Description("Event fore color"), Category("Console Log")]
public Color EventForeColor { get; set; }
[Description("Event back color"), Category("Console Log")]
public Color EventBackColor { get; set; }
[Description("Event Warning fore color"), Category("Console Log")]
public Color EventWarningForeColor { get; set; }
[Description("Event Warning back color"), Category("Console Log")]
public Color EventWarningBackColor { get; set; }
#endregion
#region Public Functions
public void Log(LogStatus x, bool verbose = false)
{
switch (x.LogType)
{
case LogTypeEnum.Default:
Log(x.Message);
break;
case LogTypeEnum.Subtle:
if (verbose) LogSubtle(x.Message);
break;
case LogTypeEnum.Info:
LogInfo(x.Message);
break;
case LogTypeEnum.Success:
if (verbose) LogSuccess(x.Message);
break;
case LogTypeEnum.Warning:
if (verbose) LogWarning(x.Message);
break;
case LogTypeEnum.Standout:
LogStandout(x.Message);
break;
case LogTypeEnum.Error:
LogError(x.Message);
if (x.Exception != null) LogError(x.Exception.ToString());
break;
case LogTypeEnum.Debug:
if (verbose) LogSubtle(x.Message);
break;
default:
throw new ArgumentOutOfRangeException();
}
}
/// <summary>
/// Refreshes the log
/// </summary>
public override void Refresh()
{
_textBox.Refresh();
}
/// <summary>
/// Saves the contents of the log to a text file
/// </summary>
/// <param name="path">The path where the file should be saved</param>
public void SaveFile(string path)
{
_textBox.SaveFile(path);
}
/// <summary>
/// Saves the contents of the log to a text file
/// </summary>
/// <param name="path">The path where the file should be saved</param>
/// <param name="fileType">The stream type</param>
public void SaveFile(string path, RichTextBoxStreamType fileType)
{
_textBox.SaveFile(path, fileType);
}
/// <summary>
/// Saves the contents of the log to a text file
/// </summary>
/// <param name="data">The stream of the data to save</param>
/// <param name="fileType">The stream type</param>
public void SaveFile(Stream data, RichTextBoxStreamType fileType)
{
_textBox.SaveFile(data, fileType);
}
/// <summary>
/// Clears the contents of the log
/// </summary>
public void ClearContents()
{
textBox.Clear();
}
public void LogHeader(string message)
{
Log(HeaderDivider, true);
Log(message);
Log(HeaderDivider, false, true);
}
/// <summary>
/// Helper method to log an info message
/// </summary>
/// <param name="message">The message or value to log</param>
public void LogInfo(object message)
{
LogInfo(message.ToString(), null);
}
/// <summary>
/// Helper method to log an info message by replacing the format item in a specified string with the string representation of a corresponding object in a specified array.
/// </summary>
/// <param name="message">A composite format string.</param>
/// <param name="args">An object array that contains zero or more objects to format.</param>
/// <exception cref="T:System.ArgumentNullException"><paramref name="message"/> or <paramref name="args"/> is null. </exception><exception cref="T:System.FormatException"><paramref name="message"/> is invalid.-or- The index of a format item is less than zero, or greater than or equal to the length of the <paramref name="args"/> array. </exception><filterpriority>1</filterpriority>
public void LogInfo(string message, params object[] args)
{
// Build the new message
message = BuildMessage(message, args);
Log(message, InfoForeColor, InfoBackColor);
}
/// <summary>
/// Helper method to log a warning message
/// </summary>
/// <param name="message">The message or value to log</param>
public void LogWarning(object message)
{
LogWarning(message.ToString(), null);
}
/// <summary>
/// Helper method to log a warning message by replacing the format item in a specified string with the string representation of a corresponding object in a specified array.
/// </summary>
/// <param name="message">A composite format string.</param>
/// <param name="args">An object array that contains zero or more objects to format.</param>
/// <exception cref="T:System.ArgumentNullException"><paramref name="message"/> or <paramref name="args"/> is null. </exception><exception cref="T:System.FormatException"><paramref name="message"/> is invalid.-or- The index of a format item is less than zero, or greater than or equal to the length of the <paramref name="args"/> array. </exception><filterpriority>1</filterpriority>
public void LogWarning(string message, params object[] args)
{
// Build the new message
message = BuildMessage(message, args);
Log(message, WarningForeColor, WarningBackColor);
}
/// <summary>
/// Helper method to log an error message
/// </summary>
/// <param name="message">The message or value to log</param>
public void LogError(object message)
{
LogError(message.ToString(), null);
}
/// <summary>
/// Helper method to log an error message by replacing the format item in a specified string with the string representation of a corresponding object in a specified array.
/// </summary>
/// <param name="message">A composite format string.</param>
/// <param name="args">An object array that contains zero or more objects to format.</param>
/// <exception cref="T:System.ArgumentNullException"><paramref name="message"/> or <paramref name="args"/> is null. </exception><exception cref="T:System.FormatException"><paramref name="message"/> is invalid.-or- The index of a format item is less than zero, or greater than or equal to the length of the <paramref name="args"/> array. </exception><filterpriority>1</filterpriority>
public void LogError(string message, params object[] args)
{
// Build the new message
message = BuildMessage(message, args);
Log(message, ErrorForeColor, ErrorBackColor);
}
/// <summary>
/// Helper method to log a success message
/// </summary>
/// <param name="message">The message or value to log</param>
public void LogSuccess(object message)
{
LogSuccess(message.ToString(), null);
}
/// <summary>
/// Helper method to log a success message by replacing the format item in a specified string with the string representation of a corresponding object in a specified array.
/// </summary>
/// <param name="message">A composite format string.</param>
/// <param name="args">An object array that contains zero or more objects to format.</param>
/// <exception cref="T:System.ArgumentNullException"><paramref name="message"/> or <paramref name="args"/> is null. </exception><exception cref="T:System.FormatException"><paramref name="message"/> is invalid.-or- The index of a format item is less than zero, or greater than or equal to the length of the <paramref name="args"/> array. </exception><filterpriority>1</filterpriority>
public void LogSuccess(string message, params object[] args)
{
// Build the new message
message = BuildMessage(message, args);
Log(message, SuccessForeColor, SuccessBackColor);
}
/// <summary>
/// Helper method to log a standout message
/// </summary>
/// <param name="message">The message or value to log</param>
public void LogStandout(object message)
{
LogStandout(message.ToString(), null);
}
/// <summary>
/// Helper method to log a standout message by replacing the format item in a specified string with the string representation of a corresponding object in a specified array.
/// </summary>
/// <param name="message">A composite format string.</param>
/// <param name="args">An object array that contains zero or more objects to format.</param>
/// <exception cref="T:System.ArgumentNullException"><paramref name="message"/> or <paramref name="args"/> is null. </exception><exception cref="T:System.FormatException"><paramref name="message"/> is invalid.-or- The index of a format item is less than zero, or greater than or equal to the length of the <paramref name="args"/> array. </exception><filterpriority>1</filterpriority>
public void LogStandout(string message, params object[] args)
{
// Build the new message
message = BuildMessage(message, args);
Log(message, StandoutForeColor, StandoutBackColor);
}
/// <summary>
/// Helper method to log an subtle message
/// </summary>
/// <param name="message">The message or value to log</param>
public void LogSubtle(object message)
{
LogSubtle(message.ToString(), null);
}
/// <summary>
/// Helper method to log an subtle message by replacing the format item in a specified string with the string representation of a corresponding object in a specified array.
/// </summary>
/// <param name="message">A composite format string.</param>
/// <param name="args">An object array that contains zero or more objects to format.</param>
/// <exception cref="T:System.ArgumentNullException"><paramref name="message"/> or <paramref name="args"/> is null. </exception><exception cref="T:System.FormatException"><paramref name="message"/> is invalid.-or- The index of a format item is less than zero, or greater than or equal to the length of the <paramref name="args"/> array. </exception><filterpriority>1</filterpriority>
public void LogSubtle(string message, params object[] args)
{
// Build the new message
message = BuildMessage(message, args);
// Log the message
Log(message, SubtleForeColor, SubtleBackColor);
}
/// <summary>
/// Helper method to log an event message
/// </summary>
/// <param name="message">The message or value to log</param>
public void LogEvent(object message)
{
LogEvent(message.ToString(), null);
}
/// <summary>
/// Helper method to log an event message by replacing the format item in a specified string with the string representation of a corresponding object in a specified array.
/// </summary>
/// <param name="message">A composite format string.</param>
/// <param name="args">An object array that contains zero or more objects to format.</param>
/// <exception cref="T:System.ArgumentNullException"><paramref name="message"/> or <paramref name="args"/> is null. </exception><exception cref="T:System.FormatException"><paramref name="message"/> is invalid.-or- The index of a format item is less than zero, or greater than or equal to the length of the <paramref name="args"/> array. </exception><filterpriority>1</filterpriority>
public void LogEvent(string message, params object[] args)
{
// Build the new message
message = BuildMessage(message, args);
Log(message, EventForeColor, EventBackColor);
}
/// <summary>
/// Helper method to log an event warning
/// </summary>
/// <param name="message">The message or value to log</param>
public void LogEventWarning(object message)
{
LogEventWarning(message.ToString(), null);
}
/// <summary>
/// Helper method to log an event warning by replacing the format item in a specified string with the string representation of a corresponding object in a specified array.
/// </summary>
/// <param name="message">A composite format string.</param>
/// <param name="args">An object array that contains zero or more objects to format.</param>
/// <exception cref="T:System.ArgumentNullException"><paramref name="message"/> or <paramref name="args"/> is null. </exception><exception cref="T:System.FormatException"><paramref name="message"/> is invalid.-or- The index of a format item is less than zero, or greater than or equal to the length of the <paramref name="args"/> array. </exception><filterpriority>1</filterpriority>
public void LogEventWarning(string message, params object[] args)
{
// Build the new message
message = BuildMessage(message, args);
// Log the message
Log(message, EventWarningForeColor, EventWarningBackColor);
}
/// <summary>
/// Main function for logging a message
/// </summary>
/// <param name="message"></param>
/// <param name="spaceBefore"></param>
/// <param name="spaceAfter"></param>
public void Log(string message, bool spaceBefore = false, bool spaceAfter = false)
{
if (spaceBefore) Log("", StandardForeColor, StandardBackColor);
Log(message, StandardForeColor, StandardBackColor);
if (spaceAfter) Log("", StandardForeColor, StandardBackColor);
}
public void Log(object message, Color foreColor, Color selectionColor)
{
// Check if we need to invoke
if (_textBox.InvokeRequired)
{
_textBox.Invoke(new MethodInvoker(() => Log(message, foreColor, selectionColor)));
return;
}
// Store the current length
var start = _textBox.TextLength;
// Append the text
_textBox.AppendText(message + Environment.NewLine);
// Get the new length
var end = _textBox.TextLength;
// Textbox may transform chars, so (end-start) != text.Length
_textBox.Select(start, end - start);
{
_textBox.SelectionColor = foreColor;
_textBox.SelectionBackColor = selectionColor;
}
_textBox.SelectionLength = 0; // clear
// Scroll to the end
_textBox.ScrollToCaret();
}
#endregion
/// <summary>
/// Helper function to build message with params argument
/// </summary>
private static string BuildMessage(string message, params object[] args)
{
string formattedMessage;
try
{
// Build the message with or without args
formattedMessage = args == null ? message : string.Format(message, args);
}
catch
{
return BuildMessageWithDate(message);
}
return BuildMessageWithDate(formattedMessage);
}
/// <summary>
/// Helper function to build message with date prefix
/// </summary>
private static string BuildMessageWithDate(string message)
{
return string.Format("{0} - {1}", DateTime.Now.ToLongTimeString(), message);
}
}
}