#define ALLOW_LOG_CLEARING // when defined will allow the log to be cleared using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Linq; using System.Text; using System.Windows.Forms; using System.IO; using System.Reflection; using System.Security; using System.Threading; namespace RomCheater.Logging { public partial class LogWriter : UserControl { private static string LOG_PATH { get { return string.Format(@"{0}\{1}", typeof(LogWriter).Assembly.Location.Replace("RomCheater.Logging.dll", ""), LoggingConstants.AppLogFile); } } private delegate void OnSetLogText(string value, bool invoked); private OnSetLogText HandleSetLogText = null; public LogWriter() : this(false) { } public LogWriter(bool redirectConsole) { InitializeComponent(); HandleSetLogText += new OnSetLogText(SetLogText); this.Log = new LogStream(this); this.RedirectConsoleOutput = redirectConsole; toolTip1.SetToolTip(btnClearLog, "Clears the Log"); toolTip1.SetToolTip(btnCopyLogToClipboard, "Copies the Log to the Clipboard"); chkAutoScroll.Checked = this.AutoScroll; } private LogStream _Log; public LogStream Log { get { return _Log; } private set { _Log = value; } } private bool _AutoScroll; new public bool AutoScroll { get { return _AutoScroll; } set { if (!_AutoScroll) chkAutoScroll.Checked = true; if (AutoScroll && !value) chkAutoScroll.Checked = false; _AutoScroll = value; } } private bool _RedirectConsoleOutput; public bool RedirectConsoleOutput { get { return _RedirectConsoleOutput; } set { _RedirectConsoleOutput = value; if (value) { Console.SetOut(this.Log); } else { Stream stream = Console.OpenStandardOutput(0x100); TextWriter writer = null; if (stream == Stream.Null) { writer = TextWriter.Synchronized(StreamWriter.Null); } Encoding encoding = this.Log.Encoding; StreamWriter writer2 = new StreamWriter(stream, encoding, 0x100); //writer2.HaveWrittenPreamble = true; writer2.AutoFlush = true; writer = TextWriter.Synchronized(writer2); Console.SetOut(writer); } } } private void btnClearLog_Click(object sender, EventArgs e) { this.Clear(true); } private void btnCopyLogToClipboard_Click(object sender, EventArgs e) { Clipboard.SetText(txtLog.Text); } public void WriteLogToFile(string value) { FileStream fs = new FileStream(LOG_PATH, FileMode.Append, FileAccess.Write, FileShare.ReadWrite); StreamWriter writer = new StreamWriter(fs); writer.AutoFlush = true; if (value == Log.NewLine) { writer.Write(System.Environment.NewLine); } else { writer.Write(value); } writer.Close(); } public void SetLogText(string value) { SetLogText(value, false); } public void SetLogText(string value, bool invoked) { if (!invoked) { WriteLogToFile(value); } if (txtLog.InvokeRequired) { this.Invoke(HandleSetLogText, new object[] { value, true }); } else { txtLog.Text += value; if (AutoScroll) { txtLog.SelectionStart = txtLog.TextLength; txtLog.ScrollToCaret(); txtLog.Refresh(); } } Thread.Sleep(100); } new public string Text { get { throw new InvalidOperationException("Please use GetLogText()"); } private set { throw new InvalidOperationException("Please use SetLogText(string value)"); } } public void CreateNewLog(bool delete) { if (delete) { FileInfo fi = new FileInfo(LOG_PATH); if (fi.Exists) fi.Delete(); } Log.WriteLine("{0} ({2} v{3} {4}) created on {1}", LoggingConstants.AppLogFile, DateTime.Now.ToString(), LoggingConstants.AppName, LoggingConstants.AppVersion, LoggingConstants.AppBuild); } public void Clear() { Clear(false); } private void Clear(bool force) { bool allow_log_clearing = false; #if ALLOW_LOG_CLEARING allow_log_clearing = true; #endif if (force || allow_log_clearing) { txtLog.Clear(); } } #region sub-classes public class LogStream : TextWriter { private StringBuilder strMessage = new StringBuilder(); public LogStream() : this(null) { } public LogStream(LogWriter text) : base() { _text_writer = text; this.NewLine = "\n"; } private LogWriter _text_writer; #region Overriden Methods public override Encoding Encoding { get { return Encoding.UTF8; } } 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 private void autoscroll_timer_Tick(object sender, EventArgs e) { //try //{ // if (this.DesignMode) { return; } // AUTO_SCROLL = true; // ////txtLog.ScrollToEnd(); // //txtLog.SelectionStart = txtLog.Text.Length - 100; // //txtLog.ScrollToCaret(); // //txtLog.Refresh(); // //txtLog.SelectionStart = 0; // //txtLog.ScrollToCaret(); // //txtLog.Refresh(); // //int last_line_position = -1; // //int position = -1; // int len = txtLog.Text.Length; // //StringReader sr = new StringReader(this.StreamToString()); // //string line = ""; // //while ((line = sr.ReadLine()) != null) // //{ // // position += (line.Length * 2); // // last_line_position = (line.Length * 2); // // //if (line.EndsWith("/n")) // // //{ // // // position++; // // //} // // //logger.VerboseDebug.WriteLine("current line: {0}", line); // //} // txtLog.SelectionStart = len; // txtLog.ScrollToCaret(); // txtLog.Refresh(); // //autoscroll_timer.Enabled = false; // FIRST_LOAD = false; //} //catch { } } private void logupdater_DoWork(object sender, DoWorkEventArgs e) { string message = e.Argument.ToString(); SetLogText(string.Format("{0}", message)); } private void logupdater_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { } private void LogWriter_Load(object sender, EventArgs e) { txtLog.SelectionStart = 0; txtLog.ScrollToCaret(); } private void chkAutoScroll_CheckedChanged(object sender, EventArgs e) { this.AutoScroll = chkAutoScroll.Checked; } } }