-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathLogViewer.cs
More file actions
149 lines (127 loc) · 5.31 KB
/
LogViewer.cs
File metadata and controls
149 lines (127 loc) · 5.31 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
using DayZServerMonitorCore;
using System;
using System.Windows.Forms;
namespace DayZServerMonitor
{
public partial class LogViewer : Form
{
private readonly Settings settings;
public LogViewer(Settings settings)
{
InitializeComponent();
this.settings = settings;
this.settings.SettingChanged += Settings_SettingChanged;
FormClosing += (source, args) => { args.Cancel = true; Hide(); };
logsDataGridView.ColumnCount = 3;
logsDataGridView.RowHeadersVisible = false;
logsDataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
logsDataGridView.MultiSelect = false;
logsDataGridView.AllowUserToResizeRows = false;
logsDataGridView.Columns[0].Name = "Timestamp";
logsDataGridView.Columns[0].SortMode = DataGridViewColumnSortMode.NotSortable;
logsDataGridView.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
logsDataGridView.Columns[1].Name = "Level";
logsDataGridView.Columns[1].SortMode = DataGridViewColumnSortMode.NotSortable;
logsDataGridView.Columns[1].AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
logsDataGridView.Columns[2].Name = "Message";
logsDataGridView.Columns[2].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
logsDataGridView.Columns[2].SortMode = DataGridViewColumnSortMode.NotSortable;
logsDataGridView.SelectionChanged += LogSelectionChanged;
logEntry.Navigate("about:blank");
}
private void Settings_SettingChanged(object sender, SettingChangedArgs args)
{
if (args.SettingName == nameof(settings.MaxLogViewerEntries))
{
TrimEntries();
}
}
public void Add(string level, string message)
{
if (InvokeRequired)
{
Invoke(new MethodInvoker(delegate { Add(level, message); }));
return;
}
bool selectionAtEnd = IsScrolledToEnd();
logsDataGridView.Rows.Add(new string[] { DateTime.Now.ToString(), level, message });
TrimEntries();
if (selectionAtEnd)
{
ScrollToEnd();
}
}
private void TrimEntries()
{
while (logsDataGridView.Rows.Count > settings.MaxLogViewerEntries)
{
logsDataGridView.Rows.RemoveAt(0);
}
}
private bool IsScrolledToEnd()
{
int firstDisplayed = logsDataGridView.FirstDisplayedScrollingRowIndex;
int displayed = logsDataGridView.DisplayedRowCount(true);
int lastVisible = (firstDisplayed + displayed) - 1;
int lastIndex = logsDataGridView.RowCount - 1;
return lastVisible == lastIndex;
}
private void ScrollToEnd()
{
logsDataGridView.FirstDisplayedScrollingRowIndex = Math.Max(
0,
logsDataGridView.RowCount - logsDataGridView.DisplayedRowCount(true));
}
private void LogSelectionChanged(object source, EventArgs args)
{
var cells = logsDataGridView.SelectedRows[0].Cells;
HtmlDocument doc = logEntry.Document.OpenNew(true);
doc.Write("<html><head>");
doc.Write("<style>body {font-family: sans-serif; font-size: 12;}; dt {font-weight: bold;}; dd {font-family: monospace;}</style>");
doc.Write("</head><body></body></html>");
var dl = doc.CreateElement("dl");
doc.Body.AppendChild(dl);
var timestampDt = doc.CreateElement("dt");
timestampDt.InnerText = "Timestamp";
dl.AppendChild(timestampDt);
var timestampDd = doc.CreateElement("dd");
timestampDd.InnerText = cells[0].Value.ToString();
dl.AppendChild(timestampDd);
var levelDt = doc.CreateElement("dt");
levelDt.InnerText = "Level";
dl.AppendChild(levelDt);
var levelDd = doc.CreateElement("dd");
levelDd.InnerText = cells[1].Value.ToString();
dl.AppendChild(levelDd);
var messageDt = doc.CreateElement("dt");
messageDt.InnerText = "Message";
dl.AppendChild(messageDt);
var messageDd = doc.CreateElement("dd");
messageDd.InnerText = cells[2].Value.ToString();
dl.AppendChild(messageDd);
}
private void CopyToolStripMenuItem_Click(object sender, EventArgs e)
{
logEntry.Document.ExecCommand("copy", false, null);
}
private void SelectAllToolStripMenuItem_Click(object sender, EventArgs e)
{
logEntry.Document.ExecCommand("selectAll", false, null);
}
private void LogEntry_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
if (!e.Control)
{
return;
}
if (e.KeyCode == Keys.A)
{
logEntry.Document.ExecCommand("selectAll", false, null);
}
else if (e.KeyCode == Keys.C)
{
logEntry.Document.ExecCommand("copy", false, null);
}
}
}
}