summaryrefslogtreecommitdiff
path: root/src/SMAPI/Framework/Logging/ConsoleInterceptionManager.cs
diff options
context:
space:
mode:
authorJesse Plamondon-Willard <github@jplamondonw.com>2017-10-07 23:07:10 -0400
committerJesse Plamondon-Willard <github@jplamondonw.com>2017-10-07 23:07:10 -0400
commit929dccb75a1405737975d76648e015a3e7c00177 (patch)
tree659fe16509327e694555db363caf7f47f326443b /src/SMAPI/Framework/Logging/ConsoleInterceptionManager.cs
parent926894f8f52c2a5cf104fcac2f7f34b637f7b531 (diff)
downloadSMAPI-929dccb75a1405737975d76648e015a3e7c00177.tar.gz
SMAPI-929dccb75a1405737975d76648e015a3e7c00177.tar.bz2
SMAPI-929dccb75a1405737975d76648e015a3e7c00177.zip
reorganise repo structure
Diffstat (limited to 'src/SMAPI/Framework/Logging/ConsoleInterceptionManager.cs')
-rw-r--r--src/SMAPI/Framework/Logging/ConsoleInterceptionManager.cs86
1 files changed, 86 insertions, 0 deletions
diff --git a/src/SMAPI/Framework/Logging/ConsoleInterceptionManager.cs b/src/SMAPI/Framework/Logging/ConsoleInterceptionManager.cs
new file mode 100644
index 00000000..b8f2c34e
--- /dev/null
+++ b/src/SMAPI/Framework/Logging/ConsoleInterceptionManager.cs
@@ -0,0 +1,86 @@
+using System;
+
+namespace StardewModdingAPI.Framework.Logging
+{
+ /// <summary>Manages console output interception.</summary>
+ internal class ConsoleInterceptionManager : IDisposable
+ {
+ /*********
+ ** Properties
+ *********/
+ /// <summary>The intercepting console writer.</summary>
+ private readonly InterceptingTextWriter Output;
+
+
+ /*********
+ ** Accessors
+ *********/
+ /// <summary>Whether the current console supports color formatting.</summary>
+ public bool SupportsColor { get; }
+
+ /// <summary>The event raised when a message is written to the console directly.</summary>
+ public event Action<string> OnMessageIntercepted;
+
+
+ /*********
+ ** Public methods
+ *********/
+ /// <summary>Construct an instance.</summary>
+ public ConsoleInterceptionManager()
+ {
+ // redirect output through interceptor
+ this.Output = new InterceptingTextWriter(Console.Out);
+ this.Output.OnMessageIntercepted += line => this.OnMessageIntercepted?.Invoke(line);
+ Console.SetOut(this.Output);
+
+ // test color support
+ this.SupportsColor = this.TestColorSupport();
+ }
+
+ /// <summary>Get an exclusive lock and write to the console output without interception.</summary>
+ /// <param name="action">The action to perform within the exclusive write block.</param>
+ public void ExclusiveWriteWithoutInterception(Action action)
+ {
+ lock (Console.Out)
+ {
+ try
+ {
+ this.Output.ShouldIntercept = false;
+ action();
+ }
+ finally
+ {
+ this.Output.ShouldIntercept = true;
+ }
+ }
+ }
+
+ /// <summary>Release all resources.</summary>
+ public void Dispose()
+ {
+ Console.SetOut(this.Output.Out);
+ this.Output.Dispose();
+ }
+
+
+ /*********
+ ** private methods
+ *********/
+ /// <summary>Test whether the current console supports color formatting.</summary>
+ private bool TestColorSupport()
+ {
+ try
+ {
+ this.ExclusiveWriteWithoutInterception(() =>
+ {
+ Console.ForegroundColor = Console.ForegroundColor;
+ });
+ return true;
+ }
+ catch (Exception)
+ {
+ return false; // Mono bug
+ }
+ }
+ }
+}