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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

--- trunk/RomCheater.Logging/LogWriter.cs	2012/06/05 11:38:32	300
+++ trunk/RomCheater.Logging/LogWriter.cs	2012/06/05 13:27:07	301
@@ -9,6 +9,7 @@
 using System.Windows.Forms;
 using System.IO;
 using System.Reflection;
+using System.Security;
 
 namespace RomCheater.Logging
 {
@@ -20,7 +21,7 @@
         private delegate void OnSetLogText(string value);
         private delegate void GenericVoidDelegate();
 
-        private OnGetLogText HandleGetLogText = null;
+        //private OnGetLogText HandleGetLogText = null;
         private OnSetLogText HandleSetLogText = null;
         private GenericVoidDelegate delegate_EndLogUpdate = null;
 
@@ -29,27 +30,27 @@
         {
             InitializeComponent();
             this.Log = new LogStream(this);
-            HandleGetLogText += new OnGetLogText(GetLogText);
+            //HandleGetLogText += new OnGetLogText(GetLogText);
             HandleSetLogText += new OnSetLogText(SetLogText);
 
-            delegate_EndLogUpdate += new GenericVoidDelegate(EndLogUpdate);
+            //delegate_EndLogUpdate += new GenericVoidDelegate(EndLogUpdate);
 
             this.RedirectConsoleOutput = redirectConsole;
             toolTip1.SetToolTip(btnClearLog, "Clears the Log");
             toolTip1.SetToolTip(btnCopyLogToClipboard, "Copies the Log to the Clipboard");
 
-            ms = new MemoryStream();
-            sw = new StreamWriter(ms);
-            sw.AutoFlush = true;
-            sr = new StreamReader(ms);
+            //ms = new MemoryStream();
+            //sw = new StreamWriter(ms);
+            //sw.AutoFlush = true;
+            //sr = new StreamReader(ms);
         }
         private LogStream _Log;
         public LogStream Log { get { return _Log; } private set { _Log = value; } }
 
 
-        private MemoryStream ms;
-        private StreamWriter sw;
-        private StreamReader sr;
+        //private MemoryStream ms;
+        //private StreamWriter sw;
+        //private StreamReader sr;
         private bool _AutoScroll;
         new public bool AutoScroll { get { return _AutoScroll; } set { _AutoScroll = value; autoscroll_timer.Enabled = value; } }
 
@@ -77,63 +78,71 @@
         }
 
         private void btnClearLog_Click(object sender, EventArgs e) { this.Clear(true); }
-        private void btnCopyLogToClipboard_Click(object sender, EventArgs e) { Clipboard.SetText(GetLogText()); }
+        private void btnCopyLogToClipboard_Click(object sender, EventArgs e) { Clipboard.SetText(txtLog.Text); }
 
 
         private string PreserveLineEndings(string text) { return text.Replace("\n", System.Environment.NewLine); }
 
 
-        private bool BeginUpdate;
-        public void BeginLogUpdate()
-        {
-            BeginUpdate = true;
-        }
-        public void EndLogUpdate()
-        {
-            if (txtLog.InvokeRequired)
-            {
-                this.Invoke(delegate_EndLogUpdate);
-            }
-            else
-            {
-                StringReader _sr = new StringReader(GetLogText());
-                List<string> lines = new List<string>();
-                string line = "";
-                while ((line = _sr.ReadLine()) != null)
-                {
-                    lines.Add(line);
-                }
-                txtLog.Lines = lines.ToArray();
-                BeginUpdate = false;
-            }
-        }
-        private string StreamToString()
-        {
-            try
-            {
-                string value = "";
-                sr = new StreamReader(ms);
-                sr.BaseStream.Seek(0, SeekOrigin.Begin);
-                value = sr.ReadToEnd();
-                return value;
-            }
-            catch (Exception ex)
-            {
-                return "";
-            }
-        }
-        public string GetLogText()
-        {
-            //if (txtLog.InvokeRequired)
-            //{
-            //    return (string)this.Invoke(HandleGetLogText, new object[] { });
-            //}
-            //else
-            //{
-            return PreserveLineEndings(StreamToString());
-            //}
-
-        }
+        //private bool BeginUpdate;
+        //public void BeginLogUpdate()
+        //{
+        //    BeginUpdate = true;
+        //}
+        //public void EndLogUpdate()
+        //{
+        //    if (txtLog.InvokeRequired)
+        //    {
+        //        this.Invoke(delegate_EndLogUpdate);
+        //    }
+        //    else
+        //    {
+        //        StringReader _sr = new StringReader(GetLogText());
+        //        List<string> lines = new List<string>();
+        //        string line = "";
+        //        while ((line = _sr.ReadLine()) != null)
+        //        {
+        //            lines.Add(line);
+        //        }
+        //        txtLog.Lines = lines.ToArray();
+        //        BeginUpdate = false;
+        //    }
+        //}
+        //private string StreamToString()
+        //{
+        //    try
+        //    {
+        //        string value = "";
+        //        sr = new StreamReader(ms);
+        //        sr.BaseStream.Seek(0, SeekOrigin.Begin);
+        //        value = sr.ReadToEnd();
+        //        return value;
+        //    }
+        //    catch (Exception ex)
+        //    {
+        //        return "";
+        //    }
+        //}
+        //public string GetLogText()
+        //{
+        //    if (txtLog.InvokeRequired)
+        //    {
+        //        return (string)this.Invoke(HandleGetLogText, new object[] { });
+        //    }
+        //    else
+        //    {
+        //        string text = PreserveLineEndings(StreamToString());
+        //        StringReader _sr = new StringReader(GetLogText());
+        //        List<string> lines = new List<string>();
+        //        string line = "";
+        //        while ((line = _sr.ReadLine()) != null)
+        //        {
+        //            lines.Add(line);
+        //        }
+        //        txtLog.Lines = lines.ToArray();
+        //        return text;
+        //    }
+        //}
         public void SetLogText(string value)
         {
             FileStream fs = new FileStream(LOG_PATH, FileMode.Append, FileAccess.Write, FileShare.ReadWrite);
@@ -143,13 +152,14 @@
             else { writer.Write(value); }
             writer.Close();
             //string text = value.Replace(System.Environment.NewLine,"");
-            string text = value.Replace(System.Environment.NewLine, Log.NewLine);
-            //if (text.StartsWith(Log.NewLine))
-            //{
-            //    text = text.Remove(0, Log.NewLine.Length);
-            //}
-            sw.Write(text);
-            Application.DoEvents();
+            //string text = value.Replace(System.Environment.NewLine, Log.NewLine);
+            ////if (text.StartsWith(Log.NewLine))
+            ////{
+            ////    text = text.Remove(0, Log.NewLine.Length);
+            ////}
+            //sw.Write(text);
+            //Application.DoEvents();
+            txtLog.AppendText(value);
         }
 
         new public string Text
@@ -187,10 +197,10 @@
             {
 
                 txtLog.Clear();
-                ms = new MemoryStream();
-                sw = new StreamWriter(ms);
-                sw.AutoFlush = true;
-                sr = new StreamReader(ms);
+                //ms = new MemoryStream();
+                //sw = new StreamWriter(ms);
+                //sw.AutoFlush = true;
+                //sr = new StreamReader(ms);
             }
         }
         #region sub-classes
@@ -200,25 +210,79 @@
             public LogStream() : this(null) { }
             public LogStream(LogWriter text) : base() { _text_writer = text; this.NewLine = "\n"; }
             private LogWriter _text_writer;
-            private void _write(string message)
-            {
-                if (_text_writer == null) return;
+            //private void _write(string message)
+            //{
+            //    if (_text_writer == null) return;
 
-                if (message.EndsWith("\n"))
-                {
-                    //_text_writer.SetLogText(string.Format("{0}", strMessage.ToString()));
-                    while (_text_writer.logupdater.IsBusy)
-                    {
-                        Application.DoEvents();
-                    }
-                    _text_writer.logupdater.RunWorkerAsync((string.Format("{0}", strMessage.ToString())));
-                    strMessage = new StringBuilder();
-                }
-                strMessage.Append(message);
-            }
+            //    if (message.EndsWith("\n"))
+            //    {
+            //        //_text_writer.SetLogText(string.Format("{0}", strMessage.ToString()));
+            //        while (_text_writer.logupdater.IsBusy)
+            //        {
+            //            Application.DoEvents();
+            //        }
+            //        _text_writer.logupdater.RunWorkerAsync((string.Format("{0}", strMessage.ToString())));
+            //        strMessage = new StringBuilder();
+            //    }
+            //    strMessage.Append(message);
+            //}
             #region Overriden Methods
             public override Encoding Encoding { get { return Encoding.UTF8; } }
-            public override void Write(char value) { base.Write(value); _write(value.ToString()); if (!_text_writer.BeginUpdate) _text_writer.EndLogUpdate(); }
+
+            public override void Write(bool value) { _text_writer.SetLogText(value.ToString()); }
+            public override void Write(char value) { _text_writer.SetLogText(value.ToString()); }
+            public override void Write(char[] buffer) { _text_writer.SetLogText(new string(buffer)); }
+            public override void Write(decimal value) { _text_writer.SetLogText(value.ToString()); }
+            public override void Write(double value) { _text_writer.SetLogText(value.ToString()); }
+            public override void Write(float value) { _text_writer.SetLogText(value.ToString()); }
+            public override void Write(int value) { _text_writer.SetLogText(value.ToString()); }
+            public override void Write(long value) { _text_writer.SetLogText(value.ToString()); }
+            public override void Write(object value) { _text_writer.SetLogText(value.ToString()); }
+            public override void Write(string value) { _text_writer.SetLogText(value.ToString()); }
+            public override void Write(uint value) { _text_writer.SetLogText(value.ToString()); }
+            public override void Write(ulong value) { _text_writer.SetLogText(value.ToString()); }
+            public override void Write(string format, object arg0) { _text_writer.SetLogText(string.Format(format, arg0)); }
+            public override void Write(string format, params object[] arg) { _text_writer.SetLogText(string.Format(format, arg)); }
+            public override void Write(char[] buffer, int index, int count)
+            {
+                byte[] t = new byte[count];
+                MemoryStream ms = new MemoryStream();
+                StreamWriter sw = new StreamWriter(ms);
+                sw.AutoFlush = true;
+                sw.Write(buffer, index, count);
+                sw.Close();
+                _text_writer.SetLogText(Encoding.GetString(ms.ToArray()));
+            }
+            public override void Write(string format, object arg0, object arg1) { _text_writer.SetLogText(string.Format(format, arg0, arg1)); }
+            public override void Write(string format, object arg0, object arg1, object arg2) { _text_writer.SetLogText(string.Format(format, arg0, arg1, arg2)); }
+            public override void WriteLine() { _text_writer.SetLogText(System.Environment.NewLine); }
+            public override void WriteLine(bool value) { _text_writer.SetLogText(string.Format("{0}{1}", value.ToString(), System.Environment.NewLine)); }
+            public override void WriteLine(char value) { _text_writer.SetLogText(string.Format("{0}{1}", value.ToString(), System.Environment.NewLine)); }
+            public override void WriteLine(char[] buffer) { _text_writer.SetLogText(string.Format("{0}{1}", new string(buffer), System.Environment.NewLine)); }
+            public override void WriteLine(decimal value) { _text_writer.SetLogText(string.Format("{0}{1}", value.ToString(), System.Environment.NewLine)); }
+            public override void WriteLine(double value) { _text_writer.SetLogText(string.Format("{0}{1}", value.ToString(), System.Environment.NewLine)); }
+            public override void WriteLine(float value) { _text_writer.SetLogText(string.Format("{0}{1}", value.ToString(), System.Environment.NewLine)); }
+            public override void WriteLine(int value) { _text_writer.SetLogText(string.Format("{0}{1}", value.ToString(), System.Environment.NewLine)); }
+            public override void WriteLine(long value) { _text_writer.SetLogText(string.Format("{0}{1}", value.ToString(), System.Environment.NewLine)); }
+            public override void WriteLine(object value) { _text_writer.SetLogText(string.Format("{0}{1}", value.ToString(), System.Environment.NewLine)); }
+            [SecuritySafeCritical]
+            public override void WriteLine(string value) { _text_writer.SetLogText(string.Format("{0}{1}", value, System.Environment.NewLine)); }
+            public override void WriteLine(uint value) { _text_writer.SetLogText(string.Format("{0}{1}", value.ToString(), System.Environment.NewLine)); }
+            public override void WriteLine(ulong value) { _text_writer.SetLogText(string.Format("{0}{1}", value.ToString(), System.Environment.NewLine)); }
+            public override void WriteLine(string format, object arg0) { _text_writer.SetLogText(string.Format("{0}{1}", string.Format(format, arg0), System.Environment.NewLine)); }
+            public override void WriteLine(string format, params object[] arg) { _text_writer.SetLogText(string.Format("{0}{1}", string.Format(format, arg), System.Environment.NewLine)); }
+            public override void WriteLine(char[] buffer, int index, int count)
+            {
+                byte[] t = new byte[count];
+                MemoryStream ms = new MemoryStream();
+                StreamWriter sw = new StreamWriter(ms);
+                sw.AutoFlush = true;
+                sw.Write(buffer, index, count);
+                sw.Close();
+                _text_writer.SetLogText(string.Format("{0}{1}",Encoding.GetString(ms.ToArray()),System.Environment.NewLine));
+            }
+            public override void WriteLine(string format, object arg0, object arg1) { _text_writer.SetLogText(string.Format("{0}{1}", string.Format(format, arg0, arg1), System.Environment.NewLine)); }
+            public override void WriteLine(string format, object arg0, object arg1, object arg2) { _text_writer.SetLogText(string.Format("{0}{1}", string.Format(format, arg0, arg1, arg2), System.Environment.NewLine)); }
             #endregion
         }
         #endregion

 

  ViewVC Help
Powered by ViewVC 1.1.22