summaryrefslogtreecommitdiff
path: root/src/StardewModdingAPI
diff options
context:
space:
mode:
authorJesse Plamondon-Willard <github@jplamondonw.com>2017-04-26 16:04:20 -0400
committerJesse Plamondon-Willard <github@jplamondonw.com>2017-04-26 16:04:20 -0400
commit4ef957c191f3ad012b234d533810dd59717f30c1 (patch)
tree74300a50aaefd8f95670858cf0a6ca01f5ac2eea /src/StardewModdingAPI
parente7606884adee2ada103439aca2d53cc29b57da17 (diff)
downloadSMAPI-4ef957c191f3ad012b234d533810dd59717f30c1.tar.gz
SMAPI-4ef957c191f3ad012b234d533810dd59717f30c1.tar.bz2
SMAPI-4ef957c191f3ad012b234d533810dd59717f30c1.zip
optimise console interception for the way Stardew Valley logs messages
Diffstat (limited to 'src/StardewModdingAPI')
-rw-r--r--src/StardewModdingAPI/Framework/Logging/ConsoleInterceptionManager.cs6
-rw-r--r--src/StardewModdingAPI/Framework/Logging/InterceptingTextWriter.cs50
-rw-r--r--src/StardewModdingAPI/Program.cs2
3 files changed, 21 insertions, 37 deletions
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
/// <summary>Whether the current console supports color formatting.</summary>
public bool SupportsColor { get; }
- /// <summary>The event raised when something writes a line to the console directly.</summary>
- public event Action<string> OnLineIntercepted;
+ /// <summary>The event raised when a message is written to the console directly.</summary>
+ public event Action<string> 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;
@@ -9,13 +8,6 @@ namespace StardewModdingAPI.Framework.Logging
internal class InterceptingTextWriter : TextWriter
{
/*********
- ** Properties
- *********/
- /// <summary>The current line being intercepted.</summary>
- private readonly List<char> Line = new List<char>();
-
-
- /*********
** Accessors
*********/
/// <summary>The underlying console output.</summary>
@@ -27,8 +19,8 @@ namespace StardewModdingAPI.Framework.Logging
/// <summary>Whether to intercept console output.</summary>
public bool ShouldIntercept { get; set; }
- /// <summary>The event raised when a line of text is intercepted.</summary>
- public event Action<string> OnLineIntercepted;
+ /// <summary>The event raised when a message is written to the console directly.</summary>
+ public event Action<string> OnMessageIntercepted;
/*********
@@ -41,39 +33,31 @@ namespace StardewModdingAPI.Framework.Logging
this.Out = output;
}
+ /// <summary>Writes a subarray of characters to the text string or stream.</summary>
+ /// <param name="buffer">The character array to write data from.</param>
+ /// <param name="index">The character position in the buffer at which to start retrieving data.</param>
+ /// <param name="count">The number of characters to write.</param>
+ 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);
+ }
+
/// <summary>Writes a character to the text string or stream.</summary>
/// <param name="ch">The character to write to the text stream.</param>
+ /// <remarks>Console log messages from the game should be caught by <see cref="Write(char[],int,int)"/>. 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.</remarks>
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);
}
/// <summary>Releases the unmanaged resources used by the <see cref="T:System.IO.TextWriter" /> and optionally releases the managed resources.</summary>
/// <param name="disposing">true to release both managed and unmanaged resources; false to release only unmanaged resources.</param>
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