1 |
william |
17 |
#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 |
william |
23 |
using System.Reflection; |
12 |
william |
17 |
|
13 |
|
|
namespace RomCheater.Logging |
14 |
|
|
{ |
15 |
|
|
public partial class LogWriter : UserControl |
16 |
|
|
{ |
17 |
william |
23 |
private static string LOG_PATH = string.Format(@"{0}\{1}", Application.StartupPath, LoggingConstants.AppLogFile); |
18 |
william |
18 |
|
19 |
william |
17 |
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 |
william |
18 |
|
41 |
william |
17 |
ms = new MemoryStream(); |
42 |
|
|
sw = new StreamWriter(ms); |
43 |
|
|
sw.AutoFlush = true; |
44 |
|
|
sr = new StreamReader(ms); |
45 |
|
|
} |
46 |
|
|
private LogStream _Log; |
47 |
william |
18 |
public LogStream Log { get { return _Log; } private set { _Log = value; } } |
48 |
william |
17 |
|
49 |
|
|
|
50 |
|
|
private MemoryStream ms; |
51 |
|
|
private StreamWriter sw; |
52 |
|
|
private StreamReader sr; |
53 |
|
|
|
54 |
|
|
|
55 |
|
|
private bool _RedirectConsoleOutput; |
56 |
|
|
public bool RedirectConsoleOutput |
57 |
|
|
{ |
58 |
|
|
get { return _RedirectConsoleOutput; } |
59 |
|
|
set |
60 |
|
|
{ |
61 |
|
|
_RedirectConsoleOutput = value; |
62 |
william |
18 |
if (value) { Console.SetOut(this.Log); } |
63 |
|
|
else |
64 |
william |
17 |
{ |
65 |
|
|
Stream stream = Console.OpenStandardOutput(0x100); |
66 |
|
|
TextWriter writer = null; |
67 |
|
|
if (stream == Stream.Null) { writer = TextWriter.Synchronized(StreamWriter.Null); } |
68 |
|
|
Encoding encoding = this.Log.Encoding; |
69 |
|
|
StreamWriter writer2 = new StreamWriter(stream, encoding, 0x100); |
70 |
|
|
//writer2.HaveWrittenPreamble = true; |
71 |
|
|
writer2.AutoFlush = true; |
72 |
|
|
writer = TextWriter.Synchronized(writer2); |
73 |
william |
18 |
Console.SetOut(writer); |
74 |
william |
17 |
} |
75 |
|
|
} |
76 |
|
|
} |
77 |
|
|
|
78 |
|
|
private void btnClearLog_Click(object sender, EventArgs e) { this.Clear(true); } |
79 |
|
|
private void btnCopyLogToClipboard_Click(object sender, EventArgs e) { Clipboard.SetText(GetLogText()); } |
80 |
|
|
|
81 |
|
|
|
82 |
|
|
private string PreserveLineEndings(string text) { return text.Replace("\n", System.Environment.NewLine); } |
83 |
|
|
|
84 |
|
|
|
85 |
|
|
private bool BeginUpdate; |
86 |
|
|
public void BeginLogUpdate() |
87 |
|
|
{ |
88 |
|
|
BeginUpdate = true; |
89 |
|
|
} |
90 |
|
|
public void EndLogUpdate() |
91 |
|
|
{ |
92 |
|
|
if (txtLog.InvokeRequired) |
93 |
|
|
{ |
94 |
|
|
this.Invoke(delegate_EndLogUpdate); |
95 |
|
|
} |
96 |
|
|
else |
97 |
|
|
{ |
98 |
|
|
StringReader _sr = new StringReader(GetLogText()); |
99 |
|
|
List<string> lines = new List<string>(); |
100 |
|
|
string line = ""; |
101 |
|
|
while ((line = _sr.ReadLine()) != null) |
102 |
|
|
{ |
103 |
|
|
lines.Add(line); |
104 |
|
|
} |
105 |
|
|
txtLog.Lines = lines.ToArray(); |
106 |
|
|
BeginUpdate = false; |
107 |
|
|
} |
108 |
|
|
} |
109 |
|
|
private string StreamToString() |
110 |
|
|
{ |
111 |
|
|
string value = ""; |
112 |
|
|
sr = new StreamReader(ms); |
113 |
|
|
sr.BaseStream.Seek(0, SeekOrigin.Begin); |
114 |
|
|
value = sr.ReadToEnd(); |
115 |
|
|
return value; |
116 |
|
|
} |
117 |
|
|
public string GetLogText() |
118 |
|
|
{ |
119 |
|
|
//if (txtLog.InvokeRequired) |
120 |
|
|
//{ |
121 |
|
|
// return (string)this.Invoke(HandleGetLogText, new object[] { }); |
122 |
|
|
//} |
123 |
|
|
//else |
124 |
|
|
//{ |
125 |
|
|
return PreserveLineEndings(StreamToString()); |
126 |
|
|
//} |
127 |
|
|
|
128 |
|
|
} |
129 |
|
|
public void SetLogText(string value) |
130 |
|
|
{ |
131 |
|
|
//if (txtLog.InvokeRequired) |
132 |
|
|
//{ |
133 |
|
|
// this.Invoke(HandleSetLogText, new object[] { value }); |
134 |
|
|
//} |
135 |
|
|
//else |
136 |
|
|
//{ |
137 |
|
|
// txtLog.AppendText(value); |
138 |
|
|
//} |
139 |
william |
18 |
FileStream fs = new FileStream(LOG_PATH, FileMode.Append, FileAccess.Write, FileShare.ReadWrite); |
140 |
|
|
StreamWriter writer = new StreamWriter(fs); |
141 |
|
|
writer.AutoFlush = true; |
142 |
william |
19 |
if (value == Log.NewLine) { writer.Write(System.Environment.NewLine); } |
143 |
|
|
else { writer.Write(value); } |
144 |
william |
18 |
writer.Close(); |
145 |
william |
17 |
sw.Write(value); |
146 |
|
|
} |
147 |
|
|
new public string Text |
148 |
|
|
{ |
149 |
|
|
get |
150 |
|
|
{ |
151 |
|
|
throw new InvalidOperationException("Please use GetLogText()"); |
152 |
|
|
} |
153 |
|
|
private set |
154 |
|
|
{ |
155 |
|
|
throw new InvalidOperationException("Please use SetLogText(string value)"); |
156 |
|
|
} |
157 |
|
|
} |
158 |
william |
23 |
public void CreateNewLog(bool delete) |
159 |
william |
22 |
{ |
160 |
william |
23 |
if (delete) |
161 |
|
|
{ |
162 |
|
|
FileInfo fi = new FileInfo(LOG_PATH); |
163 |
|
|
if (fi.Exists) |
164 |
|
|
fi.Delete(); |
165 |
|
|
} |
166 |
william |
26 |
Log.WriteLine("{0} ({2} v{3} {4}) created on {1}", LoggingConstants.AppLogFile, DateTime.Now.ToString(), LoggingConstants.AppName, LoggingConstants.AppVersion, LoggingConstants.AppBuild); |
167 |
|
|
logger.Debug.WriteLine("this is a debug message from logwriter"); |
168 |
william |
22 |
} |
169 |
william |
18 |
public void Clear() |
170 |
william |
17 |
{ |
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 |
william |
18 |
|
182 |
william |
17 |
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 |
william |
18 |
{ |
192 |
william |
17 |
public LogStream() : this(null) { } |
193 |
|
|
public LogStream(LogWriter text) : base() { _text_writer = text; this.NewLine = "\n"; } |
194 |
|
|
private LogWriter _text_writer; |
195 |
|
|
private void _write(string message) { if (_text_writer == null) return; _text_writer.SetLogText(string.Format("{0}", message)); } |
196 |
|
|
#region Overriden Methods |
197 |
|
|
public override Encoding Encoding { get { return Encoding.UTF8; } } |
198 |
|
|
public override void Write(char value) { base.Write(value); _write(value.ToString()); if (!_text_writer.BeginUpdate) _text_writer.EndLogUpdate(); } |
199 |
william |
18 |
#endregion |
200 |
william |
17 |
} |
201 |
william |
18 |
#endregion |
202 |
william |
17 |
} |
203 |
|
|
} |