/[RomCheater]/trunk/RomCheater.Logging/LogWriter.cs
ViewVC logotype

Contents of /trunk/RomCheater.Logging/LogWriter.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 308 - (show annotations) (download)
Tue Jun 5 13:52:37 2012 UTC (8 years, 10 months ago) by william
File size: 16840 byte(s)
+ add logic to set autoscroll checkbox on/off based on AutoScroll value

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

  ViewVC Help
Powered by ViewVC 1.1.22