summaryrefslogtreecommitdiff
path: root/src/StardewModdingAPI/Framework/Logging
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/StardewModdingAPI/Framework/Logging
parent926894f8f52c2a5cf104fcac2f7f34b637f7b531 (diff)
downloadSMAPI-929dccb75a1405737975d76648e015a3e7c00177.tar.gz
SMAPI-929dccb75a1405737975d76648e015a3e7c00177.tar.bz2
SMAPI-929dccb75a1405737975d76648e015a3e7c00177.zip
reorganise repo structure
Diffstat (limited to 'src/StardewModdingAPI/Framework/Logging')
-rw-r--r--src/StardewModdingAPI/Framework/Logging/ConsoleInterceptionManager.cs86
-rw-r--r--src/StardewModdingAPI/Framework/Logging/InterceptingTextWriter.cs63
-rw-r--r--src/StardewModdingAPI/Framework/Logging/LogFileManager.cs57
3 files changed, 0 insertions, 206 deletions
diff --git a/src/StardewModdingAPI/Framework/Logging/ConsoleInterceptionManager.cs b/src/StardewModdingAPI/Framework/Logging/ConsoleInterceptionManager.cs
deleted file mode 100644
index b8f2c34e..00000000
--- a/src/StardewModdingAPI/Framework/Logging/ConsoleInterceptionManager.cs
+++ /dev/null
@@ -1,86 +0,0 @@
-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
- }
- }
- }
-}
diff --git a/src/StardewModdingAPI/Framework/Logging/InterceptingTextWriter.cs b/src/StardewModdingAPI/Framework/Logging/InterceptingTextWriter.cs
deleted file mode 100644
index 9ca61b59..00000000
--- a/src/StardewModdingAPI/Framework/Logging/InterceptingTextWriter.cs
+++ /dev/null
@@ -1,63 +0,0 @@
-using System;
-using System.IO;
-using System.Text;
-
-namespace StardewModdingAPI.Framework.Logging
-{
- /// <summary>A text writer which allows intercepting output.</summary>
- internal class InterceptingTextWriter : TextWriter
- {
- /*********
- ** 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 message is written to the console directly.</summary>
- public event Action<string> OnMessageIntercepted;
-
-
- /*********
- ** 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 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)
- {
- 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.OnMessageIntercepted = null;
- }
- }
-}
diff --git a/src/StardewModdingAPI/Framework/Logging/LogFileManager.cs b/src/StardewModdingAPI/Framework/Logging/LogFileManager.cs
deleted file mode 100644
index 8cfe0527..00000000
--- a/src/StardewModdingAPI/Framework/Logging/LogFileManager.cs
+++ /dev/null
@@ -1,57 +0,0 @@
-using System;
-using System.IO;
-
-namespace StardewModdingAPI.Framework.Logging
-{
- /// <summary>Manages reading and writing to log file.</summary>
- internal class LogFileManager : IDisposable
- {
- /*********
- ** Properties
- *********/
- /// <summary>The underlying stream writer.</summary>
- private readonly StreamWriter Stream;
-
-
- /*********
- ** Accessors
- *********/
- /// <summary>The full path to the log file being written.</summary>
- public string Path { get; }
-
-
- /*********
- ** Public methods
- *********/
- /// <summary>Construct an instance.</summary>
- /// <param name="path">The log file to write.</param>
- public LogFileManager(string path)
- {
- this.Path = path;
-
- // create log directory if needed
- string logDir = System.IO.Path.GetDirectoryName(path);
- if (logDir == null)
- throw new ArgumentException($"The log path '{path}' is not valid.");
- Directory.CreateDirectory(logDir);
-
- // open log file stream
- this.Stream = new StreamWriter(path, append: false) { AutoFlush = true };
- }
-
- /// <summary>Write a message to the log.</summary>
- /// <param name="message">The message to log.</param>
- public void WriteLine(string message)
- {
- // always use Windows-style line endings for convenience
- // (Linux/Mac editors are fine with them, Windows editors often require them)
- this.Stream.Write(message + "\r\n");
- }
-
- /// <summary>Release all resources.</summary>
- public void Dispose()
- {
- this.Stream.Dispose();
- }
- }
-}