-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDebugLogForm.cs
More file actions
80 lines (72 loc) · 2.54 KB
/
DebugLogForm.cs
File metadata and controls
80 lines (72 loc) · 2.54 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
using System;
using System.IO;
using System.Windows.Forms;
namespace OHK
{
public partial class DebugLogForm : Form
{
private static readonly DebugLogForm instance = new DebugLogForm();
private static SaveFileDialog LogFileDialog = new SaveFileDialog();
private delegate void SafeCallDelegate(string text);
static DebugLogForm ()
{
}
public DebugLogForm()
{
InitializeComponent();
}
public static DebugLogForm Instance
{
get
{
return instance;
}
}
private void DebugLogForm_FormClosing(object sender, FormClosingEventArgs e)
{
if (e.CloseReason == CloseReason.UserClosing)
{
e.Cancel = true;
Hide();
}
}
public void Log(string text)
{
if (this.logConsole.InvokeRequired)
{
var d = new SafeCallDelegate(Log);
logConsole.Invoke(d, new object[] { text });
}
else
{
logConsole.AppendText($"{DateTime.Now.ToString("HH:mm:ss")}: {text}\n");
logConsole.ScrollToCaret();
}
}
private void copyButton_Click(object sender, EventArgs e)
{
Clipboard.SetText(logConsole.Text);
}
private void saveButton_Click(object sender, EventArgs e)
{
LogFileDialog.CreatePrompt = false;
LogFileDialog.OverwritePrompt = true;
LogFileDialog.FileName = Constant.appName+"_Log";
LogFileDialog.DefaultExt = "txt";
LogFileDialog.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*";
LogFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
DialogResult result = LogFileDialog.ShowDialog();
Stream fileStream;
if (result == DialogResult.OK)
{
// Open the file, copy the contents of memoryStream to fileStream,
// and close fileStream. Set the memoryStream.Position value to 0
// to copy the entire stream.
fileStream = LogFileDialog.OpenFile();
logConsole.SaveFile(fileStream, RichTextBoxStreamType.PlainText);
fileStream.Close();
Log(string.Format("Debug log written as {0}",LogFileDialog.FileName));
}
}
}
}