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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 22 - (hide annotations) (download)
Wed May 9 08:43:52 2012 UTC (8 years, 9 months ago) by william
File size: 7021 byte(s)
+ don't delete log file in contructor
moved to method CreateNewLog()

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 william 20 public const string LOG_FILE = "RomCheater.log";
17 william 18 private static string LOG_PATH = string.Format(@"{0}\{1}", Application.StartupPath, LOG_FILE);
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 22 public void CreateNewLog()
159     {
160     FileInfo fi = new FileInfo(LOG_PATH);
161     if (fi.Exists)
162     fi.Delete();
163     }
164 william 18 public void Clear()
165 william 17 {
166     Clear(false);
167     }
168     private void Clear(bool force)
169     {
170     bool allow_log_clearing = false;
171     #if ALLOW_LOG_CLEARING
172     allow_log_clearing = true;
173     #endif
174     if (force || allow_log_clearing)
175     {
176 william 18
177 william 17 txtLog.Clear();
178     ms = new MemoryStream();
179     sw = new StreamWriter(ms);
180     sw.AutoFlush = true;
181     sr = new StreamReader(ms);
182     }
183     }
184     #region sub-classes
185     public class LogStream : TextWriter
186 william 18 {
187 william 17 public LogStream() : this(null) { }
188     public LogStream(LogWriter text) : base() { _text_writer = text; this.NewLine = "\n"; }
189     private LogWriter _text_writer;
190     private void _write(string message) { if (_text_writer == null) return; _text_writer.SetLogText(string.Format("{0}", message)); }
191     #region Overriden Methods
192     public override Encoding Encoding { get { return Encoding.UTF8; } }
193     public override void Write(char value) { base.Write(value); _write(value.ToString()); if (!_text_writer.BeginUpdate) _text_writer.EndLogUpdate(); }
194 william 18 #endregion
195 william 17 }
196 william 18 #endregion
197 william 17 }
198     }

  ViewVC Help
Powered by ViewVC 1.1.22