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