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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 802 - (hide annotations) (download)
Mon Apr 7 12:23:46 2014 UTC (7 years, 9 months ago) by william
File size: 12672 byte(s)
LogWriter: when AutoScoll status changes, make sure to scroll to the bottom (don't wait for the TextChange event to fire)

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 773 if (value)
60     {
61     Console.SetOut(TextWriter.Synchronized(this.Log));
62     }
63 william 18 else
64 william 17 {
65 william 800 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 william 17 }
76     }
77     }
78    
79     private void btnClearLog_Click(object sender, EventArgs e) { this.Clear(true); }
80 william 318 private void btnCopyLogToClipboard_Click(object sender, EventArgs e) { Clipboard.SetText(txtLog.Text.Replace(Log.NewLine,System.Environment.NewLine)); }
81 william 316
82    
83     public void WriteLogToFile(string value)
84 william 17 {
85 william 18 FileStream fs = new FileStream(LOG_PATH, FileMode.Append, FileAccess.Write, FileShare.ReadWrite);
86     StreamWriter writer = new StreamWriter(fs);
87     writer.AutoFlush = true;
88 william 19 if (value == Log.NewLine) { writer.Write(System.Environment.NewLine); }
89     else { writer.Write(value); }
90 william 18 writer.Close();
91 william 316 }
92     public void SetLogText(string value) { SetLogText(value, false); }
93     public void SetLogText(string value, bool invoked)
94     {
95     if (!invoked) { WriteLogToFile(value); }
96 william 309
97 william 316 if (txtLog.InvokeRequired)
98 william 309 {
99 william 316 this.Invoke(HandleSetLogText, new object[] { value, true });
100 william 309 }
101 william 316 else
102     {
103 william 773 if (this.IsDisposed || this.txtLog.IsDisposed)
104 william 771 {
105     return;
106     }
107 william 772 try
108     {
109     txtLog.AppendText(value);
110     }
111     catch (Exception)
112     {
113 william 773 }
114 william 316 }
115 william 17 }
116 william 105
117 william 17 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 william 23 public void CreateNewLog(bool delete)
129 william 22 {
130 william 23 if (delete)
131     {
132     FileInfo fi = new FileInfo(LOG_PATH);
133     if (fi.Exists)
134     fi.Delete();
135     }
136 william 105 Log.WriteLine("{0} ({2} v{3} {4}) created on {1}", LoggingConstants.AppLogFile, DateTime.Now.ToString(), LoggingConstants.AppName, LoggingConstants.AppVersion, LoggingConstants.AppBuild);
137 william 22 }
138 william 18 public void Clear()
139 william 17 {
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 william 309 if (force || allow_log_clearing) { txtLog.Clear(); }
149 william 17 }
150     #region sub-classes
151     public class LogStream : TextWriter
152 william 18 {
153 william 105 private StringBuilder strMessage = new StringBuilder();
154 william 17 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 william 301 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 william 18 #endregion
214 william 17 }
215 william 18 #endregion
216 william 105
217 william 298 private void logupdater_DoWork(object sender, DoWorkEventArgs e)
218     {
219     string message = e.Argument.ToString();
220     SetLogText(string.Format("{0}", message));
221     }
222    
223 william 801 private void logupdater_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { }
224 william 298
225 william 801 private void LogWriter_Load(object sender, EventArgs e) { }
226 william 303
227 william 307 private void chkAutoScroll_CheckedChanged(object sender, EventArgs e)
228 william 802 {
229 william 307 this.AutoScroll = chkAutoScroll.Checked;
230 william 802 if (this.AutoScroll)
231     {
232     // initially scroll
233     txtLog_TextChanged(sender, e);
234     }
235 william 307 }
236 william 725
237     private void txtLog_TextChanged(object sender, EventArgs e)
238     {
239     if (AutoScroll)
240     {
241     txtLog.SelectionStart = 0;
242     txtLog.ScrollToCaret();
243     txtLog.SelectionStart = txtLog.TextLength;
244     txtLog.ScrollToCaret();
245     txtLog.Refresh();
246     }
247     }
248 william 17 }
249     }
250 william 105

  ViewVC Help
Powered by ViewVC 1.1.22