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