-
Notifications
You must be signed in to change notification settings - Fork 5
/
Logger.cs
152 lines (131 loc) · 4.91 KB
/
Logger.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
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
namespace Logger
{
// Source: https://stackoverflow.com/a/55540909
/// <summary>
/// A circular buffer style logging class which stores N items for display in a Rich Text Box.
/// </summary>
public class Logger
{
private readonly Queue<LogEntry> _log;
private uint _entryNumber;
private readonly uint _maxEntries;
private readonly object _logLock = new object();
private readonly Color _defaultColor = Color.White;
private class LogEntry
{
public uint EntryId;
public DateTime EntryTimeStamp;
public string EntryText;
public Color EntryColor;
}
private struct ColorTableItem
{
public uint Index;
public string RichColor;
}
/// <summary>
/// Create an instance of the Logger class which stores <paramref name="maximumEntries"/> log entries.
/// </summary>
public Logger(uint maximumEntries)
{
_log = new Queue<LogEntry>();
_maxEntries = maximumEntries;
}
/// <summary>
/// Retrieve the contents of the log as rich text, suitable for populating a <see cref="System.Windows.Forms.RichTextBox.Rtf"/> property.
/// </summary>
/// <param name="includeEntryNumbers">Option to prepend line numbers to each entry.</param>
public string GetLogAsRichText(bool includeEntryNumbers)
{
lock (_logLock)
{
var sb = new StringBuilder();
var uniqueColors = BuildRichTextColorTable();
sb.AppendLine($@"{{\rtf1{{\colortbl;{ string.Join("", uniqueColors.Select(d => d.Value.RichColor)) }}}");
foreach (var entry in _log)
{
if (includeEntryNumbers)
sb.Append($"\\cf1 { entry.EntryId }. ");
sb.Append($"\\cf1 { entry.EntryTimeStamp.ToShortDateString() } { entry.EntryTimeStamp.ToShortTimeString() }: ");
var richColor = $"\\cf{ uniqueColors[entry.EntryColor].Index + 1 }";
sb.Append($"{ richColor } { entry.EntryText }\\par").AppendLine();
}
return sb.ToString();
}
}
/// <summary>
/// Adds <paramref name="text"/> as a log entry.
/// </summary>
public void AddToLog(string text)
{
AddToLog(text, _defaultColor);
}
/// <summary>
/// Adds <paramref name="text"/> as a log entry, and specifies a color to display it in.
/// </summary>
public void AddToLog(string text, Color entryColor)
{
if (entryColor == null)
{
entryColor = _defaultColor;
}
lock (_logLock)
{
if (_entryNumber >= uint.MaxValue)
_entryNumber = 0;
_entryNumber++;
var logEntry = new LogEntry { EntryId = _entryNumber, EntryTimeStamp = DateTime.Now, EntryText = text, EntryColor = entryColor };
_log.Enqueue(logEntry);
while (_log.Count > _maxEntries)
_log.Dequeue();
}
}
/// <summary>
/// Clears the entire log.
/// </summary>
public void Clear()
{
lock (_logLock)
{
_log.Clear();
}
}
private Dictionary<Color, ColorTableItem> BuildRichTextColorTable()
{
var uniqueColors = new Dictionary<Color, ColorTableItem>();
var index = 0u;
uniqueColors.Add(_defaultColor, new ColorTableItem() { Index = index++, RichColor = ColorToRichColorString(_defaultColor) });
foreach (var c in _log.Select(l => l.EntryColor).Distinct().Where(c => c != _defaultColor))
uniqueColors.Add(c, new ColorTableItem() { Index = index++, RichColor = ColorToRichColorString(c) });
return uniqueColors;
}
private string ColorToRichColorString(Color c)
{
return $"\\red{c.R}\\green{c.G}\\blue{c.B};";
}
}
public class ScrollingRichTextBox : System.Windows.Forms.RichTextBox
{
[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern IntPtr SendMessage(
IntPtr hWnd,
uint Msg,
IntPtr wParam,
IntPtr LParam);
private const int _WM_VSCROLL = 277;
private const int _SB_BOTTOM = 7;
/// <summary>
/// Scrolls to the bottom of the RichTextBox.
/// </summary>
public void ScrollToBottom()
{
SendMessage(Handle, _WM_VSCROLL, new IntPtr(_SB_BOTTOM), new IntPtr(0));
}
}
}