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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 307 - (show annotations) (download)
Tue Jun 5 13:49:07 2012 UTC (8 years, 5 months ago) by william
File size: 16511 byte(s)
+ add a checkbox to turn on/off autoscroll

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

  ViewVC Help
Powered by ViewVC 1.1.22