From 4c7329d75e5baf3819852f77367c1c47ce57fcb4 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Fri, 4 Nov 2016 20:01:45 -0400 Subject: make log writer internal (not meant for use outside SMAPI) --- src/StardewModdingAPI/Framework/LogWriter.cs | 110 +++++++++++++++++++++++++ src/StardewModdingAPI/Log.cs | 1 + src/StardewModdingAPI/LogInfo.cs | 40 +++++++++ src/StardewModdingAPI/LogWriter.cs | 109 ------------------------ src/StardewModdingAPI/Logger.cs | 40 --------- src/StardewModdingAPI/StardewModdingAPI.csproj | 4 +- 6 files changed, 153 insertions(+), 151 deletions(-) create mode 100644 src/StardewModdingAPI/Framework/LogWriter.cs create mode 100644 src/StardewModdingAPI/LogInfo.cs delete mode 100644 src/StardewModdingAPI/LogWriter.cs delete mode 100644 src/StardewModdingAPI/Logger.cs (limited to 'src/StardewModdingAPI') diff --git a/src/StardewModdingAPI/Framework/LogWriter.cs b/src/StardewModdingAPI/Framework/LogWriter.cs new file mode 100644 index 00000000..2cabb8d5 --- /dev/null +++ b/src/StardewModdingAPI/Framework/LogWriter.cs @@ -0,0 +1,110 @@ +using System; +using System.Collections.Concurrent; +using System.IO; +using System.Linq; + +namespace StardewModdingAPI.Framework +{ + /// A log writer which queues messages for output, and periodically flushes them to the console and log file. + internal class LogWriter + { + /********* + ** Properties + *********/ + /// The singleton instance. + private static LogWriter _instance; + + /// The queued messages to flush. + private static ConcurrentQueue Queue; + + /// The underlying file stream. + private static StreamWriter FileStream; + + + /********* + ** Accessors + *********/ + /// The singleton instance. + internal static LogWriter Instance + { + 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(); + 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); + } + + LogWriter.FileStream = new StreamWriter(Constants.LogPath, false); + Console.WriteLine("Created log instance"); + } + return LogWriter._instance; + } + } + + + /********* + ** Public methods + *********/ + /// Queue a message for output. + /// The message to log. + public void WriteToLog(string message) + { + lock (LogWriter.Queue) + { + var logEntry = new LogInfo(message); + LogWriter.Queue.Enqueue(logEntry); + + if (LogWriter.Queue.Any()) + this.FlushLog(); + } + } + + /// Queue a message for output. + /// The message to log. + public void WriteToLog(LogInfo message) + { + lock (LogWriter.Queue) + { + LogWriter.Queue.Enqueue(message); + if (LogWriter.Queue.Any()) + this.FlushLog(); + } + } + + + /********* + ** Private methods + *********/ + /// Construct an instance. + private LogWriter() { } + + /// Flush the underlying queue to the console and file. + private void FlushLog() + { + lock (LogWriter.FileStream) + { + LogInfo entry; + while (LogWriter.Queue.TryDequeue(out entry)) + { + string m = $"[{entry.LogTime}] {entry.Message}"; + + Console.ForegroundColor = entry.Colour; + Console.WriteLine(m); + Console.ForegroundColor = ConsoleColor.Gray; + + LogWriter.FileStream.WriteLine(m); + } + LogWriter.FileStream.Flush(); + } + } + } +} \ No newline at end of file diff --git a/src/StardewModdingAPI/Log.cs b/src/StardewModdingAPI/Log.cs index ddd82579..2b814f91 100644 --- a/src/StardewModdingAPI/Log.cs +++ b/src/StardewModdingAPI/Log.cs @@ -2,6 +2,7 @@ using System; using System.IO; using System.Threading; using System.Threading.Tasks; +using StardewModdingAPI.Framework; namespace StardewModdingAPI { diff --git a/src/StardewModdingAPI/LogInfo.cs b/src/StardewModdingAPI/LogInfo.cs new file mode 100644 index 00000000..f0f08e4f --- /dev/null +++ b/src/StardewModdingAPI/LogInfo.cs @@ -0,0 +1,40 @@ +using System; + +namespace StardewModdingAPI +{ + /// A message queued for log output. + public struct LogInfo + { + /********* + ** Accessors + *********/ + /// The message to log. + public string Message { get; set; } + + /// The log date. + public string LogDate { get; set; } + + /// The log time. + public string LogTime { get; set; } + + /// The message color. + public ConsoleColor Colour { get; set; } + + + /********* + ** Public methods + *********/ + /// Construct an instance. + /// The message to log. + /// The message color. + public LogInfo(string message, ConsoleColor color = ConsoleColor.Gray) + { + if (string.IsNullOrEmpty(message)) + message = "[null]"; + this.Message = message; + this.LogDate = DateTime.Now.ToString("yyyy-MM-dd"); + this.LogTime = DateTime.Now.ToString("hh:mm:ss.fff tt"); + this.Colour = color; + } + } +} diff --git a/src/StardewModdingAPI/LogWriter.cs b/src/StardewModdingAPI/LogWriter.cs deleted file mode 100644 index 139d89bf..00000000 --- a/src/StardewModdingAPI/LogWriter.cs +++ /dev/null @@ -1,109 +0,0 @@ -using System; -using System.Collections.Concurrent; -using System.IO; -using System.Linq; - -namespace StardewModdingAPI -{ - /// A log writer which queues messages for output, and periodically flushes them to the console and log file. - public class LogWriter - { - /********* - ** Properties - *********/ - /// The singleton instance. - private static LogWriter _instance; - - /// The queued messages to flush. - private static ConcurrentQueue Queue; - - /// The underlying file stream. - private static StreamWriter FileStream; - - - /********* - ** Accessors - *********/ - /// The singleton instance. - internal static LogWriter Instance - { - 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(); - 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); - } - - LogWriter.FileStream = new StreamWriter(Constants.LogPath, false); - Console.WriteLine("Created log instance"); - } - return LogWriter._instance; - } - } - - - /********* - ** Public methods - *********/ - /// Queue a message for output. - /// The message to log. - public void WriteToLog(string message) - { - lock (LogWriter.Queue) - { - var logEntry = new LogInfo(message); - LogWriter.Queue.Enqueue(logEntry); - - if (LogWriter.Queue.Any()) - this.FlushLog(); - } - } - - /// Queue a message for output. - /// The message to log. - public void WriteToLog(LogInfo message) - { - lock (LogWriter.Queue) - { - LogWriter.Queue.Enqueue(message); - if (LogWriter.Queue.Any()) - this.FlushLog(); - } - } - - /********* - ** Private methods - *********/ - /// Construct an instance. - private LogWriter() { } - - /// Flush the underlying queue to the console and file. - private void FlushLog() - { - lock (LogWriter.FileStream) - { - LogInfo entry; - while (LogWriter.Queue.TryDequeue(out entry)) - { - string m = $"[{entry.LogTime}] {entry.Message}"; - - Console.ForegroundColor = entry.Colour; - Console.WriteLine(m); - Console.ForegroundColor = ConsoleColor.Gray; - - LogWriter.FileStream.WriteLine(m); - } - LogWriter.FileStream.Flush(); - } - } - } -} \ No newline at end of file diff --git a/src/StardewModdingAPI/Logger.cs b/src/StardewModdingAPI/Logger.cs deleted file mode 100644 index f0f08e4f..00000000 --- a/src/StardewModdingAPI/Logger.cs +++ /dev/null @@ -1,40 +0,0 @@ -using System; - -namespace StardewModdingAPI -{ - /// A message queued for log output. - public struct LogInfo - { - /********* - ** Accessors - *********/ - /// The message to log. - public string Message { get; set; } - - /// The log date. - public string LogDate { get; set; } - - /// The log time. - public string LogTime { get; set; } - - /// The message color. - public ConsoleColor Colour { get; set; } - - - /********* - ** Public methods - *********/ - /// Construct an instance. - /// The message to log. - /// The message color. - public LogInfo(string message, ConsoleColor color = ConsoleColor.Gray) - { - if (string.IsNullOrEmpty(message)) - message = "[null]"; - this.Message = message; - this.LogDate = DateTime.Now.ToString("yyyy-MM-dd"); - this.LogTime = DateTime.Now.ToString("hh:mm:ss.fff tt"); - this.Colour = color; - } - } -} diff --git a/src/StardewModdingAPI/StardewModdingAPI.csproj b/src/StardewModdingAPI/StardewModdingAPI.csproj index ddab8734..9e89c9e6 100644 --- a/src/StardewModdingAPI/StardewModdingAPI.csproj +++ b/src/StardewModdingAPI/StardewModdingAPI.csproj @@ -198,8 +198,8 @@ - - + + -- cgit