From 4ef957c191f3ad012b234d533810dd59717f30c1 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Wed, 26 Apr 2017 16:04:20 -0400 Subject: optimise console interception for the way Stardew Valley logs messages --- .../Logging/ConsoleInterceptionManager.cs | 6 +-- .../Framework/Logging/InterceptingTextWriter.cs | 50 ++++++++-------------- src/StardewModdingAPI/Program.cs | 2 +- 3 files changed, 21 insertions(+), 37 deletions(-) (limited to 'src/StardewModdingAPI') diff --git a/src/StardewModdingAPI/Framework/Logging/ConsoleInterceptionManager.cs b/src/StardewModdingAPI/Framework/Logging/ConsoleInterceptionManager.cs index d84671ee..b8f2c34e 100644 --- a/src/StardewModdingAPI/Framework/Logging/ConsoleInterceptionManager.cs +++ b/src/StardewModdingAPI/Framework/Logging/ConsoleInterceptionManager.cs @@ -18,8 +18,8 @@ namespace StardewModdingAPI.Framework.Logging /// Whether the current console supports color formatting. public bool SupportsColor { get; } - /// The event raised when something writes a line to the console directly. - public event Action OnLineIntercepted; + /// The event raised when a message is written to the console directly. + public event Action OnMessageIntercepted; /********* @@ -30,7 +30,7 @@ namespace StardewModdingAPI.Framework.Logging { // redirect output through interceptor this.Output = new InterceptingTextWriter(Console.Out); - this.Output.OnLineIntercepted += line => this.OnLineIntercepted?.Invoke(line); + this.Output.OnMessageIntercepted += line => this.OnMessageIntercepted?.Invoke(line); Console.SetOut(this.Output); // test color support diff --git a/src/StardewModdingAPI/Framework/Logging/InterceptingTextWriter.cs b/src/StardewModdingAPI/Framework/Logging/InterceptingTextWriter.cs index 14789109..9ca61b59 100644 --- a/src/StardewModdingAPI/Framework/Logging/InterceptingTextWriter.cs +++ b/src/StardewModdingAPI/Framework/Logging/InterceptingTextWriter.cs @@ -1,5 +1,4 @@ using System; -using System.Collections.Generic; using System.IO; using System.Text; @@ -8,13 +7,6 @@ namespace StardewModdingAPI.Framework.Logging /// A text writer which allows intercepting output. internal class InterceptingTextWriter : TextWriter { - /********* - ** Properties - *********/ - /// The current line being intercepted. - private readonly List Line = new List(); - - /********* ** Accessors *********/ @@ -27,8 +19,8 @@ namespace StardewModdingAPI.Framework.Logging /// Whether to intercept console output. public bool ShouldIntercept { get; set; } - /// The event raised when a line of text is intercepted. - public event Action OnLineIntercepted; + /// The event raised when a message is written to the console directly. + public event Action OnMessageIntercepted; /********* @@ -41,39 +33,31 @@ namespace StardewModdingAPI.Framework.Logging this.Out = output; } + /// Writes a subarray of characters to the text string or stream. + /// The character array to write data from. + /// The character position in the buffer at which to start retrieving data. + /// The number of characters to write. + public override void Write(char[] buffer, int index, int count) + { + if (this.ShouldIntercept) + this.OnMessageIntercepted?.Invoke(new string(buffer, index, count).TrimEnd('\r', '\n')); + else + this.Out.Write(buffer, index, count); + } + /// Writes a character to the text string or stream. /// The character to write to the text stream. + /// Console log messages from the game should be caught by . This method passes through anything that bypasses that method for some reason, since it's better to show it to users than hide it from everyone. public override void Write(char ch) { - // intercept - if (this.ShouldIntercept) - { - switch (ch) - { - case '\r': - return; - - case '\n': - this.OnLineIntercepted?.Invoke(new string(this.Line.ToArray())); - this.Line.Clear(); - break; - - default: - this.Line.Add(ch); - break; - } - } - - // pass through - else - this.Out.Write(ch); + this.Out.Write(ch); } /// Releases the unmanaged resources used by the and optionally releases the managed resources. /// true to release both managed and unmanaged resources; false to release only unmanaged resources. protected override void Dispose(bool disposing) { - this.OnLineIntercepted = null; + this.OnMessageIntercepted = null; } } } diff --git a/src/StardewModdingAPI/Program.cs b/src/StardewModdingAPI/Program.cs index 31aeb3a6..6e7f9950 100644 --- a/src/StardewModdingAPI/Program.cs +++ b/src/StardewModdingAPI/Program.cs @@ -249,7 +249,7 @@ namespace StardewModdingAPI Monitor monitor = this.GetSecondaryMonitor("Console.Out"); monitor.WriteToFile = false; // not useful for troubleshooting mods per discussion if (monitor.WriteToConsole) - this.ConsoleManager.OnLineIntercepted += line => monitor.Log(line, LogLevel.Trace); + this.ConsoleManager.OnMessageIntercepted += line => monitor.Log(line, LogLevel.Trace); } // add warning headers -- cgit