diff options
author | Jesse Plamondon-Willard <github@jplamondonw.com> | 2016-11-04 20:02:39 -0400 |
---|---|---|
committer | Jesse Plamondon-Willard <github@jplamondonw.com> | 2016-11-04 20:02:39 -0400 |
commit | ea0550028c5e1a473b4e4547610812b8921e218f (patch) | |
tree | 6d487fc04a2f2194135c9429588f4cf84bfcc74d /src/StardewModdingAPI/Framework | |
parent | 4c7329d75e5baf3819852f77367c1c47ce57fcb4 (diff) | |
download | SMAPI-ea0550028c5e1a473b4e4547610812b8921e218f.tar.gz SMAPI-ea0550028c5e1a473b4e4547610812b8921e218f.tar.bz2 SMAPI-ea0550028c5e1a473b4e4547610812b8921e218f.zip |
simplify log singleton management
Diffstat (limited to 'src/StardewModdingAPI/Framework')
-rw-r--r-- | src/StardewModdingAPI/Framework/LogWriter.cs | 71 |
1 files changed, 24 insertions, 47 deletions
diff --git a/src/StardewModdingAPI/Framework/LogWriter.cs b/src/StardewModdingAPI/Framework/LogWriter.cs index 2cabb8d5..64769d2e 100644 --- a/src/StardewModdingAPI/Framework/LogWriter.cs +++ b/src/StardewModdingAPI/Framework/LogWriter.cs @@ -6,64 +6,44 @@ using System.Linq; namespace StardewModdingAPI.Framework { /// <summary>A log writer which queues messages for output, and periodically flushes them to the console and log file.</summary> + /// <remarks>Only one instance should be created.</remarks> internal class LogWriter { /********* ** Properties *********/ - /// <summary>The singleton instance.</summary> - private static LogWriter _instance; - /// <summary>The queued messages to flush.</summary> - private static ConcurrentQueue<LogInfo> Queue; + private readonly ConcurrentQueue<LogInfo> Queue; /// <summary>The underlying file stream.</summary> - private static StreamWriter FileStream; + private readonly StreamWriter FileStream; /********* - ** Accessors + ** Public methods *********/ - /// <summary>The singleton instance.</summary> - internal static LogWriter Instance + /// <summary>Construct an instance.</summary> + /// <param name="path">The log path to write.</param> + public LogWriter(string path) { - get - { - if (LogWriter._instance == null) - { - LogWriter._instance = new LogWriter(); - // Field cannot be used by anything else regardless, do not surround with lock { } - // ReSharper disable once InconsistentlySynchronizedField - LogWriter.Queue = new ConcurrentQueue<LogInfo>(); - Console.WriteLine(Constants.LogPath); - - // If the ErrorLogs dir doesn't exist StreamWriter will throw an exception. - if (!Directory.Exists(Constants.LogDir)) - { - Directory.CreateDirectory(Constants.LogDir); - } + // create log directory (required for stream writer) + Directory.CreateDirectory(Path.GetDirectoryName(path)); - LogWriter.FileStream = new StreamWriter(Constants.LogPath, false); - Console.WriteLine("Created log instance"); - } - return LogWriter._instance; - } + // initialise + this.Queue = new ConcurrentQueue<LogInfo>(); + this.FileStream = new StreamWriter(Constants.LogPath, false); } - - /********* - ** Public methods - *********/ /// <summary>Queue a message for output.</summary> /// <param name="message">The message to log.</param> public void WriteToLog(string message) { - lock (LogWriter.Queue) + lock (this.Queue) { var logEntry = new LogInfo(message); - LogWriter.Queue.Enqueue(logEntry); + this.Queue.Enqueue(logEntry); - if (LogWriter.Queue.Any()) + if (this.Queue.Any()) this.FlushLog(); } } @@ -72,10 +52,10 @@ namespace StardewModdingAPI.Framework /// <param name="message">The message to log.</param> public void WriteToLog(LogInfo message) { - lock (LogWriter.Queue) + lock (this.Queue) { - LogWriter.Queue.Enqueue(message); - if (LogWriter.Queue.Any()) + this.Queue.Enqueue(message); + if (this.Queue.Any()) this.FlushLog(); } } @@ -84,26 +64,23 @@ namespace StardewModdingAPI.Framework /********* ** Private methods *********/ - /// <summary>Construct an instance.</summary> - private LogWriter() { } - /// <summary>Flush the underlying queue to the console and file.</summary> private void FlushLog() { - lock (LogWriter.FileStream) + lock (this.FileStream) { LogInfo entry; - while (LogWriter.Queue.TryDequeue(out entry)) + while (this.Queue.TryDequeue(out entry)) { - string m = $"[{entry.LogTime}] {entry.Message}"; + string message = $"[{entry.LogTime}] {entry.Message}"; Console.ForegroundColor = entry.Colour; - Console.WriteLine(m); + Console.WriteLine(message); Console.ForegroundColor = ConsoleColor.Gray; - LogWriter.FileStream.WriteLine(m); + this.FileStream.WriteLine(message); } - LogWriter.FileStream.Flush(); + this.FileStream.Flush(); } } } |