summaryrefslogtreecommitdiff
path: root/src/StardewModdingAPI/Framework/Logging
diff options
context:
space:
mode:
Diffstat (limited to 'src/StardewModdingAPI/Framework/Logging')
-rw-r--r--src/StardewModdingAPI/Framework/Logging/ConsoleInterceptionManager.cs6
-rw-r--r--src/StardewModdingAPI/Framework/Logging/InterceptingTextWriter.cs50
2 files changed, 20 insertions, 36 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;
}
}
}