-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathFormLog.cs
114 lines (97 loc) · 3.36 KB
/
FormLog.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
using System;
using System.Diagnostics;
using System.IO;
using System.Windows.Forms;
namespace GitForce
{
/// <summary>
/// Implements the Log form.
/// This form is never closed but is only shown or hidden as requested.
/// </summary>
public partial class FormLog : Form
{
public FormLog()
{
InitializeComponent();
ClassWinGeometry.Restore(this);
// Add our main print function callback delegate
App.PrintLogMessage += Print;
if (App.AppLog != null)
Print("Logging: " + App.AppLog, MessageType.General);
// Prints only in Debug build...
Debug("Debug build.");
}
/// <summary>
/// Form is closing.
/// </summary>
private void FormLogFormClosing(object sender, FormClosingEventArgs e)
{
ClassWinGeometry.Save(this);
// Remove our print function delegate
App.PrintLogMessage -= Print;
}
/// <summary>
/// Form helper function that shows or hides a form
/// </summary>
public void ShowWindow(bool toShow)
{
if (!Visible && toShow)
Show();
if (Visible && !toShow)
Hide();
}
/// <summary>
/// Adds a text string to the end of the text box.
/// For performance reasons, only up to 120 characters of text are added in one call.
/// This is a thread-safe call.
/// </summary>
private void Print(string text, MessageType type)
{
if (textBox.InvokeRequired)
textBox.BeginInvoke((MethodInvoker)(() => Print(text, type)));
else
{
try
{
// Mirror the text to the file log, if enabled
if (App.AppLog != null)
using (StreamWriter sw = File.AppendText(App.AppLog))
sw.WriteLine(DateTime.Now.ToString("HH:mm:ss") + "|" + text);
// Print the text into the textbox
int len = Math.Min(text.Length, 120);
textBox.Text += text.Substring(0, len).Trim() + Environment.NewLine;
// Scroll to the bottom and move carret position
textBox.SelectionStart = textBox.TextLength;
textBox.ScrollToCaret();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
/// <summary>
/// Prints a message only in debug build
/// This is a thread-safe call.
/// </summary>
[Conditional("DEBUG")]
private void Debug(string text)
{
Print(text, MessageType.Debug);
}
#region Context menu handlers: Copy, Select All and Clear
private void CopyToolStripMenuItemClick(object sender, EventArgs e)
{
textBox.Copy();
}
private void SelectAllToolStripMenuItemClick(object sender, EventArgs e)
{
textBox.SelectAll();
}
private void ClearToolStripMenuItemClick(object sender, EventArgs e)
{
textBox.Clear();
}
#endregion
}
}