1 |
william |
17 |
#define ALLOW_LOG_CLEARING // when defined will allow the log to be cleared |
2 |
|
|
using System; |
3 |
|
|
using System.Collections.Generic; |
4 |
|
|
using System.ComponentModel; |
5 |
|
|
using System.Drawing; |
6 |
|
|
using System.Data; |
7 |
|
|
using System.Linq; |
8 |
|
|
using System.Text; |
9 |
|
|
using System.Windows.Forms; |
10 |
|
|
using System.IO; |
11 |
|
|
|
12 |
|
|
namespace RomCheater.Logging |
13 |
|
|
{ |
14 |
|
|
public partial class LogWriter : UserControl |
15 |
|
|
{ |
16 |
william |
18 |
private const string LOG_FILE = "RomCheater.log"; |
17 |
|
|
private static string LOG_PATH = string.Format(@"{0}\{1}", Application.StartupPath, LOG_FILE); |
18 |
|
|
|
19 |
william |
17 |
private delegate string OnGetLogText(); |
20 |
|
|
private delegate void OnSetLogText(string value); |
21 |
|
|
private delegate void GenericVoidDelegate(); |
22 |
|
|
|
23 |
|
|
private OnGetLogText HandleGetLogText = null; |
24 |
|
|
private OnSetLogText HandleSetLogText = null; |
25 |
|
|
private GenericVoidDelegate delegate_EndLogUpdate = null; |
26 |
|
|
|
27 |
|
|
public LogWriter() : this(false) { } |
28 |
|
|
public LogWriter(bool redirectConsole) |
29 |
|
|
{ |
30 |
|
|
InitializeComponent(); |
31 |
|
|
this.Log = new LogStream(this); |
32 |
|
|
HandleGetLogText += new OnGetLogText(GetLogText); |
33 |
|
|
HandleSetLogText += new OnSetLogText(SetLogText); |
34 |
|
|
|
35 |
|
|
delegate_EndLogUpdate += new GenericVoidDelegate(EndLogUpdate); |
36 |
|
|
|
37 |
|
|
this.RedirectConsoleOutput = redirectConsole; |
38 |
|
|
toolTip1.SetToolTip(btnClearLog, "Clears the Log"); |
39 |
|
|
toolTip1.SetToolTip(btnCopyLogToClipboard, "Copies the Log to the Clipboard"); |
40 |
william |
18 |
|
41 |
william |
17 |
ms = new MemoryStream(); |
42 |
|
|
sw = new StreamWriter(ms); |
43 |
|
|
sw.AutoFlush = true; |
44 |
|
|
sr = new StreamReader(ms); |
45 |
william |
18 |
|
46 |
|
|
FileInfo fi = new FileInfo(LOG_PATH); |
47 |
|
|
if (fi.Exists) |
48 |
|
|
fi.Delete(); |
49 |
|
|
|
50 |
william |
17 |
} |
51 |
|
|
private LogStream _Log; |
52 |
william |
18 |
public LogStream Log { get { return _Log; } private set { _Log = value; } } |
53 |
william |
17 |
|
54 |
|
|
|
55 |
|
|
private MemoryStream ms; |
56 |
|
|
private StreamWriter sw; |
57 |
|
|
private StreamReader sr; |
58 |
|
|
|
59 |
|
|
|
60 |
|
|
private bool _RedirectConsoleOutput; |
61 |
|
|
public bool RedirectConsoleOutput |
62 |
|
|
{ |
63 |
|
|
get { return _RedirectConsoleOutput; } |
64 |
|
|
set |
65 |
|
|
{ |
66 |
|
|
_RedirectConsoleOutput = value; |
67 |
william |
18 |
if (value) { Console.SetOut(this.Log); } |
68 |
|
|
else |
69 |
william |
17 |
{ |
70 |
|
|
Stream stream = Console.OpenStandardOutput(0x100); |
71 |
|
|
TextWriter writer = null; |
72 |
|
|
if (stream == Stream.Null) { writer = TextWriter.Synchronized(StreamWriter.Null); } |
73 |
|
|
Encoding encoding = this.Log.Encoding; |
74 |
|
|
StreamWriter writer2 = new StreamWriter(stream, encoding, 0x100); |
75 |
|
|
//writer2.HaveWrittenPreamble = true; |
76 |
|
|
writer2.AutoFlush = true; |
77 |
|
|
writer = TextWriter.Synchronized(writer2); |
78 |
william |
18 |
Console.SetOut(writer); |
79 |
william |
17 |
} |
80 |
|
|
} |
81 |
|
|
} |
82 |
|
|
|
83 |
|
|
private void btnClearLog_Click(object sender, EventArgs e) { this.Clear(true); } |
84 |
|
|
private void btnCopyLogToClipboard_Click(object sender, EventArgs e) { Clipboard.SetText(GetLogText()); } |
85 |
|
|
|
86 |
|
|
|
87 |
|
|
private string PreserveLineEndings(string text) { return text.Replace("\n", System.Environment.NewLine); } |
88 |
|
|
|
89 |
|
|
|
90 |
|
|
private bool BeginUpdate; |
91 |
|
|
public void BeginLogUpdate() |
92 |
|
|
{ |
93 |
|
|
BeginUpdate = true; |
94 |
|
|
} |
95 |
|
|
public void EndLogUpdate() |
96 |
|
|
{ |
97 |
|
|
if (txtLog.InvokeRequired) |
98 |
|
|
{ |
99 |
|
|
this.Invoke(delegate_EndLogUpdate); |
100 |
|
|
} |
101 |
|
|
else |
102 |
|
|
{ |
103 |
|
|
StringReader _sr = new StringReader(GetLogText()); |
104 |
|
|
List<string> lines = new List<string>(); |
105 |
|
|
string line = ""; |
106 |
|
|
while ((line = _sr.ReadLine()) != null) |
107 |
|
|
{ |
108 |
|
|
lines.Add(line); |
109 |
|
|
} |
110 |
|
|
txtLog.Lines = lines.ToArray(); |
111 |
|
|
BeginUpdate = false; |
112 |
|
|
} |
113 |
|
|
} |
114 |
|
|
private string StreamToString() |
115 |
|
|
{ |
116 |
|
|
string value = ""; |
117 |
|
|
sr = new StreamReader(ms); |
118 |
|
|
sr.BaseStream.Seek(0, SeekOrigin.Begin); |
119 |
|
|
value = sr.ReadToEnd(); |
120 |
|
|
return value; |
121 |
|
|
} |
122 |
|
|
public string GetLogText() |
123 |
|
|
{ |
124 |
|
|
//if (txtLog.InvokeRequired) |
125 |
|
|
//{ |
126 |
|
|
// return (string)this.Invoke(HandleGetLogText, new object[] { }); |
127 |
|
|
//} |
128 |
|
|
//else |
129 |
|
|
//{ |
130 |
|
|
return PreserveLineEndings(StreamToString()); |
131 |
|
|
//} |
132 |
|
|
|
133 |
|
|
} |
134 |
|
|
public void SetLogText(string value) |
135 |
|
|
{ |
136 |
|
|
//if (txtLog.InvokeRequired) |
137 |
|
|
//{ |
138 |
|
|
// this.Invoke(HandleSetLogText, new object[] { value }); |
139 |
|
|
//} |
140 |
|
|
//else |
141 |
|
|
//{ |
142 |
|
|
// txtLog.AppendText(value); |
143 |
|
|
//} |
144 |
william |
18 |
FileStream fs = new FileStream(LOG_PATH, FileMode.Append, FileAccess.Write, FileShare.ReadWrite); |
145 |
|
|
StreamWriter writer = new StreamWriter(fs); |
146 |
|
|
writer.AutoFlush = true; |
147 |
william |
19 |
if (value == Log.NewLine) { writer.Write(System.Environment.NewLine); } |
148 |
|
|
else { writer.Write(value); } |
149 |
william |
18 |
writer.Close(); |
150 |
william |
17 |
sw.Write(value); |
151 |
|
|
} |
152 |
|
|
new public string Text |
153 |
|
|
{ |
154 |
|
|
get |
155 |
|
|
{ |
156 |
|
|
throw new InvalidOperationException("Please use GetLogText()"); |
157 |
|
|
} |
158 |
|
|
private set |
159 |
|
|
{ |
160 |
|
|
throw new InvalidOperationException("Please use SetLogText(string value)"); |
161 |
|
|
} |
162 |
|
|
} |
163 |
|
|
|
164 |
william |
18 |
public void Clear() |
165 |
william |
17 |
{ |
166 |
|
|
Clear(false); |
167 |
|
|
} |
168 |
|
|
private void Clear(bool force) |
169 |
|
|
{ |
170 |
|
|
bool allow_log_clearing = false; |
171 |
|
|
#if ALLOW_LOG_CLEARING |
172 |
|
|
allow_log_clearing = true; |
173 |
|
|
#endif |
174 |
|
|
if (force || allow_log_clearing) |
175 |
|
|
{ |
176 |
william |
18 |
|
177 |
william |
17 |
txtLog.Clear(); |
178 |
|
|
ms = new MemoryStream(); |
179 |
|
|
sw = new StreamWriter(ms); |
180 |
|
|
sw.AutoFlush = true; |
181 |
|
|
sr = new StreamReader(ms); |
182 |
|
|
} |
183 |
|
|
} |
184 |
|
|
#region sub-classes |
185 |
|
|
public class LogStream : TextWriter |
186 |
william |
18 |
{ |
187 |
william |
17 |
public LogStream() : this(null) { } |
188 |
|
|
public LogStream(LogWriter text) : base() { _text_writer = text; this.NewLine = "\n"; } |
189 |
|
|
private LogWriter _text_writer; |
190 |
|
|
private void _write(string message) { if (_text_writer == null) return; _text_writer.SetLogText(string.Format("{0}", message)); } |
191 |
|
|
#region Overriden Methods |
192 |
|
|
public override Encoding Encoding { get { return Encoding.UTF8; } } |
193 |
|
|
public override void Write(char value) { base.Write(value); _write(value.ToString()); if (!_text_writer.BeginUpdate) _text_writer.EndLogUpdate(); } |
194 |
william |
18 |
#endregion |
195 |
william |
17 |
} |
196 |
william |
18 |
#endregion |
197 |
william |
17 |
} |
198 |
|
|
} |