ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/RomCheater/trunk/RomCheater.Logging/LogWriter.cs
Revision: 18
Committed: Wed May 9 08:16:18 2012 UTC (11 years, 7 months ago) by william
File size: 6876 byte(s)
Log Message:
+ add support to write log to file (when a message is logged)

File Contents

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