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 = string.Format(@"{0}\{1}", Application.StartupPath, 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 |
|
54 |
|
55 |
private bool _RedirectConsoleOutput; |
56 |
public bool RedirectConsoleOutput |
57 |
{ |
58 |
get { return _RedirectConsoleOutput; } |
59 |
set |
60 |
{ |
61 |
_RedirectConsoleOutput = value; |
62 |
if (value) { Console.SetOut(this.Log); } |
63 |
else |
64 |
{ |
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 |
Console.SetOut(writer); |
74 |
} |
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 |
FileStream fs = new FileStream(LOG_PATH, FileMode.Append, FileAccess.Write, FileShare.ReadWrite); |
140 |
StreamWriter writer = new StreamWriter(fs); |
141 |
writer.AutoFlush = true; |
142 |
if (value == Log.NewLine) { writer.Write(System.Environment.NewLine); } |
143 |
else { writer.Write(value); } |
144 |
writer.Close(); |
145 |
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 |
public void CreateNewLog(bool delete) |
159 |
{ |
160 |
if (delete) |
161 |
{ |
162 |
FileInfo fi = new FileInfo(LOG_PATH); |
163 |
if (fi.Exists) |
164 |
fi.Delete(); |
165 |
} |
166 |
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 |
} |
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 |
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 |
#endregion |
200 |
} |
201 |
#endregion |
202 |
} |
203 |
} |