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 |
william |
23 |
using System.Reflection; |
12 |
william |
301 |
using System.Security; |
13 |
william |
17 |
|
14 |
|
|
namespace RomCheater.Logging |
15 |
|
|
{ |
16 |
|
|
public partial class LogWriter : UserControl |
17 |
|
|
{ |
18 |
william |
61 |
private static string LOG_PATH { get { return string.Format(@"{0}\{1}", typeof(LogWriter).Assembly.Location.Replace("RomCheater.Logging.dll", ""), LoggingConstants.AppLogFile); } } |
19 |
william |
18 |
|
20 |
william |
17 |
private delegate string OnGetLogText(); |
21 |
|
|
private delegate void OnSetLogText(string value); |
22 |
|
|
private delegate void GenericVoidDelegate(); |
23 |
|
|
|
24 |
william |
301 |
//private OnGetLogText HandleGetLogText = null; |
25 |
william |
17 |
private OnSetLogText HandleSetLogText = null; |
26 |
|
|
private GenericVoidDelegate delegate_EndLogUpdate = null; |
27 |
|
|
|
28 |
|
|
public LogWriter() : this(false) { } |
29 |
|
|
public LogWriter(bool redirectConsole) |
30 |
|
|
{ |
31 |
|
|
InitializeComponent(); |
32 |
|
|
this.Log = new LogStream(this); |
33 |
william |
301 |
//HandleGetLogText += new OnGetLogText(GetLogText); |
34 |
william |
17 |
HandleSetLogText += new OnSetLogText(SetLogText); |
35 |
|
|
|
36 |
william |
301 |
//delegate_EndLogUpdate += new GenericVoidDelegate(EndLogUpdate); |
37 |
william |
17 |
|
38 |
|
|
this.RedirectConsoleOutput = redirectConsole; |
39 |
|
|
toolTip1.SetToolTip(btnClearLog, "Clears the Log"); |
40 |
|
|
toolTip1.SetToolTip(btnCopyLogToClipboard, "Copies the Log to the Clipboard"); |
41 |
william |
18 |
|
42 |
william |
301 |
//ms = new MemoryStream(); |
43 |
|
|
//sw = new StreamWriter(ms); |
44 |
|
|
//sw.AutoFlush = true; |
45 |
|
|
//sr = new StreamReader(ms); |
46 |
william |
17 |
} |
47 |
|
|
private LogStream _Log; |
48 |
william |
18 |
public LogStream Log { get { return _Log; } private set { _Log = value; } } |
49 |
william |
17 |
|
50 |
|
|
|
51 |
william |
301 |
//private MemoryStream ms; |
52 |
|
|
//private StreamWriter sw; |
53 |
|
|
//private StreamReader sr; |
54 |
william |
105 |
private bool _AutoScroll; |
55 |
|
|
new public bool AutoScroll { get { return _AutoScroll; } set { _AutoScroll = value; autoscroll_timer.Enabled = value; } } |
56 |
william |
17 |
|
57 |
|
|
private bool _RedirectConsoleOutput; |
58 |
|
|
public bool RedirectConsoleOutput |
59 |
|
|
{ |
60 |
|
|
get { return _RedirectConsoleOutput; } |
61 |
|
|
set |
62 |
|
|
{ |
63 |
|
|
_RedirectConsoleOutput = value; |
64 |
william |
18 |
if (value) { Console.SetOut(this.Log); } |
65 |
|
|
else |
66 |
william |
17 |
{ |
67 |
|
|
Stream stream = Console.OpenStandardOutput(0x100); |
68 |
|
|
TextWriter writer = null; |
69 |
|
|
if (stream == Stream.Null) { writer = TextWriter.Synchronized(StreamWriter.Null); } |
70 |
|
|
Encoding encoding = this.Log.Encoding; |
71 |
|
|
StreamWriter writer2 = new StreamWriter(stream, encoding, 0x100); |
72 |
|
|
//writer2.HaveWrittenPreamble = true; |
73 |
|
|
writer2.AutoFlush = true; |
74 |
|
|
writer = TextWriter.Synchronized(writer2); |
75 |
william |
18 |
Console.SetOut(writer); |
76 |
william |
17 |
} |
77 |
|
|
} |
78 |
|
|
} |
79 |
|
|
|
80 |
|
|
private void btnClearLog_Click(object sender, EventArgs e) { this.Clear(true); } |
81 |
william |
301 |
private void btnCopyLogToClipboard_Click(object sender, EventArgs e) { Clipboard.SetText(txtLog.Text); } |
82 |
william |
17 |
|
83 |
|
|
|
84 |
|
|
private string PreserveLineEndings(string text) { return text.Replace("\n", System.Environment.NewLine); } |
85 |
|
|
|
86 |
|
|
|
87 |
william |
301 |
//private bool BeginUpdate; |
88 |
|
|
//public void BeginLogUpdate() |
89 |
|
|
//{ |
90 |
|
|
// BeginUpdate = true; |
91 |
|
|
//} |
92 |
|
|
//public void EndLogUpdate() |
93 |
|
|
//{ |
94 |
|
|
// if (txtLog.InvokeRequired) |
95 |
|
|
// { |
96 |
|
|
// this.Invoke(delegate_EndLogUpdate); |
97 |
|
|
// } |
98 |
|
|
// else |
99 |
|
|
// { |
100 |
|
|
// StringReader _sr = new StringReader(GetLogText()); |
101 |
|
|
// List<string> lines = new List<string>(); |
102 |
|
|
// string line = ""; |
103 |
|
|
// while ((line = _sr.ReadLine()) != null) |
104 |
|
|
// { |
105 |
|
|
// lines.Add(line); |
106 |
|
|
// } |
107 |
|
|
// txtLog.Lines = lines.ToArray(); |
108 |
|
|
// BeginUpdate = false; |
109 |
|
|
// } |
110 |
|
|
//} |
111 |
|
|
//private string StreamToString() |
112 |
|
|
//{ |
113 |
|
|
// try |
114 |
|
|
// { |
115 |
|
|
// string value = ""; |
116 |
|
|
// sr = new StreamReader(ms); |
117 |
|
|
// sr.BaseStream.Seek(0, SeekOrigin.Begin); |
118 |
|
|
// value = sr.ReadToEnd(); |
119 |
|
|
// return value; |
120 |
|
|
// } |
121 |
|
|
// catch (Exception ex) |
122 |
|
|
// { |
123 |
|
|
// return ""; |
124 |
|
|
// } |
125 |
|
|
//} |
126 |
|
|
//public string GetLogText() |
127 |
|
|
//{ |
128 |
|
|
// if (txtLog.InvokeRequired) |
129 |
|
|
// { |
130 |
|
|
// return (string)this.Invoke(HandleGetLogText, new object[] { }); |
131 |
|
|
// } |
132 |
|
|
// else |
133 |
|
|
// { |
134 |
|
|
// string text = PreserveLineEndings(StreamToString()); |
135 |
|
|
// StringReader _sr = new StringReader(GetLogText()); |
136 |
|
|
// List<string> lines = new List<string>(); |
137 |
|
|
// string line = ""; |
138 |
|
|
// while ((line = _sr.ReadLine()) != null) |
139 |
|
|
// { |
140 |
|
|
// lines.Add(line); |
141 |
|
|
// } |
142 |
|
|
// txtLog.Lines = lines.ToArray(); |
143 |
|
|
// return text; |
144 |
|
|
// } |
145 |
|
|
//} |
146 |
william |
17 |
public void SetLogText(string value) |
147 |
|
|
{ |
148 |
william |
18 |
FileStream fs = new FileStream(LOG_PATH, FileMode.Append, FileAccess.Write, FileShare.ReadWrite); |
149 |
|
|
StreamWriter writer = new StreamWriter(fs); |
150 |
|
|
writer.AutoFlush = true; |
151 |
william |
19 |
if (value == Log.NewLine) { writer.Write(System.Environment.NewLine); } |
152 |
|
|
else { writer.Write(value); } |
153 |
william |
18 |
writer.Close(); |
154 |
william |
106 |
//string text = value.Replace(System.Environment.NewLine,""); |
155 |
william |
301 |
//string text = value.Replace(System.Environment.NewLine, Log.NewLine); |
156 |
|
|
////if (text.StartsWith(Log.NewLine)) |
157 |
|
|
////{ |
158 |
|
|
//// text = text.Remove(0, Log.NewLine.Length); |
159 |
|
|
////} |
160 |
|
|
//sw.Write(text); |
161 |
|
|
//Application.DoEvents(); |
162 |
|
|
txtLog.AppendText(value); |
163 |
william |
17 |
} |
164 |
william |
105 |
|
165 |
william |
17 |
new public string Text |
166 |
|
|
{ |
167 |
|
|
get |
168 |
|
|
{ |
169 |
|
|
throw new InvalidOperationException("Please use GetLogText()"); |
170 |
|
|
} |
171 |
|
|
private set |
172 |
|
|
{ |
173 |
|
|
throw new InvalidOperationException("Please use SetLogText(string value)"); |
174 |
|
|
} |
175 |
|
|
} |
176 |
william |
23 |
public void CreateNewLog(bool delete) |
177 |
william |
22 |
{ |
178 |
william |
23 |
if (delete) |
179 |
|
|
{ |
180 |
|
|
FileInfo fi = new FileInfo(LOG_PATH); |
181 |
|
|
if (fi.Exists) |
182 |
|
|
fi.Delete(); |
183 |
|
|
} |
184 |
william |
105 |
Log.WriteLine("{0} ({2} v{3} {4}) created on {1}", LoggingConstants.AppLogFile, DateTime.Now.ToString(), LoggingConstants.AppName, LoggingConstants.AppVersion, LoggingConstants.AppBuild); |
185 |
william |
22 |
} |
186 |
william |
18 |
public void Clear() |
187 |
william |
17 |
{ |
188 |
|
|
Clear(false); |
189 |
|
|
} |
190 |
|
|
private void Clear(bool force) |
191 |
|
|
{ |
192 |
|
|
bool allow_log_clearing = false; |
193 |
|
|
#if ALLOW_LOG_CLEARING |
194 |
|
|
allow_log_clearing = true; |
195 |
|
|
#endif |
196 |
|
|
if (force || allow_log_clearing) |
197 |
|
|
{ |
198 |
william |
18 |
|
199 |
william |
17 |
txtLog.Clear(); |
200 |
william |
301 |
//ms = new MemoryStream(); |
201 |
|
|
//sw = new StreamWriter(ms); |
202 |
|
|
//sw.AutoFlush = true; |
203 |
|
|
//sr = new StreamReader(ms); |
204 |
william |
17 |
} |
205 |
|
|
} |
206 |
|
|
#region sub-classes |
207 |
|
|
public class LogStream : TextWriter |
208 |
william |
18 |
{ |
209 |
william |
105 |
private StringBuilder strMessage = new StringBuilder(); |
210 |
william |
17 |
public LogStream() : this(null) { } |
211 |
|
|
public LogStream(LogWriter text) : base() { _text_writer = text; this.NewLine = "\n"; } |
212 |
|
|
private LogWriter _text_writer; |
213 |
william |
301 |
//private void _write(string message) |
214 |
|
|
//{ |
215 |
|
|
// if (_text_writer == null) return; |
216 |
william |
297 |
|
217 |
william |
301 |
// if (message.EndsWith("\n")) |
218 |
|
|
// { |
219 |
|
|
// //_text_writer.SetLogText(string.Format("{0}", strMessage.ToString())); |
220 |
|
|
// while (_text_writer.logupdater.IsBusy) |
221 |
|
|
// { |
222 |
|
|
// Application.DoEvents(); |
223 |
|
|
// } |
224 |
|
|
// _text_writer.logupdater.RunWorkerAsync((string.Format("{0}", strMessage.ToString()))); |
225 |
|
|
// strMessage = new StringBuilder(); |
226 |
|
|
// } |
227 |
|
|
// strMessage.Append(message); |
228 |
|
|
//} |
229 |
william |
17 |
#region Overriden Methods |
230 |
|
|
public override Encoding Encoding { get { return Encoding.UTF8; } } |
231 |
william |
301 |
|
232 |
|
|
public override void Write(bool value) { _text_writer.SetLogText(value.ToString()); } |
233 |
|
|
public override void Write(char value) { _text_writer.SetLogText(value.ToString()); } |
234 |
|
|
public override void Write(char[] buffer) { _text_writer.SetLogText(new string(buffer)); } |
235 |
|
|
public override void Write(decimal value) { _text_writer.SetLogText(value.ToString()); } |
236 |
|
|
public override void Write(double value) { _text_writer.SetLogText(value.ToString()); } |
237 |
|
|
public override void Write(float value) { _text_writer.SetLogText(value.ToString()); } |
238 |
|
|
public override void Write(int value) { _text_writer.SetLogText(value.ToString()); } |
239 |
|
|
public override void Write(long value) { _text_writer.SetLogText(value.ToString()); } |
240 |
|
|
public override void Write(object value) { _text_writer.SetLogText(value.ToString()); } |
241 |
|
|
public override void Write(string value) { _text_writer.SetLogText(value.ToString()); } |
242 |
|
|
public override void Write(uint value) { _text_writer.SetLogText(value.ToString()); } |
243 |
|
|
public override void Write(ulong value) { _text_writer.SetLogText(value.ToString()); } |
244 |
|
|
public override void Write(string format, object arg0) { _text_writer.SetLogText(string.Format(format, arg0)); } |
245 |
|
|
public override void Write(string format, params object[] arg) { _text_writer.SetLogText(string.Format(format, arg)); } |
246 |
|
|
public override void Write(char[] buffer, int index, int count) |
247 |
|
|
{ |
248 |
|
|
byte[] t = new byte[count]; |
249 |
|
|
MemoryStream ms = new MemoryStream(); |
250 |
|
|
StreamWriter sw = new StreamWriter(ms); |
251 |
|
|
sw.AutoFlush = true; |
252 |
|
|
sw.Write(buffer, index, count); |
253 |
|
|
sw.Close(); |
254 |
|
|
_text_writer.SetLogText(Encoding.GetString(ms.ToArray())); |
255 |
|
|
} |
256 |
|
|
public override void Write(string format, object arg0, object arg1) { _text_writer.SetLogText(string.Format(format, arg0, arg1)); } |
257 |
|
|
public override void Write(string format, object arg0, object arg1, object arg2) { _text_writer.SetLogText(string.Format(format, arg0, arg1, arg2)); } |
258 |
|
|
public override void WriteLine() { _text_writer.SetLogText(System.Environment.NewLine); } |
259 |
|
|
public override void WriteLine(bool value) { _text_writer.SetLogText(string.Format("{0}{1}", value.ToString(), System.Environment.NewLine)); } |
260 |
|
|
public override void WriteLine(char value) { _text_writer.SetLogText(string.Format("{0}{1}", value.ToString(), System.Environment.NewLine)); } |
261 |
|
|
public override void WriteLine(char[] buffer) { _text_writer.SetLogText(string.Format("{0}{1}", new string(buffer), System.Environment.NewLine)); } |
262 |
|
|
public override void WriteLine(decimal value) { _text_writer.SetLogText(string.Format("{0}{1}", value.ToString(), System.Environment.NewLine)); } |
263 |
|
|
public override void WriteLine(double value) { _text_writer.SetLogText(string.Format("{0}{1}", value.ToString(), System.Environment.NewLine)); } |
264 |
|
|
public override void WriteLine(float value) { _text_writer.SetLogText(string.Format("{0}{1}", value.ToString(), System.Environment.NewLine)); } |
265 |
|
|
public override void WriteLine(int value) { _text_writer.SetLogText(string.Format("{0}{1}", value.ToString(), System.Environment.NewLine)); } |
266 |
|
|
public override void WriteLine(long value) { _text_writer.SetLogText(string.Format("{0}{1}", value.ToString(), System.Environment.NewLine)); } |
267 |
|
|
public override void WriteLine(object value) { _text_writer.SetLogText(string.Format("{0}{1}", value.ToString(), System.Environment.NewLine)); } |
268 |
|
|
[SecuritySafeCritical] |
269 |
|
|
public override void WriteLine(string value) { _text_writer.SetLogText(string.Format("{0}{1}", value, System.Environment.NewLine)); } |
270 |
|
|
public override void WriteLine(uint value) { _text_writer.SetLogText(string.Format("{0}{1}", value.ToString(), System.Environment.NewLine)); } |
271 |
|
|
public override void WriteLine(ulong value) { _text_writer.SetLogText(string.Format("{0}{1}", value.ToString(), System.Environment.NewLine)); } |
272 |
|
|
public override void WriteLine(string format, object arg0) { _text_writer.SetLogText(string.Format("{0}{1}", string.Format(format, arg0), System.Environment.NewLine)); } |
273 |
|
|
public override void WriteLine(string format, params object[] arg) { _text_writer.SetLogText(string.Format("{0}{1}", string.Format(format, arg), System.Environment.NewLine)); } |
274 |
|
|
public override void WriteLine(char[] buffer, int index, int count) |
275 |
|
|
{ |
276 |
|
|
byte[] t = new byte[count]; |
277 |
|
|
MemoryStream ms = new MemoryStream(); |
278 |
|
|
StreamWriter sw = new StreamWriter(ms); |
279 |
|
|
sw.AutoFlush = true; |
280 |
|
|
sw.Write(buffer, index, count); |
281 |
|
|
sw.Close(); |
282 |
|
|
_text_writer.SetLogText(string.Format("{0}{1}",Encoding.GetString(ms.ToArray()),System.Environment.NewLine)); |
283 |
|
|
} |
284 |
|
|
public override void WriteLine(string format, object arg0, object arg1) { _text_writer.SetLogText(string.Format("{0}{1}", string.Format(format, arg0, arg1), System.Environment.NewLine)); } |
285 |
|
|
public override void WriteLine(string format, object arg0, object arg1, object arg2) { _text_writer.SetLogText(string.Format("{0}{1}", string.Format(format, arg0, arg1, arg2), System.Environment.NewLine)); } |
286 |
william |
18 |
#endregion |
287 |
william |
17 |
} |
288 |
william |
18 |
#endregion |
289 |
william |
105 |
|
290 |
|
|
private void autoscroll_timer_Tick(object sender, EventArgs e) |
291 |
william |
155 |
{ |
292 |
william |
113 |
try |
293 |
|
|
{ |
294 |
|
|
if (this.DesignMode) { return; } |
295 |
william |
155 |
////txtLog.ScrollToEnd(); |
296 |
|
|
//txtLog.SelectionStart = txtLog.Text.Length - 100; |
297 |
william |
113 |
//txtLog.ScrollToCaret(); |
298 |
|
|
//txtLog.Refresh(); |
299 |
william |
105 |
|
300 |
william |
246 |
//txtLog.SelectionStart = 0; |
301 |
|
|
//txtLog.ScrollToCaret(); |
302 |
|
|
//txtLog.Refresh(); |
303 |
william |
105 |
|
304 |
william |
246 |
//int last_line_position = -1; |
305 |
|
|
//int position = -1; |
306 |
william |
113 |
int len = txtLog.Text.Length; |
307 |
william |
246 |
//StringReader sr = new StringReader(this.StreamToString()); |
308 |
|
|
//string line = ""; |
309 |
|
|
//while ((line = sr.ReadLine()) != null) |
310 |
|
|
//{ |
311 |
|
|
// position += (line.Length * 2); |
312 |
|
|
// last_line_position = (line.Length * 2); |
313 |
|
|
// //if (line.EndsWith("/n")) |
314 |
|
|
// //{ |
315 |
|
|
// // position++; |
316 |
|
|
// //} |
317 |
|
|
// //logger.VerboseDebug.WriteLine("current line: {0}", line); |
318 |
|
|
//} |
319 |
|
|
txtLog.SelectionStart = len; |
320 |
william |
113 |
txtLog.ScrollToCaret(); |
321 |
|
|
txtLog.Refresh(); |
322 |
|
|
//autoscroll_timer.Enabled = false; |
323 |
william |
105 |
} |
324 |
william |
113 |
catch { } |
325 |
william |
105 |
|
326 |
|
|
} |
327 |
william |
298 |
|
328 |
|
|
private void logupdater_DoWork(object sender, DoWorkEventArgs e) |
329 |
|
|
{ |
330 |
|
|
string message = e.Argument.ToString(); |
331 |
|
|
SetLogText(string.Format("{0}", message)); |
332 |
|
|
} |
333 |
|
|
|
334 |
|
|
private void logupdater_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) |
335 |
|
|
{ |
336 |
|
|
|
337 |
|
|
} |
338 |
william |
17 |
} |
339 |
|
|
} |
340 |
william |
105 |
|