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); } } |
19 |
– |
|
20 |
– |
private delegate string OnGetLogText(); |
21 |
– |
private delegate void OnSetLogText(string value); |
22 |
– |
private delegate void GenericVoidDelegate(); |
23 |
– |
|
24 |
– |
//private OnGetLogText HandleGetLogText = null; |
25 |
– |
private OnSetLogText HandleSetLogText = null; |
26 |
– |
private GenericVoidDelegate delegate_EndLogUpdate = null; |
27 |
– |
|
20 |
|
public LogWriter() : this(false) { } |
21 |
|
public LogWriter(bool redirectConsole) |
22 |
|
{ |
23 |
|
InitializeComponent(); |
24 |
|
this.Log = new LogStream(this); |
33 |
– |
//HandleGetLogText += new OnGetLogText(GetLogText); |
34 |
– |
HandleSetLogText += new OnSetLogText(SetLogText); |
35 |
– |
|
36 |
– |
//delegate_EndLogUpdate += new GenericVoidDelegate(EndLogUpdate); |
37 |
– |
|
25 |
|
this.RedirectConsoleOutput = redirectConsole; |
26 |
|
toolTip1.SetToolTip(btnClearLog, "Clears the Log"); |
27 |
|
toolTip1.SetToolTip(btnCopyLogToClipboard, "Copies the Log to the Clipboard"); |
41 |
– |
|
42 |
– |
//ms = new MemoryStream(); |
43 |
– |
//sw = new StreamWriter(ms); |
44 |
– |
//sw.AutoFlush = true; |
45 |
– |
//sr = new StreamReader(ms); |
46 |
– |
|
28 |
|
chkAutoScroll.Checked = this.AutoScroll; |
29 |
|
} |
30 |
|
private LogStream _Log; |
31 |
|
public LogStream Log { get { return _Log; } private set { _Log = value; } } |
51 |
– |
|
52 |
– |
|
53 |
– |
//private MemoryStream ms; |
54 |
– |
//private StreamWriter sw; |
55 |
– |
//private StreamReader sr; |
32 |
|
private bool _AutoScroll; |
33 |
|
new public bool AutoScroll |
34 |
|
{ |
43 |
|
if (AutoScroll && !value) |
44 |
|
chkAutoScroll.Checked = false; |
45 |
|
_AutoScroll = value; |
70 |
– |
autoscroll_timer.Enabled = value; |
46 |
|
} |
47 |
|
} |
48 |
|
|
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); } |
99 |
– |
|
100 |
– |
|
101 |
– |
private string PreserveLineEndings(string text) { return text.Replace("\n", System.Environment.NewLine); } |
102 |
– |
|
103 |
– |
|
104 |
– |
//private bool BeginUpdate; |
105 |
– |
//public void BeginLogUpdate() |
106 |
– |
//{ |
107 |
– |
// BeginUpdate = true; |
108 |
– |
//} |
109 |
– |
//public void EndLogUpdate() |
110 |
– |
//{ |
111 |
– |
// if (txtLog.InvokeRequired) |
112 |
– |
// { |
113 |
– |
// this.Invoke(delegate_EndLogUpdate); |
114 |
– |
// } |
115 |
– |
// else |
116 |
– |
// { |
117 |
– |
// StringReader _sr = new StringReader(GetLogText()); |
118 |
– |
// List<string> lines = new List<string>(); |
119 |
– |
// string line = ""; |
120 |
– |
// while ((line = _sr.ReadLine()) != null) |
121 |
– |
// { |
122 |
– |
// lines.Add(line); |
123 |
– |
// } |
124 |
– |
// txtLog.Lines = lines.ToArray(); |
125 |
– |
// BeginUpdate = false; |
126 |
– |
// } |
127 |
– |
//} |
128 |
– |
//private string StreamToString() |
129 |
– |
//{ |
130 |
– |
// try |
131 |
– |
// { |
132 |
– |
// string value = ""; |
133 |
– |
// sr = new StreamReader(ms); |
134 |
– |
// sr.BaseStream.Seek(0, SeekOrigin.Begin); |
135 |
– |
// value = sr.ReadToEnd(); |
136 |
– |
// return value; |
137 |
– |
// } |
138 |
– |
// catch (Exception ex) |
139 |
– |
// { |
140 |
– |
// return ""; |
141 |
– |
// } |
142 |
– |
//} |
143 |
– |
//public string GetLogText() |
144 |
– |
//{ |
145 |
– |
// if (txtLog.InvokeRequired) |
146 |
– |
// { |
147 |
– |
// return (string)this.Invoke(HandleGetLogText, new object[] { }); |
148 |
– |
// } |
149 |
– |
// else |
150 |
– |
// { |
151 |
– |
// string text = PreserveLineEndings(StreamToString()); |
152 |
– |
// StringReader _sr = new StringReader(GetLogText()); |
153 |
– |
// List<string> lines = new List<string>(); |
154 |
– |
// string line = ""; |
155 |
– |
// while ((line = _sr.ReadLine()) != null) |
156 |
– |
// { |
157 |
– |
// lines.Add(line); |
158 |
– |
// } |
159 |
– |
// txtLog.Lines = lines.ToArray(); |
160 |
– |
// return text; |
161 |
– |
// } |
162 |
– |
//} |
74 |
|
public void SetLogText(string value) |
75 |
|
{ |
76 |
|
FileStream fs = new FileStream(LOG_PATH, FileMode.Append, FileAccess.Write, FileShare.ReadWrite); |
79 |
|
if (value == Log.NewLine) { writer.Write(System.Environment.NewLine); } |
80 |
|
else { writer.Write(value); } |
81 |
|
writer.Close(); |
171 |
– |
//string text = value.Replace(System.Environment.NewLine,""); |
172 |
– |
//string text = value.Replace(System.Environment.NewLine, Log.NewLine); |
173 |
– |
////if (text.StartsWith(Log.NewLine)) |
174 |
– |
////{ |
175 |
– |
//// text = text.Remove(0, Log.NewLine.Length); |
176 |
– |
////} |
177 |
– |
//sw.Write(text); |
178 |
– |
//Application.DoEvents(); |
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 |
121 |
|
#if ALLOW_LOG_CLEARING |
122 |
|
allow_log_clearing = true; |
123 |
|
#endif |
124 |
< |
if (force || allow_log_clearing) |
214 |
< |
{ |
215 |
< |
|
216 |
< |
txtLog.Clear(); |
217 |
< |
//ms = new MemoryStream(); |
218 |
< |
//sw = new StreamWriter(ms); |
219 |
< |
//sw.AutoFlush = true; |
220 |
< |
//sr = new StreamReader(ms); |
221 |
< |
} |
124 |
> |
if (force || allow_log_clearing) { txtLog.Clear(); } |
125 |
|
} |
126 |
|
#region sub-classes |
127 |
|
public class LogStream : TextWriter |
130 |
|
public LogStream() : this(null) { } |
131 |
|
public LogStream(LogWriter text) : base() { _text_writer = text; this.NewLine = "\n"; } |
132 |
|
private LogWriter _text_writer; |
230 |
– |
//private void _write(string message) |
231 |
– |
//{ |
232 |
– |
// if (_text_writer == null) return; |
233 |
– |
|
234 |
– |
// if (message.EndsWith("\n")) |
235 |
– |
// { |
236 |
– |
// //_text_writer.SetLogText(string.Format("{0}", strMessage.ToString())); |
237 |
– |
// while (_text_writer.logupdater.IsBusy) |
238 |
– |
// { |
239 |
– |
// Application.DoEvents(); |
240 |
– |
// } |
241 |
– |
// _text_writer.logupdater.RunWorkerAsync((string.Format("{0}", strMessage.ToString()))); |
242 |
– |
// strMessage = new StringBuilder(); |
243 |
– |
// } |
244 |
– |
// strMessage.Append(message); |
245 |
– |
//} |
133 |
|
#region Overriden Methods |
134 |
|
public override Encoding Encoding { get { return Encoding.UTF8; } } |
248 |
– |
|
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)); } |
192 |
|
|
193 |
|
private void autoscroll_timer_Tick(object sender, EventArgs e) |
194 |
|
{ |
195 |
< |
try |
196 |
< |
{ |
197 |
< |
if (this.DesignMode) { return; } |
198 |
< |
////txtLog.ScrollToEnd(); |
199 |
< |
//txtLog.SelectionStart = txtLog.Text.Length - 100; |
200 |
< |
//txtLog.ScrollToCaret(); |
201 |
< |
//txtLog.Refresh(); |
202 |
< |
|
203 |
< |
//txtLog.SelectionStart = 0; |
204 |
< |
//txtLog.ScrollToCaret(); |
205 |
< |
//txtLog.Refresh(); |
206 |
< |
|
207 |
< |
//int last_line_position = -1; |
208 |
< |
//int position = -1; |
209 |
< |
int len = txtLog.Text.Length; |
210 |
< |
//StringReader sr = new StringReader(this.StreamToString()); |
211 |
< |
//string line = ""; |
212 |
< |
//while ((line = sr.ReadLine()) != null) |
213 |
< |
//{ |
214 |
< |
// position += (line.Length * 2); |
215 |
< |
// last_line_position = (line.Length * 2); |
216 |
< |
// //if (line.EndsWith("/n")) |
217 |
< |
// //{ |
218 |
< |
// // position++; |
219 |
< |
// //} |
220 |
< |
// //logger.VerboseDebug.WriteLine("current line: {0}", line); |
221 |
< |
//} |
222 |
< |
txtLog.SelectionStart = len; |
223 |
< |
txtLog.ScrollToCaret(); |
224 |
< |
txtLog.Refresh(); |
225 |
< |
//autoscroll_timer.Enabled = false; |
226 |
< |
} |
227 |
< |
catch { } |
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 |
|
|