diff --git a/BizHawk.MultiClient/LogConsole.cs b/BizHawk.MultiClient/LogConsole.cs index 460d467d6d..80e7db806b 100644 --- a/BizHawk.MultiClient/LogConsole.cs +++ b/BizHawk.MultiClient/LogConsole.cs @@ -1,40 +1,110 @@ using System; +using System.Text; using System.IO; using System.Runtime.InteropServices; +//todo - quit using Console.WriteLine (well, we can leave it hooked up as a backstop) +//use a different method instead, so we can collect unicode data +//also, collect log data independently of whether the log window is open +//we also need to dice it into lines so that we can have a backlog policy + namespace BizHawk.MultiClient { static class LogConsole { - [DllImport("kernel32.dll", SetLastError = true)] - static extern bool AllocConsole(); - - [DllImport("kernel32.dll", SetLastError = true)] - static extern bool FreeConsole(); - public static bool ConsoleVisible { get; private set; } + static LogWindow window; + static LogStream logStream; + static StringBuilder sbLog; + + class LogStream : Stream + { + public LogStream() + { + } + + public override bool CanRead { get { return false; } } + public override bool CanSeek { get { return false; } } + public override bool CanWrite { get { return true; } } + + public override void Flush() + { + //TODO - maybe this will help with decoding + } + + public override long Length + { + get { throw new NotImplementedException(); } + } + + public override long Position + { + get + { + throw new NotImplementedException(); + } + set + { + throw new NotImplementedException(); + } + } + + public override int Read(byte[] buffer, int offset, int count) + { + throw new NotImplementedException(); + } + + public override long Seek(long offset, SeekOrigin origin) + { + throw new NotImplementedException(); + } + + public override void SetLength(long value) + { + throw new NotImplementedException(); + } + + public override void Write(byte[] buffer, int offset, int count) + { + //TODO - buffer undecoded characters (this may be important) + //(use decoder = System.Text.Encoding.Unicode.GetDecoder()) + string str = System.Text.Encoding.ASCII.GetString(buffer, offset, count); + if (Emit != null) + Emit(str); + } + + public Action Emit; + } + public static void ShowConsole() { if (ConsoleVisible) return; - AllocConsole(); - ConsoleVisible = true; - var sout = new StreamWriter(Console.OpenStandardOutput()) {AutoFlush = true}; - Console.SetOut(sout); - Console.Title = "BizHawk Message Log"; - + ConsoleVisible = true; + + logStream = new LogStream(); + var sout = new StreamWriter(logStream) { AutoFlush = true }; + Console.SetOut(sout); + window = new LogWindow(); + window.Show(); + sbLog = new StringBuilder(); //not using this right now + logStream.Emit = (str) => { window.Append(str); }; } public static void HideConsole() { if (ConsoleVisible == false) return; - FreeConsole(); - Console.SetOut(TextWriter.Null); - ConsoleVisible = false; + window.Dispose(); + logStream.Dispose(); + logStream = null; + window = null; + + Console.SetOut(TextWriter.Null); + ConsoleVisible = false; } } } \ No newline at end of file diff --git a/BizHawk.MultiClient/LogWindow.Designer.cs b/BizHawk.MultiClient/LogWindow.Designer.cs index 92b810a7ef..eccfd870f9 100644 --- a/BizHawk.MultiClient/LogWindow.Designer.cs +++ b/BizHawk.MultiClient/LogWindow.Designer.cs @@ -37,7 +37,7 @@ // // btnClose // - this.btnClose.Location = new System.Drawing.Point(590, 3); + this.btnClose.Location = new System.Drawing.Point(618, 3); this.btnClose.Name = "btnClose"; this.btnClose.Size = new System.Drawing.Size(75, 23); this.btnClose.TabIndex = 2; @@ -70,25 +70,26 @@ this.tableLayoutPanel1.Name = "tableLayoutPanel1"; this.tableLayoutPanel1.RowCount = 1; this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F)); - this.tableLayoutPanel1.Size = new System.Drawing.Size(668, 29); + this.tableLayoutPanel1.Size = new System.Drawing.Size(696, 29); this.tableLayoutPanel1.TabIndex = 5; // // textBox1 // this.textBox1.Dock = System.Windows.Forms.DockStyle.Fill; + this.textBox1.Font = new System.Drawing.Font("Lucida Console", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); this.textBox1.Location = new System.Drawing.Point(0, 0); this.textBox1.Multiline = true; this.textBox1.Name = "textBox1"; this.textBox1.ReadOnly = true; this.textBox1.ScrollBars = System.Windows.Forms.ScrollBars.Vertical; - this.textBox1.Size = new System.Drawing.Size(668, 273); + this.textBox1.Size = new System.Drawing.Size(696, 273); this.textBox1.TabIndex = 6; // // LogWindow // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(668, 302); + this.ClientSize = new System.Drawing.Size(696, 302); this.Controls.Add(this.textBox1); this.Controls.Add(this.tableLayoutPanel1); this.Name = "LogWindow"; diff --git a/BizHawk.MultiClient/LogWindow.cs b/BizHawk.MultiClient/LogWindow.cs index 1316a0405c..c9a101b892 100644 --- a/BizHawk.MultiClient/LogWindow.cs +++ b/BizHawk.MultiClient/LogWindow.cs @@ -7,6 +7,8 @@ using System.Linq; using System.Text; using System.Windows.Forms; +//todo - perks - pause, copy to clipboard, backlog length limiting + namespace BizHawk.MultiClient { public partial class LogWindow : Form @@ -24,9 +26,31 @@ namespace BizHawk.MultiClient ShowDialog(); } + public void ShowConsole() + { + Show(); + } + + public void SetLogText(string str) + { + textBox1.Text = str; + textBox1.SelectionStart = str.Length; + textBox1.ScrollToCaret(); + Refresh(); + } + + StringBuilder sbLog = new StringBuilder(); + public void Append(string str) + { + sbLog.Append(str); + SetLogText(sbLog.ToString()); + } + private void btnClear_Click(object sender, EventArgs e) { - + sbLog.Length = 0; + SetLogText(""); + Refresh(); } private void btnClose_Click(object sender, EventArgs e)