diff options
author | Jesse Plamondon-Willard <Pathoschild@users.noreply.github.com> | 2020-08-24 19:25:57 -0400 |
---|---|---|
committer | Jesse Plamondon-Willard <Pathoschild@users.noreply.github.com> | 2020-08-24 19:25:57 -0400 |
commit | 046deb2d56b6d4665280cc5478d9e683ec1d777d (patch) | |
tree | bccc347514ecfe1801994232c0c40ba43782adb9 /src/SMAPI/Framework/Logging/ConsoleInterceptionManager.cs | |
parent | 46d63e11cc76c70ff77ee90edb6cc055c63e7224 (diff) | |
download | SMAPI-046deb2d56b6d4665280cc5478d9e683ec1d777d.tar.gz SMAPI-046deb2d56b6d4665280cc5478d9e683ec1d777d.tar.bz2 SMAPI-046deb2d56b6d4665280cc5478d9e683ec1d777d.zip |
simplify console interception flow
The console interceptor now uses a marker in the string (instead of a state field) to track whether the message should intercepted. This makes each write more atomic, so it's less affected by multithreading in some cases.
Diffstat (limited to 'src/SMAPI/Framework/Logging/ConsoleInterceptionManager.cs')
-rw-r--r-- | src/SMAPI/Framework/Logging/ConsoleInterceptionManager.cs | 59 |
1 files changed, 0 insertions, 59 deletions
diff --git a/src/SMAPI/Framework/Logging/ConsoleInterceptionManager.cs b/src/SMAPI/Framework/Logging/ConsoleInterceptionManager.cs deleted file mode 100644 index ef42e536..00000000 --- a/src/SMAPI/Framework/Logging/ConsoleInterceptionManager.cs +++ /dev/null @@ -1,59 +0,0 @@ -using System; - -namespace StardewModdingAPI.Framework.Logging -{ - /// <summary>Manages console output interception.</summary> - internal class ConsoleInterceptionManager : IDisposable - { - /********* - ** Fields - *********/ - /// <summary>The intercepting console writer.</summary> - private readonly InterceptingTextWriter Output; - - - /********* - ** Accessors - *********/ - /// <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); - } - - /// <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(); - } - } -} |