summaryrefslogtreecommitdiff
path: root/src/StardewModdingAPI/Framework/Logging/InterceptingTextWriter.cs
diff options
context:
space:
mode:
authorJesse Plamondon-Willard <github@jplamondonw.com>2017-02-11 01:15:56 -0500
committerJesse Plamondon-Willard <github@jplamondonw.com>2017-02-11 01:15:56 -0500
commit46b7d7a4001e209d7201ed5cad38cad2f1ad2a7f (patch)
tree905b16cb83e0d47d7ae89b4ffcfaa2a02630cb6d /src/StardewModdingAPI/Framework/Logging/InterceptingTextWriter.cs
parent3e91af6b06deec6aa2dca80945c82af528094c52 (diff)
downloadSMAPI-46b7d7a4001e209d7201ed5cad38cad2f1ad2a7f.tar.gz
SMAPI-46b7d7a4001e209d7201ed5cad38cad2f1ad2a7f.tar.bz2
SMAPI-46b7d7a4001e209d7201ed5cad38cad2f1ad2a7f.zip
redirect the game's debug messages into trace logs (#233)
The game writes debug messages directly to the console, which shows up for SMAPI users. This commit redirects direct console messages to a monitor.
Diffstat (limited to 'src/StardewModdingAPI/Framework/Logging/InterceptingTextWriter.cs')
-rw-r--r--src/StardewModdingAPI/Framework/Logging/InterceptingTextWriter.cs79
1 files changed, 79 insertions, 0 deletions
diff --git a/src/StardewModdingAPI/Framework/Logging/InterceptingTextWriter.cs b/src/StardewModdingAPI/Framework/Logging/InterceptingTextWriter.cs
new file mode 100644
index 00000000..14789109
--- /dev/null
+++ b/src/StardewModdingAPI/Framework/Logging/InterceptingTextWriter.cs
@@ -0,0 +1,79 @@
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Text;
+
+namespace StardewModdingAPI.Framework.Logging
+{
+ /// <summary>A text writer which allows intercepting output.</summary>
+ 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>
+ public TextWriter Out { get; }
+
+ /// <summary>The character encoding in which the output is written.</summary>
+ public override Encoding Encoding => this.Out.Encoding;
+
+ /// <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;
+
+
+ /*********
+ ** Public methods
+ *********/
+ /// <summary>Construct an instance.</summary>
+ /// <param name="output">The underlying output writer.</param>
+ public InterceptingTextWriter(TextWriter output)
+ {
+ this.Out = output;
+ }
+
+ /// <summary>Writes a character to the text string or stream.</summary>
+ /// <param name="ch">The character to write to the text stream.</param>
+ 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);
+ }
+
+ /// <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;
+ }
+ }
+}