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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 301 - (show annotations) (download)
Tue Jun 5 13:27:07 2012 UTC (9 years, 5 months ago) by william
File size: 16132 byte(s)
+ massive speed boost in logging

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 private LogStream _Log;
48 public LogStream Log { get { return _Log; } private set { _Log = value; } }
49
50
51 //private MemoryStream ms;
52 //private StreamWriter sw;
53 //private StreamReader sr;
54 private bool _AutoScroll;
55 new public bool AutoScroll { get { return _AutoScroll; } set { _AutoScroll = value; autoscroll_timer.Enabled = value; } }
56
57 private bool _RedirectConsoleOutput;
58 public bool RedirectConsoleOutput
59 {
60 get { return _RedirectConsoleOutput; }
61 set
62 {
63 _RedirectConsoleOutput = value;
64 if (value) { Console.SetOut(this.Log); }
65 else
66 {
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 Console.SetOut(writer);
76 }
77 }
78 }
79
80 private void btnClearLog_Click(object sender, EventArgs e) { this.Clear(true); }
81 private void btnCopyLogToClipboard_Click(object sender, EventArgs e) { Clipboard.SetText(txtLog.Text); }
82
83
84 private string PreserveLineEndings(string text) { return text.Replace("\n", System.Environment.NewLine); }
85
86
87 //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 public void SetLogText(string value)
147 {
148 FileStream fs = new FileStream(LOG_PATH, FileMode.Append, FileAccess.Write, FileShare.ReadWrite);
149 StreamWriter writer = new StreamWriter(fs);
150 writer.AutoFlush = true;
151 if (value == Log.NewLine) { writer.Write(System.Environment.NewLine); }
152 else { writer.Write(value); }
153 writer.Close();
154 //string text = value.Replace(System.Environment.NewLine,"");
155 //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 }
164
165 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 public void CreateNewLog(bool delete)
177 {
178 if (delete)
179 {
180 FileInfo fi = new FileInfo(LOG_PATH);
181 if (fi.Exists)
182 fi.Delete();
183 }
184 Log.WriteLine("{0} ({2} v{3} {4}) created on {1}", LoggingConstants.AppLogFile, DateTime.Now.ToString(), LoggingConstants.AppName, LoggingConstants.AppVersion, LoggingConstants.AppBuild);
185 }
186 public void Clear()
187 {
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
199 txtLog.Clear();
200 //ms = new MemoryStream();
201 //sw = new StreamWriter(ms);
202 //sw.AutoFlush = true;
203 //sr = new StreamReader(ms);
204 }
205 }
206 #region sub-classes
207 public class LogStream : TextWriter
208 {
209 private StringBuilder strMessage = new StringBuilder();
210 public LogStream() : this(null) { }
211 public LogStream(LogWriter text) : base() { _text_writer = text; this.NewLine = "\n"; }
212 private LogWriter _text_writer;
213 //private void _write(string message)
214 //{
215 // if (_text_writer == null) return;
216
217 // 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 #region Overriden Methods
230 public override Encoding Encoding { get { return Encoding.UTF8; } }
231
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 #endregion
287 }
288 #endregion
289
290 private void autoscroll_timer_Tick(object sender, EventArgs e)
291 {
292 try
293 {
294 if (this.DesignMode) { return; }
295 ////txtLog.ScrollToEnd();
296 //txtLog.SelectionStart = txtLog.Text.Length - 100;
297 //txtLog.ScrollToCaret();
298 //txtLog.Refresh();
299
300 //txtLog.SelectionStart = 0;
301 //txtLog.ScrollToCaret();
302 //txtLog.Refresh();
303
304 //int last_line_position = -1;
305 //int position = -1;
306 int len = txtLog.Text.Length;
307 //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 txtLog.ScrollToCaret();
321 txtLog.Refresh();
322 //autoscroll_timer.Enabled = false;
323 }
324 catch { }
325
326 }
327
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 }
339 }
340

  ViewVC Help
Powered by ViewVC 1.1.22