14 |
|
|
15 |
namespace RomCheater.Logging |
namespace RomCheater.Logging |
16 |
{ |
{ |
17 |
public partial class LogWriter : UserControl |
// 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); } } |
// 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); |
// private delegate void OnSetLogText(string value, bool invoked); |
21 |
private OnSetLogText HandleSetLogText = null; |
// private OnSetLogText HandleSetLogText = null; |
22 |
public LogWriter() : this(false) { } |
// public LogWriter() : this(false) { } |
23 |
public LogWriter(bool redirectConsole) |
// public LogWriter(bool redirectConsole) |
24 |
{ |
// { |
25 |
InitializeComponent(); |
// InitializeComponent(); |
26 |
HandleSetLogText += new OnSetLogText(SetLogText); |
// HandleSetLogText += new OnSetLogText(SetLogText); |
27 |
this.Log = new LogStream(this); |
// this.Log = new LogStream(this); |
28 |
this.RedirectConsoleOutput = redirectConsole; |
// this.RedirectConsoleOutput = redirectConsole; |
29 |
toolTip1.SetToolTip(btnClearLog, "Clears the Log"); |
// toolTip1.SetToolTip(btnClearLog, "Clears the Log"); |
30 |
toolTip1.SetToolTip(btnCopyLogToClipboard, "Copies the Log to the Clipboard"); |
// toolTip1.SetToolTip(btnCopyLogToClipboard, "Copies the Log to the Clipboard"); |
31 |
chkAutoScroll.Checked = this.AutoScroll; |
// chkAutoScroll.Checked = this.AutoScroll; |
32 |
} |
// } |
33 |
private LogStream _Log; |
// private LogStream _Log; |
34 |
public LogStream Log { get { return _Log; } private set { _Log = value; } } |
// public LogStream Log { get { return _Log; } private set { _Log = value; } } |
35 |
private bool _AutoScroll; |
// private bool _AutoScroll; |
36 |
new public bool AutoScroll |
// new public bool AutoScroll |
37 |
{ |
// { |
38 |
get |
// get |
39 |
{ |
// { |
40 |
return _AutoScroll; |
// return _AutoScroll; |
41 |
} |
// } |
42 |
set |
// set |
43 |
{ |
// { |
44 |
if (!_AutoScroll) |
// if (!_AutoScroll) |
45 |
chkAutoScroll.Checked = true; |
// chkAutoScroll.Checked = true; |
46 |
if (AutoScroll && !value) |
// if (AutoScroll && !value) |
47 |
chkAutoScroll.Checked = false; |
// chkAutoScroll.Checked = false; |
48 |
_AutoScroll = value; |
// _AutoScroll = value; |
49 |
} |
// } |
50 |
} |
// } |
51 |
|
|
52 |
private bool _RedirectConsoleOutput; |
// private bool _RedirectConsoleOutput; |
53 |
public bool RedirectConsoleOutput |
// public bool RedirectConsoleOutput |
54 |
{ |
// { |
55 |
get { return _RedirectConsoleOutput; } |
// get { return _RedirectConsoleOutput; } |
56 |
set |
// set |
57 |
{ |
// { |
58 |
_RedirectConsoleOutput = value; |
// _RedirectConsoleOutput = value; |
59 |
if (value) |
// if (value) |
60 |
{ |
// { |
61 |
Console.SetOut(TextWriter.Synchronized(this.Log)); |
// Console.SetOut(TextWriter.Synchronized(this.Log)); |
62 |
} |
// } |
63 |
else |
// else |
64 |
{ |
// { |
65 |
Stream stream = Console.OpenStandardOutput(0x100); |
// Stream stream = Console.OpenStandardOutput(0x100); |
66 |
TextWriter writer = null; |
// TextWriter writer = null; |
67 |
if (stream == Stream.Null) { writer = TextWriter.Synchronized(StreamWriter.Null); } |
// if (stream == Stream.Null) { writer = TextWriter.Synchronized(StreamWriter.Null); } |
68 |
Encoding encoding = this.Log.Encoding; |
// Encoding encoding = this.Log.Encoding; |
69 |
StreamWriter writer2 = new StreamWriter(stream, encoding, 0x100); |
// StreamWriter writer2 = new StreamWriter(stream, encoding, 0x100); |
70 |
//writer2.HaveWrittenPreamble = true; |
// //writer2.HaveWrittenPreamble = true; |
71 |
writer2.AutoFlush = true; |
// writer2.AutoFlush = true; |
72 |
writer = TextWriter.Synchronized(writer2); |
// writer = TextWriter.Synchronized(writer2); |
73 |
Console.SetOut(writer); |
// Console.SetOut(writer); |
74 |
Console.SetOut(this.Log); |
// Console.SetOut(this.Log); |
75 |
} |
// } |
76 |
} |
// } |
77 |
} |
// } |
78 |
|
|
79 |
private void btnClearLog_Click(object sender, EventArgs e) { this.Clear(true); } |
// 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)); } |
// 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) |
// public void WriteLogToFile(string value) |
84 |
{ |
// { |
85 |
FileStream fs = new FileStream(LOG_PATH, FileMode.Append, FileAccess.Write, FileShare.ReadWrite); |
// FileStream fs = new FileStream(LOG_PATH, FileMode.Append, FileAccess.Write, FileShare.ReadWrite); |
86 |
StreamWriter writer = new StreamWriter(fs); |
// StreamWriter writer = new StreamWriter(fs); |
87 |
writer.AutoFlush = true; |
// writer.AutoFlush = true; |
88 |
if (value == Log.NewLine) { writer.Write(System.Environment.NewLine); } |
// if (value == Log.NewLine) { writer.Write(System.Environment.NewLine); } |
89 |
else { writer.Write(value); } |
// else { writer.Write(value); } |
90 |
writer.Close(); |
// writer.Close(); |
91 |
} |
// } |
92 |
public void SetLogText(string value) { SetLogText(value, false); } |
// public void SetLogText(string value) { SetLogText(value, false); } |
93 |
public void SetLogText(string value, bool invoked) |
// public void SetLogText(string value, bool invoked) |
94 |
{ |
// { |
95 |
if (!invoked) { WriteLogToFile(value); } |
// if (!invoked) { WriteLogToFile(value); } |
96 |
|
|
97 |
if (txtLog.InvokeRequired) |
// if (txtLog.InvokeRequired) |
98 |
{ |
// { |
99 |
this.Invoke(HandleSetLogText, new object[] { value, true }); |
// this.Invoke(HandleSetLogText, new object[] { value, true }); |
100 |
} |
// } |
101 |
else |
// else |
102 |
{ |
// { |
103 |
if (this.IsDisposed || this.txtLog.IsDisposed) |
// if (this.IsDisposed || this.txtLog.IsDisposed) |
104 |
{ |
// { |
105 |
return; |
// return; |
106 |
} |
// } |
107 |
try |
// try |
108 |
{ |
// { |
109 |
txtLog.AppendText(value); |
// txtLog.AppendText(value); |
110 |
} |
// } |
111 |
catch (Exception) |
// catch (Exception) |
112 |
{ |
// { |
113 |
} |
// } |
114 |
} |
// } |
115 |
} |
// } |
116 |
|
|
117 |
new public string Text |
// new public string Text |
118 |
{ |
// { |
119 |
get |
// get |
120 |
{ |
// { |
121 |
throw new InvalidOperationException("Please use GetLogText()"); |
// throw new InvalidOperationException("Please use GetLogText()"); |
122 |
} |
// } |
123 |
private set |
// private set |
124 |
{ |
// { |
125 |
throw new InvalidOperationException("Please use SetLogText(string value)"); |
// throw new InvalidOperationException("Please use SetLogText(string value)"); |
126 |
} |
// } |
127 |
} |
// } |
128 |
public void CreateNewLog(bool delete) |
// public void CreateNewLog(bool delete) |
129 |
{ |
// { |
130 |
if (delete) |
// if (delete) |
131 |
{ |
// { |
132 |
FileInfo fi = new FileInfo(LOG_PATH); |
// FileInfo fi = new FileInfo(LOG_PATH); |
133 |
if (fi.Exists) |
// if (fi.Exists) |
134 |
fi.Delete(); |
// 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); |
// 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() |
// public void Clear() |
139 |
{ |
// { |
140 |
Clear(false); |
// Clear(false); |
141 |
} |
// } |
142 |
private void Clear(bool force) |
// private void Clear(bool force) |
143 |
{ |
// { |
144 |
bool allow_log_clearing = false; |
// bool allow_log_clearing = false; |
145 |
#if ALLOW_LOG_CLEARING |
//#if ALLOW_LOG_CLEARING |
146 |
allow_log_clearing = true; |
// allow_log_clearing = true; |
147 |
#endif |
//#endif |
148 |
if (force || allow_log_clearing) { txtLog.Clear(); } |
// if (force || allow_log_clearing) { txtLog.Clear(); } |
149 |
} |
// } |
150 |
#region sub-classes |
// #region sub-classes |
151 |
public class LogStream : TextWriter |
// public class LogStream : TextWriter |
152 |
{ |
// { |
153 |
private StringBuilder strMessage = new StringBuilder(); |
// private StringBuilder strMessage = new StringBuilder(); |
154 |
public LogStream() : this(null) { } |
// public LogStream() : this(null) { } |
155 |
public LogStream(LogWriter text) : base() { _text_writer = text; this.NewLine = "\n"; } |
// public LogStream(LogWriter text) : base() { _text_writer = text; this.NewLine = "\n"; } |
156 |
private LogWriter _text_writer; |
// private LogWriter _text_writer; |
157 |
#region Overriden Methods |
// #region Overriden Methods |
158 |
public override Encoding Encoding { get { return Encoding.UTF8; } } |
// public override Encoding Encoding { get { return Encoding.UTF8; } } |
159 |
public override void Write(bool value) { _text_writer.SetLogText(value.ToString()); } |
// public override void Write(bool value) { _text_writer.SetLogText(value.ToString()); } |
160 |
public override void Write(char value) { _text_writer.SetLogText(value.ToString()); } |
// public override void Write(char value) { _text_writer.SetLogText(value.ToString()); } |
161 |
public override void Write(char[] buffer) { _text_writer.SetLogText(new string(buffer)); } |
// public override void Write(char[] buffer) { _text_writer.SetLogText(new string(buffer)); } |
162 |
public override void Write(decimal value) { _text_writer.SetLogText(value.ToString()); } |
// public override void Write(decimal value) { _text_writer.SetLogText(value.ToString()); } |
163 |
public override void Write(double value) { _text_writer.SetLogText(value.ToString()); } |
// public override void Write(double value) { _text_writer.SetLogText(value.ToString()); } |
164 |
public override void Write(float value) { _text_writer.SetLogText(value.ToString()); } |
// public override void Write(float value) { _text_writer.SetLogText(value.ToString()); } |
165 |
public override void Write(int value) { _text_writer.SetLogText(value.ToString()); } |
// public override void Write(int value) { _text_writer.SetLogText(value.ToString()); } |
166 |
public override void Write(long value) { _text_writer.SetLogText(value.ToString()); } |
// public override void Write(long value) { _text_writer.SetLogText(value.ToString()); } |
167 |
public override void Write(object value) { _text_writer.SetLogText(value.ToString()); } |
// public override void Write(object value) { _text_writer.SetLogText(value.ToString()); } |
168 |
public override void Write(string value) { _text_writer.SetLogText(value.ToString()); } |
// public override void Write(string value) { _text_writer.SetLogText(value.ToString()); } |
169 |
public override void Write(uint value) { _text_writer.SetLogText(value.ToString()); } |
// public override void Write(uint value) { _text_writer.SetLogText(value.ToString()); } |
170 |
public override void Write(ulong value) { _text_writer.SetLogText(value.ToString()); } |
// 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)); } |
// 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)); } |
// 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) |
// public override void Write(char[] buffer, int index, int count) |
174 |
{ |
// { |
175 |
byte[] t = new byte[count]; |
// byte[] t = new byte[count]; |
176 |
MemoryStream ms = new MemoryStream(); |
// MemoryStream ms = new MemoryStream(); |
177 |
StreamWriter sw = new StreamWriter(ms); |
// StreamWriter sw = new StreamWriter(ms); |
178 |
sw.AutoFlush = true; |
// sw.AutoFlush = true; |
179 |
sw.Write(buffer, index, count); |
// sw.Write(buffer, index, count); |
180 |
sw.Close(); |
// sw.Close(); |
181 |
_text_writer.SetLogText(Encoding.GetString(ms.ToArray())); |
// _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)); } |
// 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)); } |
// 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); } |
// 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)); } |
// 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)); } |
// 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)); } |
// 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)); } |
// 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)); } |
// 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)); } |
// 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)); } |
// 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)); } |
// 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)); } |
// public override void WriteLine(object value) { _text_writer.SetLogText(string.Format("{0}{1}", value.ToString(), System.Environment.NewLine)); } |
195 |
[SecuritySafeCritical] |
// [SecuritySafeCritical] |
196 |
public override void WriteLine(string value) { _text_writer.SetLogText(string.Format("{0}{1}", value, System.Environment.NewLine)); } |
// 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)); } |
// 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)); } |
// 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)); } |
// 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)); } |
// 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) |
// public override void WriteLine(char[] buffer, int index, int count) |
202 |
{ |
// { |
203 |
byte[] t = new byte[count]; |
// byte[] t = new byte[count]; |
204 |
MemoryStream ms = new MemoryStream(); |
// MemoryStream ms = new MemoryStream(); |
205 |
StreamWriter sw = new StreamWriter(ms); |
// StreamWriter sw = new StreamWriter(ms); |
206 |
sw.AutoFlush = true; |
// sw.AutoFlush = true; |
207 |
sw.Write(buffer, index, count); |
// sw.Write(buffer, index, count); |
208 |
sw.Close(); |
// sw.Close(); |
209 |
_text_writer.SetLogText(string.Format("{0}{1}",Encoding.GetString(ms.ToArray()),System.Environment.NewLine)); |
// _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)); } |
// 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)); } |
// 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 |
// #endregion |
214 |
} |
// } |
215 |
#endregion |
// #endregion |
216 |
|
|
217 |
private void logupdater_DoWork(object sender, DoWorkEventArgs e) |
// private void logupdater_DoWork(object sender, DoWorkEventArgs e) |
218 |
{ |
// { |
219 |
string message = e.Argument.ToString(); |
// string message = e.Argument.ToString(); |
220 |
SetLogText(string.Format("{0}", message)); |
// SetLogText(string.Format("{0}", message)); |
221 |
} |
// } |
222 |
|
|
223 |
private void logupdater_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { } |
// private void logupdater_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { } |
224 |
|
|
225 |
private void LogWriter_Load(object sender, EventArgs e) { } |
// private void LogWriter_Load(object sender, EventArgs e) { } |
226 |
|
|
227 |
private void chkAutoScroll_CheckedChanged(object sender, EventArgs e) |
// private void chkAutoScroll_CheckedChanged(object sender, EventArgs e) |
228 |
{ |
// { |
229 |
this.AutoScroll = chkAutoScroll.Checked; |
// this.AutoScroll = chkAutoScroll.Checked; |
230 |
if (this.AutoScroll) |
// if (this.AutoScroll) |
231 |
{ |
// { |
232 |
// initially scroll |
// // initially scroll |
233 |
txtLog_TextChanged(sender, e); |
// txtLog_TextChanged(sender, e); |
234 |
} |
// } |
235 |
} |
// } |
236 |
|
|
237 |
private void txtLog_TextChanged(object sender, EventArgs e) |
// private void txtLog_TextChanged(object sender, EventArgs e) |
238 |
{ |
// { |
239 |
if (AutoScroll) |
// if (AutoScroll) |
240 |
{ |
// { |
241 |
txtLog.SelectionStart = 0; |
// txtLog.SelectionStart = 0; |
242 |
txtLog.ScrollToCaret(); |
// txtLog.ScrollToCaret(); |
243 |
txtLog.SelectionStart = txtLog.TextLength; |
// txtLog.SelectionStart = txtLog.TextLength; |
244 |
txtLog.ScrollToCaret(); |
// txtLog.ScrollToCaret(); |
245 |
txtLog.Refresh(); |
// txtLog.Refresh(); |
246 |
} |
// } |
247 |
} |
// } |
248 |
} |
// } |
249 |
} |
} |
250 |
|
|