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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 773 - (show annotations) (download)
Thu Jun 20 22:03:10 2013 UTC (7 years, 5 months ago) by william
File size: 14614 byte(s)

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

  ViewVC Help
Powered by ViewVC 1.1.22