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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 316 - (hide annotations) (download)
Tue Jun 5 15:35:51 2012 UTC (8 years, 8 months ago) by william
File size: 13596 byte(s)
+ make SetLogText thread safe

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

  ViewVC Help
Powered by ViewVC 1.1.22