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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.22