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 |
public LogWriter() : this(false) { } |
21 |
public LogWriter(bool redirectConsole) |
22 |
{ |
23 |
InitializeComponent(); |
24 |
this.Log = new LogStream(this); |
25 |
this.RedirectConsoleOutput = redirectConsole; |
26 |
toolTip1.SetToolTip(btnClearLog, "Clears the Log"); |
27 |
toolTip1.SetToolTip(btnCopyLogToClipboard, "Copies the Log to the Clipboard"); |
28 |
chkAutoScroll.Checked = this.AutoScroll; |
29 |
} |
30 |
private LogStream _Log; |
31 |
public LogStream Log { get { return _Log; } private set { _Log = value; } } |
32 |
private bool _AutoScroll; |
33 |
new public bool AutoScroll |
34 |
{ |
35 |
get |
36 |
{ |
37 |
return _AutoScroll; |
38 |
} |
39 |
set |
40 |
{ |
41 |
if (!_AutoScroll) |
42 |
chkAutoScroll.Checked = true; |
43 |
if (AutoScroll && !value) |
44 |
chkAutoScroll.Checked = false; |
45 |
_AutoScroll = value; |
46 |
} |
47 |
} |
48 |
|
49 |
private bool _RedirectConsoleOutput; |
50 |
public bool RedirectConsoleOutput |
51 |
{ |
52 |
get { return _RedirectConsoleOutput; } |
53 |
set |
54 |
{ |
55 |
_RedirectConsoleOutput = value; |
56 |
if (value) { Console.SetOut(this.Log); } |
57 |
else |
58 |
{ |
59 |
Stream stream = Console.OpenStandardOutput(0x100); |
60 |
TextWriter writer = null; |
61 |
if (stream == Stream.Null) { writer = TextWriter.Synchronized(StreamWriter.Null); } |
62 |
Encoding encoding = this.Log.Encoding; |
63 |
StreamWriter writer2 = new StreamWriter(stream, encoding, 0x100); |
64 |
//writer2.HaveWrittenPreamble = true; |
65 |
writer2.AutoFlush = true; |
66 |
writer = TextWriter.Synchronized(writer2); |
67 |
Console.SetOut(writer); |
68 |
} |
69 |
} |
70 |
} |
71 |
|
72 |
private void btnClearLog_Click(object sender, EventArgs e) { this.Clear(true); } |
73 |
private void btnCopyLogToClipboard_Click(object sender, EventArgs e) { Clipboard.SetText(txtLog.Text); } |
74 |
public void SetLogText(string value) |
75 |
{ |
76 |
FileStream fs = new FileStream(LOG_PATH, FileMode.Append, FileAccess.Write, FileShare.ReadWrite); |
77 |
StreamWriter writer = new StreamWriter(fs); |
78 |
writer.AutoFlush = true; |
79 |
if (value == Log.NewLine) { writer.Write(System.Environment.NewLine); } |
80 |
else { writer.Write(value); } |
81 |
writer.Close(); |
82 |
txtLog.Text += value; |
83 |
|
84 |
if (AutoScroll) |
85 |
{ |
86 |
txtLog.SelectionStart = txtLog.TextLength; |
87 |
txtLog.ScrollToCaret(); |
88 |
txtLog.Refresh(); |
89 |
} |
90 |
Thread.Sleep(100); |
91 |
} |
92 |
|
93 |
new public string Text |
94 |
{ |
95 |
get |
96 |
{ |
97 |
throw new InvalidOperationException("Please use GetLogText()"); |
98 |
} |
99 |
private set |
100 |
{ |
101 |
throw new InvalidOperationException("Please use SetLogText(string value)"); |
102 |
} |
103 |
} |
104 |
public void CreateNewLog(bool delete) |
105 |
{ |
106 |
if (delete) |
107 |
{ |
108 |
FileInfo fi = new FileInfo(LOG_PATH); |
109 |
if (fi.Exists) |
110 |
fi.Delete(); |
111 |
} |
112 |
Log.WriteLine("{0} ({2} v{3} {4}) created on {1}", LoggingConstants.AppLogFile, DateTime.Now.ToString(), LoggingConstants.AppName, LoggingConstants.AppVersion, LoggingConstants.AppBuild); |
113 |
} |
114 |
public void Clear() |
115 |
{ |
116 |
Clear(false); |
117 |
} |
118 |
private void Clear(bool force) |
119 |
{ |
120 |
bool allow_log_clearing = false; |
121 |
#if ALLOW_LOG_CLEARING |
122 |
allow_log_clearing = true; |
123 |
#endif |
124 |
if (force || allow_log_clearing) { txtLog.Clear(); } |
125 |
} |
126 |
#region sub-classes |
127 |
public class LogStream : TextWriter |
128 |
{ |
129 |
private StringBuilder strMessage = new StringBuilder(); |
130 |
public LogStream() : this(null) { } |
131 |
public LogStream(LogWriter text) : base() { _text_writer = text; this.NewLine = "\n"; } |
132 |
private LogWriter _text_writer; |
133 |
#region Overriden Methods |
134 |
public override Encoding Encoding { get { return Encoding.UTF8; } } |
135 |
public override void Write(bool value) { _text_writer.SetLogText(value.ToString()); } |
136 |
public override void Write(char value) { _text_writer.SetLogText(value.ToString()); } |
137 |
public override void Write(char[] buffer) { _text_writer.SetLogText(new string(buffer)); } |
138 |
public override void Write(decimal value) { _text_writer.SetLogText(value.ToString()); } |
139 |
public override void Write(double value) { _text_writer.SetLogText(value.ToString()); } |
140 |
public override void Write(float value) { _text_writer.SetLogText(value.ToString()); } |
141 |
public override void Write(int value) { _text_writer.SetLogText(value.ToString()); } |
142 |
public override void Write(long value) { _text_writer.SetLogText(value.ToString()); } |
143 |
public override void Write(object value) { _text_writer.SetLogText(value.ToString()); } |
144 |
public override void Write(string value) { _text_writer.SetLogText(value.ToString()); } |
145 |
public override void Write(uint value) { _text_writer.SetLogText(value.ToString()); } |
146 |
public override void Write(ulong value) { _text_writer.SetLogText(value.ToString()); } |
147 |
public override void Write(string format, object arg0) { _text_writer.SetLogText(string.Format(format, arg0)); } |
148 |
public override void Write(string format, params object[] arg) { _text_writer.SetLogText(string.Format(format, arg)); } |
149 |
public override void Write(char[] buffer, int index, int count) |
150 |
{ |
151 |
byte[] t = new byte[count]; |
152 |
MemoryStream ms = new MemoryStream(); |
153 |
StreamWriter sw = new StreamWriter(ms); |
154 |
sw.AutoFlush = true; |
155 |
sw.Write(buffer, index, count); |
156 |
sw.Close(); |
157 |
_text_writer.SetLogText(Encoding.GetString(ms.ToArray())); |
158 |
} |
159 |
public override void Write(string format, object arg0, object arg1) { _text_writer.SetLogText(string.Format(format, arg0, arg1)); } |
160 |
public override void Write(string format, object arg0, object arg1, object arg2) { _text_writer.SetLogText(string.Format(format, arg0, arg1, arg2)); } |
161 |
public override void WriteLine() { _text_writer.SetLogText(System.Environment.NewLine); } |
162 |
public override void WriteLine(bool value) { _text_writer.SetLogText(string.Format("{0}{1}", value.ToString(), System.Environment.NewLine)); } |
163 |
public override void WriteLine(char value) { _text_writer.SetLogText(string.Format("{0}{1}", value.ToString(), System.Environment.NewLine)); } |
164 |
public override void WriteLine(char[] buffer) { _text_writer.SetLogText(string.Format("{0}{1}", new string(buffer), System.Environment.NewLine)); } |
165 |
public override void WriteLine(decimal value) { _text_writer.SetLogText(string.Format("{0}{1}", value.ToString(), System.Environment.NewLine)); } |
166 |
public override void WriteLine(double value) { _text_writer.SetLogText(string.Format("{0}{1}", value.ToString(), System.Environment.NewLine)); } |
167 |
public override void WriteLine(float value) { _text_writer.SetLogText(string.Format("{0}{1}", value.ToString(), System.Environment.NewLine)); } |
168 |
public override void WriteLine(int value) { _text_writer.SetLogText(string.Format("{0}{1}", value.ToString(), System.Environment.NewLine)); } |
169 |
public override void WriteLine(long value) { _text_writer.SetLogText(string.Format("{0}{1}", value.ToString(), System.Environment.NewLine)); } |
170 |
public override void WriteLine(object value) { _text_writer.SetLogText(string.Format("{0}{1}", value.ToString(), System.Environment.NewLine)); } |
171 |
[SecuritySafeCritical] |
172 |
public override void WriteLine(string value) { _text_writer.SetLogText(string.Format("{0}{1}", value, System.Environment.NewLine)); } |
173 |
public override void WriteLine(uint value) { _text_writer.SetLogText(string.Format("{0}{1}", value.ToString(), System.Environment.NewLine)); } |
174 |
public override void WriteLine(ulong value) { _text_writer.SetLogText(string.Format("{0}{1}", value.ToString(), System.Environment.NewLine)); } |
175 |
public override void WriteLine(string format, object arg0) { _text_writer.SetLogText(string.Format("{0}{1}", string.Format(format, arg0), System.Environment.NewLine)); } |
176 |
public override void WriteLine(string format, params object[] arg) { _text_writer.SetLogText(string.Format("{0}{1}", string.Format(format, arg), System.Environment.NewLine)); } |
177 |
public override void WriteLine(char[] buffer, int index, int count) |
178 |
{ |
179 |
byte[] t = new byte[count]; |
180 |
MemoryStream ms = new MemoryStream(); |
181 |
StreamWriter sw = new StreamWriter(ms); |
182 |
sw.AutoFlush = true; |
183 |
sw.Write(buffer, index, count); |
184 |
sw.Close(); |
185 |
_text_writer.SetLogText(string.Format("{0}{1}",Encoding.GetString(ms.ToArray()),System.Environment.NewLine)); |
186 |
} |
187 |
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)); } |
188 |
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)); } |
189 |
#endregion |
190 |
} |
191 |
#endregion |
192 |
|
193 |
private void autoscroll_timer_Tick(object sender, EventArgs e) |
194 |
{ |
195 |
//try |
196 |
//{ |
197 |
// if (this.DesignMode) { return; } |
198 |
// AUTO_SCROLL = true; |
199 |
// ////txtLog.ScrollToEnd(); |
200 |
// //txtLog.SelectionStart = txtLog.Text.Length - 100; |
201 |
// //txtLog.ScrollToCaret(); |
202 |
// //txtLog.Refresh(); |
203 |
|
204 |
// //txtLog.SelectionStart = 0; |
205 |
// //txtLog.ScrollToCaret(); |
206 |
// //txtLog.Refresh(); |
207 |
|
208 |
// //int last_line_position = -1; |
209 |
// //int position = -1; |
210 |
// int len = txtLog.Text.Length; |
211 |
// //StringReader sr = new StringReader(this.StreamToString()); |
212 |
// //string line = ""; |
213 |
// //while ((line = sr.ReadLine()) != null) |
214 |
// //{ |
215 |
// // position += (line.Length * 2); |
216 |
// // last_line_position = (line.Length * 2); |
217 |
// // //if (line.EndsWith("/n")) |
218 |
// // //{ |
219 |
// // // position++; |
220 |
// // //} |
221 |
// // //logger.VerboseDebug.WriteLine("current line: {0}", line); |
222 |
// //} |
223 |
// txtLog.SelectionStart = len; |
224 |
// txtLog.ScrollToCaret(); |
225 |
// txtLog.Refresh(); |
226 |
// //autoscroll_timer.Enabled = false; |
227 |
// FIRST_LOAD = false; |
228 |
//} |
229 |
//catch { } |
230 |
|
231 |
} |
232 |
|
233 |
private void logupdater_DoWork(object sender, DoWorkEventArgs e) |
234 |
{ |
235 |
string message = e.Argument.ToString(); |
236 |
SetLogText(string.Format("{0}", message)); |
237 |
} |
238 |
|
239 |
private void logupdater_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) |
240 |
{ |
241 |
|
242 |
} |
243 |
|
244 |
private void LogWriter_Load(object sender, EventArgs e) |
245 |
{ |
246 |
txtLog.SelectionStart = 0; |
247 |
txtLog.ScrollToCaret(); |
248 |
} |
249 |
|
250 |
private void chkAutoScroll_CheckedChanged(object sender, EventArgs e) |
251 |
{ |
252 |
this.AutoScroll = chkAutoScroll.Checked; |
253 |
} |
254 |
} |
255 |
} |
256 |
|