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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 800 - (show annotations) (download)
Mon Apr 7 12:16:42 2014 UTC (6 years, 7 months ago) by william
File size: 14189 byte(s)
+ cleaup LogWriter

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 txtLog.AppendText(value);
110 }
111 catch (Exception)
112 {
113 }
114 }
115 }
116
117 new public string Text
118 {
119 get
120 {
121 throw new InvalidOperationException("Please use GetLogText()");
122 }
123 private set
124 {
125 throw new InvalidOperationException("Please use SetLogText(string value)");
126 }
127 }
128 public void CreateNewLog(bool delete)
129 {
130 if (delete)
131 {
132 FileInfo fi = new FileInfo(LOG_PATH);
133 if (fi.Exists)
134 fi.Delete();
135 }
136 Log.WriteLine("{0} ({2} v{3} {4}) created on {1}", LoggingConstants.AppLogFile, DateTime.Now.ToString(), LoggingConstants.AppName, LoggingConstants.AppVersion, LoggingConstants.AppBuild);
137 }
138 public void Clear()
139 {
140 Clear(false);
141 }
142 private void Clear(bool force)
143 {
144 bool allow_log_clearing = false;
145 #if ALLOW_LOG_CLEARING
146 allow_log_clearing = true;
147 #endif
148 if (force || allow_log_clearing) { txtLog.Clear(); }
149 }
150 #region sub-classes
151 public class LogStream : TextWriter
152 {
153 private StringBuilder strMessage = new StringBuilder();
154 public LogStream() : this(null) { }
155 public LogStream(LogWriter text) : base() { _text_writer = text; this.NewLine = "\n"; }
156 private LogWriter _text_writer;
157 #region Overriden Methods
158 public override Encoding Encoding { get { return Encoding.UTF8; } }
159 public override void Write(bool value) { _text_writer.SetLogText(value.ToString()); }
160 public override void Write(char value) { _text_writer.SetLogText(value.ToString()); }
161 public override void Write(char[] buffer) { _text_writer.SetLogText(new string(buffer)); }
162 public override void Write(decimal value) { _text_writer.SetLogText(value.ToString()); }
163 public override void Write(double value) { _text_writer.SetLogText(value.ToString()); }
164 public override void Write(float value) { _text_writer.SetLogText(value.ToString()); }
165 public override void Write(int value) { _text_writer.SetLogText(value.ToString()); }
166 public override void Write(long value) { _text_writer.SetLogText(value.ToString()); }
167 public override void Write(object value) { _text_writer.SetLogText(value.ToString()); }
168 public override void Write(string value) { _text_writer.SetLogText(value.ToString()); }
169 public override void Write(uint value) { _text_writer.SetLogText(value.ToString()); }
170 public override void Write(ulong value) { _text_writer.SetLogText(value.ToString()); }
171 public override void Write(string format, object arg0) { _text_writer.SetLogText(string.Format(format, arg0)); }
172 public override void Write(string format, params object[] arg) { _text_writer.SetLogText(string.Format(format, arg)); }
173 public override void Write(char[] buffer, int index, int count)
174 {
175 byte[] t = new byte[count];
176 MemoryStream ms = new MemoryStream();
177 StreamWriter sw = new StreamWriter(ms);
178 sw.AutoFlush = true;
179 sw.Write(buffer, index, count);
180 sw.Close();
181 _text_writer.SetLogText(Encoding.GetString(ms.ToArray()));
182 }
183 public override void Write(string format, object arg0, object arg1) { _text_writer.SetLogText(string.Format(format, arg0, arg1)); }
184 public override void Write(string format, object arg0, object arg1, object arg2) { _text_writer.SetLogText(string.Format(format, arg0, arg1, arg2)); }
185 public override void WriteLine() { _text_writer.SetLogText(System.Environment.NewLine); }
186 public override void WriteLine(bool value) { _text_writer.SetLogText(string.Format("{0}{1}", value.ToString(), System.Environment.NewLine)); }
187 public override void WriteLine(char value) { _text_writer.SetLogText(string.Format("{0}{1}", value.ToString(), System.Environment.NewLine)); }
188 public override void WriteLine(char[] buffer) { _text_writer.SetLogText(string.Format("{0}{1}", new string(buffer), System.Environment.NewLine)); }
189 public override void WriteLine(decimal value) { _text_writer.SetLogText(string.Format("{0}{1}", value.ToString(), System.Environment.NewLine)); }
190 public override void WriteLine(double value) { _text_writer.SetLogText(string.Format("{0}{1}", value.ToString(), System.Environment.NewLine)); }
191 public override void WriteLine(float value) { _text_writer.SetLogText(string.Format("{0}{1}", value.ToString(), System.Environment.NewLine)); }
192 public override void WriteLine(int value) { _text_writer.SetLogText(string.Format("{0}{1}", value.ToString(), System.Environment.NewLine)); }
193 public override void WriteLine(long value) { _text_writer.SetLogText(string.Format("{0}{1}", value.ToString(), System.Environment.NewLine)); }
194 public override void WriteLine(object value) { _text_writer.SetLogText(string.Format("{0}{1}", value.ToString(), System.Environment.NewLine)); }
195 [SecuritySafeCritical]
196 public override void WriteLine(string value) { _text_writer.SetLogText(string.Format("{0}{1}", value, System.Environment.NewLine)); }
197 public override void WriteLine(uint value) { _text_writer.SetLogText(string.Format("{0}{1}", value.ToString(), System.Environment.NewLine)); }
198 public override void WriteLine(ulong value) { _text_writer.SetLogText(string.Format("{0}{1}", value.ToString(), System.Environment.NewLine)); }
199 public override void WriteLine(string format, object arg0) { _text_writer.SetLogText(string.Format("{0}{1}", string.Format(format, arg0), System.Environment.NewLine)); }
200 public override void WriteLine(string format, params object[] arg) { _text_writer.SetLogText(string.Format("{0}{1}", string.Format(format, arg), System.Environment.NewLine)); }
201 public override void WriteLine(char[] buffer, int index, int count)
202 {
203 byte[] t = new byte[count];
204 MemoryStream ms = new MemoryStream();
205 StreamWriter sw = new StreamWriter(ms);
206 sw.AutoFlush = true;
207 sw.Write(buffer, index, count);
208 sw.Close();
209 _text_writer.SetLogText(string.Format("{0}{1}",Encoding.GetString(ms.ToArray()),System.Environment.NewLine));
210 }
211 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)); }
212 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)); }
213 #endregion
214 }
215 #endregion
216
217 private void autoscroll_timer_Tick(object sender, EventArgs e)
218 {
219 //try
220 //{
221 // if (this.DesignMode) { return; }
222 // AUTO_SCROLL = true;
223 // ////txtLog.ScrollToEnd();
224 // //txtLog.SelectionStart = txtLog.Text.Length - 100;
225 // //txtLog.ScrollToCaret();
226 // //txtLog.Refresh();
227
228 // //txtLog.SelectionStart = 0;
229 // //txtLog.ScrollToCaret();
230 // //txtLog.Refresh();
231
232 // //int last_line_position = -1;
233 // //int position = -1;
234 // int len = txtLog.Text.Length;
235 // //StringReader sr = new StringReader(this.StreamToString());
236 // //string line = "";
237 // //while ((line = sr.ReadLine()) != null)
238 // //{
239 // // position += (line.Length * 2);
240 // // last_line_position = (line.Length * 2);
241 // // //if (line.EndsWith("/n"))
242 // // //{
243 // // // position++;
244 // // //}
245 // // //logger.VerboseDebug.WriteLine("current line: {0}", line);
246 // //}
247 // txtLog.SelectionStart = len;
248 // txtLog.ScrollToCaret();
249 // txtLog.Refresh();
250 // //autoscroll_timer.Enabled = false;
251 // FIRST_LOAD = false;
252 //}
253 //catch { }
254
255 }
256
257 private void logupdater_DoWork(object sender, DoWorkEventArgs e)
258 {
259 string message = e.Argument.ToString();
260 SetLogText(string.Format("{0}", message));
261 }
262
263 private void logupdater_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
264 {
265
266 }
267
268 private void LogWriter_Load(object sender, EventArgs e)
269 {
270 //txtLog.SelectionStart = 0;
271 //txtLog.ScrollToCaret();
272 }
273
274 private void chkAutoScroll_CheckedChanged(object sender, EventArgs e)
275 {
276 this.AutoScroll = chkAutoScroll.Checked;
277 }
278
279 private void txtLog_TextChanged(object sender, EventArgs e)
280 {
281 if (AutoScroll)
282 {
283 txtLog.SelectionStart = 0;
284 txtLog.ScrollToCaret();
285 txtLog.SelectionStart = txtLog.TextLength;
286 txtLog.ScrollToCaret();
287 txtLog.Refresh();
288 }
289 }
290 }
291 }
292

  ViewVC Help
Powered by ViewVC 1.1.22