10 |
using System.IO; |
using System.IO; |
11 |
using System.Reflection; |
using System.Reflection; |
12 |
using System.Security; |
using System.Security; |
13 |
|
using System.Threading; |
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); } } |
|
|
|
|
private delegate string OnGetLogText(); |
|
|
private delegate void OnSetLogText(string value); |
|
|
private delegate void GenericVoidDelegate(); |
|
|
|
|
|
//private OnGetLogText HandleGetLogText = null; |
|
|
private OnSetLogText HandleSetLogText = null; |
|
|
private GenericVoidDelegate delegate_EndLogUpdate = null; |
|
|
|
|
20 |
public LogWriter() : this(false) { } |
public LogWriter() : this(false) { } |
21 |
public LogWriter(bool redirectConsole) |
public LogWriter(bool redirectConsole) |
22 |
{ |
{ |
23 |
InitializeComponent(); |
InitializeComponent(); |
24 |
this.Log = new LogStream(this); |
this.Log = new LogStream(this); |
|
//HandleGetLogText += new OnGetLogText(GetLogText); |
|
|
HandleSetLogText += new OnSetLogText(SetLogText); |
|
|
|
|
|
//delegate_EndLogUpdate += new GenericVoidDelegate(EndLogUpdate); |
|
|
|
|
25 |
this.RedirectConsoleOutput = redirectConsole; |
this.RedirectConsoleOutput = redirectConsole; |
26 |
toolTip1.SetToolTip(btnClearLog, "Clears the Log"); |
toolTip1.SetToolTip(btnClearLog, "Clears the Log"); |
27 |
toolTip1.SetToolTip(btnCopyLogToClipboard, "Copies the Log to the Clipboard"); |
toolTip1.SetToolTip(btnCopyLogToClipboard, "Copies the Log to the Clipboard"); |
|
|
|
|
//ms = new MemoryStream(); |
|
|
//sw = new StreamWriter(ms); |
|
|
//sw.AutoFlush = true; |
|
|
//sr = new StreamReader(ms); |
|
|
|
|
28 |
chkAutoScroll.Checked = this.AutoScroll; |
chkAutoScroll.Checked = this.AutoScroll; |
29 |
} |
} |
30 |
private LogStream _Log; |
private LogStream _Log; |
31 |
public LogStream Log { get { return _Log; } private set { _Log = value; } } |
public LogStream Log { get { return _Log; } private set { _Log = value; } } |
|
|
|
|
|
|
|
//private MemoryStream ms; |
|
|
//private StreamWriter sw; |
|
|
//private StreamReader sr; |
|
32 |
private bool _AutoScroll; |
private bool _AutoScroll; |
33 |
new public bool AutoScroll |
new public bool AutoScroll |
34 |
{ |
{ |
43 |
if (AutoScroll && !value) |
if (AutoScroll && !value) |
44 |
chkAutoScroll.Checked = false; |
chkAutoScroll.Checked = false; |
45 |
_AutoScroll = value; |
_AutoScroll = value; |
|
autoscroll_timer.Enabled = value; |
|
46 |
} |
} |
47 |
} |
} |
48 |
|
|
71 |
|
|
72 |
private void btnClearLog_Click(object sender, EventArgs e) { this.Clear(true); } |
private void btnClearLog_Click(object sender, EventArgs e) { this.Clear(true); } |
73 |
private void btnCopyLogToClipboard_Click(object sender, EventArgs e) { Clipboard.SetText(txtLog.Text); } |
private void btnCopyLogToClipboard_Click(object sender, EventArgs e) { Clipboard.SetText(txtLog.Text); } |
|
|
|
|
|
|
|
private string PreserveLineEndings(string text) { return text.Replace("\n", System.Environment.NewLine); } |
|
|
|
|
|
|
|
|
//private bool BeginUpdate; |
|
|
//public void BeginLogUpdate() |
|
|
//{ |
|
|
// BeginUpdate = true; |
|
|
//} |
|
|
//public void EndLogUpdate() |
|
|
//{ |
|
|
// if (txtLog.InvokeRequired) |
|
|
// { |
|
|
// this.Invoke(delegate_EndLogUpdate); |
|
|
// } |
|
|
// else |
|
|
// { |
|
|
// StringReader _sr = new StringReader(GetLogText()); |
|
|
// List<string> lines = new List<string>(); |
|
|
// string line = ""; |
|
|
// while ((line = _sr.ReadLine()) != null) |
|
|
// { |
|
|
// lines.Add(line); |
|
|
// } |
|
|
// txtLog.Lines = lines.ToArray(); |
|
|
// BeginUpdate = false; |
|
|
// } |
|
|
//} |
|
|
//private string StreamToString() |
|
|
//{ |
|
|
// try |
|
|
// { |
|
|
// string value = ""; |
|
|
// sr = new StreamReader(ms); |
|
|
// sr.BaseStream.Seek(0, SeekOrigin.Begin); |
|
|
// value = sr.ReadToEnd(); |
|
|
// return value; |
|
|
// } |
|
|
// catch (Exception ex) |
|
|
// { |
|
|
// return ""; |
|
|
// } |
|
|
//} |
|
|
//public string GetLogText() |
|
|
//{ |
|
|
// if (txtLog.InvokeRequired) |
|
|
// { |
|
|
// return (string)this.Invoke(HandleGetLogText, new object[] { }); |
|
|
// } |
|
|
// else |
|
|
// { |
|
|
// string text = PreserveLineEndings(StreamToString()); |
|
|
// StringReader _sr = new StringReader(GetLogText()); |
|
|
// List<string> lines = new List<string>(); |
|
|
// string line = ""; |
|
|
// while ((line = _sr.ReadLine()) != null) |
|
|
// { |
|
|
// lines.Add(line); |
|
|
// } |
|
|
// txtLog.Lines = lines.ToArray(); |
|
|
// return text; |
|
|
// } |
|
|
//} |
|
74 |
public void SetLogText(string value) |
public void SetLogText(string value) |
75 |
{ |
{ |
76 |
FileStream fs = new FileStream(LOG_PATH, FileMode.Append, FileAccess.Write, FileShare.ReadWrite); |
FileStream fs = new FileStream(LOG_PATH, FileMode.Append, FileAccess.Write, FileShare.ReadWrite); |
79 |
if (value == Log.NewLine) { writer.Write(System.Environment.NewLine); } |
if (value == Log.NewLine) { writer.Write(System.Environment.NewLine); } |
80 |
else { writer.Write(value); } |
else { writer.Write(value); } |
81 |
writer.Close(); |
writer.Close(); |
|
//string text = value.Replace(System.Environment.NewLine,""); |
|
|
//string text = value.Replace(System.Environment.NewLine, Log.NewLine); |
|
|
////if (text.StartsWith(Log.NewLine)) |
|
|
////{ |
|
|
//// text = text.Remove(0, Log.NewLine.Length); |
|
|
////} |
|
|
//sw.Write(text); |
|
|
//Application.DoEvents(); |
|
82 |
txtLog.Text += value; |
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 |
new public string Text |
121 |
#if ALLOW_LOG_CLEARING |
#if ALLOW_LOG_CLEARING |
122 |
allow_log_clearing = true; |
allow_log_clearing = true; |
123 |
#endif |
#endif |
124 |
if (force || allow_log_clearing) |
if (force || allow_log_clearing) { txtLog.Clear(); } |
|
{ |
|
|
|
|
|
txtLog.Clear(); |
|
|
//ms = new MemoryStream(); |
|
|
//sw = new StreamWriter(ms); |
|
|
//sw.AutoFlush = true; |
|
|
//sr = new StreamReader(ms); |
|
|
} |
|
125 |
} |
} |
126 |
#region sub-classes |
#region sub-classes |
127 |
public class LogStream : TextWriter |
public class LogStream : TextWriter |
130 |
public LogStream() : this(null) { } |
public LogStream() : this(null) { } |
131 |
public LogStream(LogWriter text) : base() { _text_writer = text; this.NewLine = "\n"; } |
public LogStream(LogWriter text) : base() { _text_writer = text; this.NewLine = "\n"; } |
132 |
private LogWriter _text_writer; |
private LogWriter _text_writer; |
|
//private void _write(string message) |
|
|
//{ |
|
|
// if (_text_writer == null) return; |
|
|
|
|
|
// if (message.EndsWith("\n")) |
|
|
// { |
|
|
// //_text_writer.SetLogText(string.Format("{0}", strMessage.ToString())); |
|
|
// while (_text_writer.logupdater.IsBusy) |
|
|
// { |
|
|
// Application.DoEvents(); |
|
|
// } |
|
|
// _text_writer.logupdater.RunWorkerAsync((string.Format("{0}", strMessage.ToString()))); |
|
|
// strMessage = new StringBuilder(); |
|
|
// } |
|
|
// strMessage.Append(message); |
|
|
//} |
|
133 |
#region Overriden Methods |
#region Overriden Methods |
134 |
public override Encoding Encoding { get { return Encoding.UTF8; } } |
public override Encoding Encoding { get { return Encoding.UTF8; } } |
|
|
|
135 |
public override void Write(bool value) { _text_writer.SetLogText(value.ToString()); } |
public override void Write(bool value) { _text_writer.SetLogText(value.ToString()); } |
136 |
public override void Write(char value) { _text_writer.SetLogText(value.ToString()); } |
public override void Write(char value) { _text_writer.SetLogText(value.ToString()); } |
137 |
public override void Write(char[] buffer) { _text_writer.SetLogText(new string(buffer)); } |
public override void Write(char[] buffer) { _text_writer.SetLogText(new string(buffer)); } |
192 |
|
|
193 |
private void autoscroll_timer_Tick(object sender, EventArgs e) |
private void autoscroll_timer_Tick(object sender, EventArgs e) |
194 |
{ |
{ |
195 |
try |
//try |
196 |
{ |
//{ |
197 |
if (this.DesignMode) { return; } |
// if (this.DesignMode) { return; } |
198 |
////txtLog.ScrollToEnd(); |
// AUTO_SCROLL = true; |
199 |
//txtLog.SelectionStart = txtLog.Text.Length - 100; |
// ////txtLog.ScrollToEnd(); |
200 |
//txtLog.ScrollToCaret(); |
// //txtLog.SelectionStart = txtLog.Text.Length - 100; |
201 |
//txtLog.Refresh(); |
// //txtLog.ScrollToCaret(); |
202 |
|
// //txtLog.Refresh(); |
203 |
//txtLog.SelectionStart = 0; |
|
204 |
//txtLog.ScrollToCaret(); |
// //txtLog.SelectionStart = 0; |
205 |
//txtLog.Refresh(); |
// //txtLog.ScrollToCaret(); |
206 |
|
// //txtLog.Refresh(); |
207 |
//int last_line_position = -1; |
|
208 |
//int position = -1; |
// //int last_line_position = -1; |
209 |
int len = txtLog.Text.Length; |
// //int position = -1; |
210 |
//StringReader sr = new StringReader(this.StreamToString()); |
// int len = txtLog.Text.Length; |
211 |
//string line = ""; |
// //StringReader sr = new StringReader(this.StreamToString()); |
212 |
//while ((line = sr.ReadLine()) != null) |
// //string line = ""; |
213 |
//{ |
// //while ((line = sr.ReadLine()) != null) |
214 |
// position += (line.Length * 2); |
// //{ |
215 |
// last_line_position = (line.Length * 2); |
// // position += (line.Length * 2); |
216 |
// //if (line.EndsWith("/n")) |
// // last_line_position = (line.Length * 2); |
217 |
// //{ |
// // //if (line.EndsWith("/n")) |
218 |
// // position++; |
// // //{ |
219 |
// //} |
// // // position++; |
220 |
// //logger.VerboseDebug.WriteLine("current line: {0}", line); |
// // //} |
221 |
//} |
// // //logger.VerboseDebug.WriteLine("current line: {0}", line); |
222 |
txtLog.SelectionStart = len; |
// //} |
223 |
txtLog.ScrollToCaret(); |
// txtLog.SelectionStart = len; |
224 |
txtLog.Refresh(); |
// txtLog.ScrollToCaret(); |
225 |
//autoscroll_timer.Enabled = false; |
// txtLog.Refresh(); |
226 |
} |
// //autoscroll_timer.Enabled = false; |
227 |
catch { } |
// FIRST_LOAD = false; |
228 |
|
//} |
229 |
|
//catch { } |
230 |
|
|
231 |
} |
} |
232 |
|
|