/[RomCheater]/trunk/RomCheater.Logging/LogWriter.cs
ViewVC logotype

Annotation of /trunk/RomCheater.Logging/LogWriter.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 17 - (hide annotations) (download)
Wed May 9 08:02:18 2012 UTC (8 years, 8 months ago) by william
File size: 6374 byte(s)
implement basic logging support

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    
12     namespace RomCheater.Logging
13     {
14     public partial class LogWriter : UserControl
15     {
16     private delegate string OnGetLogText();
17     private delegate void OnSetLogText(string value);
18     private delegate void GenericVoidDelegate();
19    
20     private OnGetLogText HandleGetLogText = null;
21     private OnSetLogText HandleSetLogText = null;
22     private GenericVoidDelegate delegate_EndLogUpdate = null;
23    
24     public LogWriter() : this(false) { }
25     public LogWriter(bool redirectConsole)
26     {
27     InitializeComponent();
28     this.Log = new LogStream(this);
29     HandleGetLogText += new OnGetLogText(GetLogText);
30     HandleSetLogText += new OnSetLogText(SetLogText);
31    
32     delegate_EndLogUpdate += new GenericVoidDelegate(EndLogUpdate);
33    
34     this.RedirectConsoleOutput = redirectConsole;
35     toolTip1.SetToolTip(btnClearLog, "Clears the Log");
36     toolTip1.SetToolTip(btnCopyLogToClipboard, "Copies the Log to the Clipboard");
37     ms = new MemoryStream();
38     sw = new StreamWriter(ms);
39     sw.AutoFlush = true;
40     sr = new StreamReader(ms);
41     }
42     private LogStream _Log;
43     public LogStream Log { get { return _Log; } private set { _Log = value; } }
44    
45    
46     private MemoryStream ms;
47     private StreamWriter sw;
48     private StreamReader sr;
49    
50    
51     private bool _RedirectConsoleOutput;
52     public bool RedirectConsoleOutput
53     {
54     get { return _RedirectConsoleOutput; }
55     set
56     {
57     _RedirectConsoleOutput = value;
58     if (value) { Console.SetOut(this.Log); }
59     else
60     {
61     Stream stream = Console.OpenStandardOutput(0x100);
62     TextWriter writer = null;
63     if (stream == Stream.Null) { writer = TextWriter.Synchronized(StreamWriter.Null); }
64     Encoding encoding = this.Log.Encoding;
65     StreamWriter writer2 = new StreamWriter(stream, encoding, 0x100);
66     //writer2.HaveWrittenPreamble = true;
67     writer2.AutoFlush = true;
68     writer = TextWriter.Synchronized(writer2);
69     Console.SetOut(writer);
70     }
71     }
72     }
73    
74     private void btnClearLog_Click(object sender, EventArgs e) { this.Clear(true); }
75     private void btnCopyLogToClipboard_Click(object sender, EventArgs e) { Clipboard.SetText(GetLogText()); }
76    
77    
78     private string PreserveLineEndings(string text) { return text.Replace("\n", System.Environment.NewLine); }
79    
80    
81     private bool BeginUpdate;
82     public void BeginLogUpdate()
83     {
84     BeginUpdate = true;
85     }
86     public void EndLogUpdate()
87     {
88     if (txtLog.InvokeRequired)
89     {
90     this.Invoke(delegate_EndLogUpdate);
91     }
92     else
93     {
94     StringReader _sr = new StringReader(GetLogText());
95     List<string> lines = new List<string>();
96     string line = "";
97     while ((line = _sr.ReadLine()) != null)
98     {
99     lines.Add(line);
100     }
101     txtLog.Lines = lines.ToArray();
102     BeginUpdate = false;
103     }
104     }
105     private string StreamToString()
106     {
107     string value = "";
108     sr = new StreamReader(ms);
109     sr.BaseStream.Seek(0, SeekOrigin.Begin);
110     value = sr.ReadToEnd();
111     return value;
112     }
113     public string GetLogText()
114     {
115     //if (txtLog.InvokeRequired)
116     //{
117     // return (string)this.Invoke(HandleGetLogText, new object[] { });
118     //}
119     //else
120     //{
121     return PreserveLineEndings(StreamToString());
122     //}
123    
124     }
125     public void SetLogText(string value)
126     {
127     //if (txtLog.InvokeRequired)
128     //{
129     // this.Invoke(HandleSetLogText, new object[] { value });
130     //}
131     //else
132     //{
133     // txtLog.AppendText(value);
134     //}
135     sw.Write(value);
136     }
137     new public string Text
138     {
139     get
140     {
141     throw new InvalidOperationException("Please use GetLogText()");
142     }
143     private set
144     {
145     throw new InvalidOperationException("Please use SetLogText(string value)");
146     }
147     }
148    
149     public void Clear()
150     {
151     Clear(false);
152     }
153     private void Clear(bool force)
154     {
155     bool allow_log_clearing = false;
156     #if ALLOW_LOG_CLEARING
157     allow_log_clearing = true;
158     #endif
159     if (force || allow_log_clearing)
160     {
161    
162     txtLog.Clear();
163     ms = new MemoryStream();
164     sw = new StreamWriter(ms);
165     sw.AutoFlush = true;
166     sr = new StreamReader(ms);
167     }
168     }
169     #region sub-classes
170     public class LogStream : TextWriter
171     {
172     public LogStream() : this(null) { }
173     public LogStream(LogWriter text) : base() { _text_writer = text; this.NewLine = "\n"; }
174     private LogWriter _text_writer;
175     private void _write(string message) { if (_text_writer == null) return; _text_writer.SetLogText(string.Format("{0}", message)); }
176     #region Overriden Methods
177     public override Encoding Encoding { get { return Encoding.UTF8; } }
178     public override void Write(char value) { base.Write(value); _write(value.ToString()); if (!_text_writer.BeginUpdate) _text_writer.EndLogUpdate(); }
179     #endregion
180     }
181     #endregion
182     }
183     }

  ViewVC Help
Powered by ViewVC 1.1.22