ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/RomCheater/trunk/RomCheater.Logging/LogWriter.cs
Revision: 318
Committed: Tue Jun 5 17:57:37 2012 UTC (10 years, 9 months ago) by william
File size: 13648 byte(s)
Log Message:
+ add support for displaying PE Struct data into property grid (read-only)

File Contents

# Content
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 txtLog.AppendText(value);
100 if (AutoScroll)
101 {
102 txtLog.SelectionStart = txtLog.TextLength;
103 txtLog.ScrollToCaret();
104 txtLog.Refresh();
105 }
106 }
107 Thread.Sleep(100);
108 }
109
110 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 public void CreateNewLog(bool delete)
122 {
123 if (delete)
124 {
125 FileInfo fi = new FileInfo(LOG_PATH);
126 if (fi.Exists)
127 fi.Delete();
128 }
129 Log.WriteLine("{0} ({2} v{3} {4}) created on {1}", LoggingConstants.AppLogFile, DateTime.Now.ToString(), LoggingConstants.AppName, LoggingConstants.AppVersion, LoggingConstants.AppBuild);
130 }
131 public void Clear()
132 {
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 if (force || allow_log_clearing) { txtLog.Clear(); }
142 }
143 #region sub-classes
144 public class LogStream : TextWriter
145 {
146 private StringBuilder strMessage = new StringBuilder();
147 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 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 #endregion
207 }
208 #endregion
209
210 private void autoscroll_timer_Tick(object sender, EventArgs e)
211 {
212 //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
221 // //txtLog.SelectionStart = 0;
222 // //txtLog.ScrollToCaret();
223 // //txtLog.Refresh();
224
225 // //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
248 }
249
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
261 private void LogWriter_Load(object sender, EventArgs e)
262 {
263 txtLog.SelectionStart = 0;
264 txtLog.ScrollToCaret();
265 }
266
267 private void chkAutoScroll_CheckedChanged(object sender, EventArgs e)
268 {
269 this.AutoScroll = chkAutoScroll.Checked;
270 }
271 }
272 }
273