From 06b108d4c4960d8b8bdfc732ba72ac093fc161ba Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Mon, 14 Nov 2016 19:27:44 -0500 Subject: deprecate legacy logging interfaces, rewrite to use new framework under the hood (#168) --- src/StardewModdingAPI/Log.cs | 86 +++++++++++++++++++++++++------------- src/StardewModdingAPI/LogWriter.cs | 68 +++++++++--------------------- src/StardewModdingAPI/Program.cs | 4 ++ 3 files changed, 80 insertions(+), 78 deletions(-) (limited to 'src') diff --git a/src/StardewModdingAPI/Log.cs b/src/StardewModdingAPI/Log.cs index 665e727f..b1af111c 100644 --- a/src/StardewModdingAPI/Log.cs +++ b/src/StardewModdingAPI/Log.cs @@ -1,22 +1,25 @@ using System; -using System.IO; using System.Threading; -using System.Threading.Tasks; using StardewModdingAPI.Framework; +using Monitor = StardewModdingAPI.Framework.Monitor; namespace StardewModdingAPI { /// A singleton which logs messages to the SMAPI console and log file. + [Obsolete("Use " + nameof(Mod) + "." + nameof(Mod.Monitor))] public static class Log { /********* - ** Properties + ** Accessors *********/ - /// A pseudorandom number generator used to generate log files. - private static readonly Random Random = new Random(); + /// The underlying logger. + internal static Monitor Monitor; - /// The underlying log writer. - private static readonly LogWriter Writer = new LogWriter(Constants.LogPath); + /// Tracks the installed mods. + internal static ModRegistry ModRegistry; + + /// A temporary field to avoid infinite loops (since we use a deprecated interface to warn about the deprecated interface). + private static bool WarnedDeprecated; /********* @@ -30,8 +33,8 @@ namespace StardewModdingAPI /// The event arguments. public static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) { - Console.WriteLine("An exception has been caught"); - File.WriteAllText(Path.Combine(Constants.LogDir, $"MODDED_ErrorLog.Log_{DateTime.UtcNow.Ticks}.txt"), e.ExceptionObject.ToString()); + Log.WarnDeprecated(); + Log.Monitor.Log($"Critical app domain exception: {e.ExceptionObject}", LogLevel.Error); } /// Log a thread exception event. @@ -39,8 +42,8 @@ namespace StardewModdingAPI /// The event arguments. public static void Application_ThreadException(object sender, ThreadExceptionEventArgs e) { - Console.WriteLine("A thread exception has been caught"); - File.WriteAllText(Path.Combine(Constants.LogDir, $"MODDED_ErrorLog.Log_{Log.Random.Next(100000000, 999999999)}.txt"), e.Exception.ToString()); + Log.WarnDeprecated(); + Log.Monitor.Log($"Critical thread exception: {e.Exception}", LogLevel.Error); } /**** @@ -51,7 +54,8 @@ namespace StardewModdingAPI /// The message color. public static void SyncColour(object message, ConsoleColor color) { - Log.PrintLog(new LogInfo(message?.ToString(), color)); + Log.WarnDeprecated(); + Log.Monitor.LegacyLog(Log.GetModName(), message?.ToString(), color); } /**** @@ -62,76 +66,87 @@ namespace StardewModdingAPI /// The message color. public static void AsyncColour(object message, ConsoleColor color) { - Task.Run(() => { Log.PrintLog(new LogInfo(message?.ToString(), color)); }); + Log.WarnDeprecated(); + Log.Monitor.LegacyLog(Log.GetModName(), message?.ToString(), color); } /// Asynchronously log a message to the console. /// The message to log. public static void Async(object message) { - Log.AsyncColour(message?.ToString(), ConsoleColor.Gray); + Log.WarnDeprecated(); + Log.Monitor.LegacyLog(Log.GetModName(), message?.ToString(), ConsoleColor.Gray); } /// Asynchronously log a red message to the console. /// The message to log. public static void AsyncR(object message) { - Log.AsyncColour(message?.ToString(), ConsoleColor.Red); + Log.WarnDeprecated(); + Log.Monitor.LegacyLog(Log.GetModName(), message?.ToString(), ConsoleColor.Red); } /// Asynchronously log an orange message to the console. /// The message to log. public static void AsyncO(object message) { - Log.AsyncColour(message.ToString(), ConsoleColor.DarkYellow); + Log.WarnDeprecated(); + Log.Monitor.LegacyLog(Log.GetModName(), message?.ToString(), ConsoleColor.DarkYellow); } /// Asynchronously log a yellow message to the console. /// The message to log. public static void AsyncY(object message) { - Log.AsyncColour(message?.ToString(), ConsoleColor.Yellow); + Log.WarnDeprecated(); + Log.Monitor.LegacyLog(Log.GetModName(), message?.ToString(), ConsoleColor.Yellow); } /// Asynchronously log a green message to the console. /// The message to log. public static void AsyncG(object message) { - Log.AsyncColour(message?.ToString(), ConsoleColor.Green); + Log.WarnDeprecated(); + Log.Monitor.LegacyLog(Log.GetModName(), message?.ToString(), ConsoleColor.Green); } /// Asynchronously log a cyan message to the console. /// The message to log. public static void AsyncC(object message) { - Log.AsyncColour(message?.ToString(), ConsoleColor.Cyan); + Log.WarnDeprecated(); + Log.Monitor.LegacyLog(Log.GetModName(), message?.ToString(), ConsoleColor.Cyan); } /// Asynchronously log a magenta message to the console. /// The message to log. public static void AsyncM(object message) { - Log.AsyncColour(message?.ToString(), ConsoleColor.Magenta); + Log.WarnDeprecated(); + Log.Monitor.LegacyLog(Log.GetModName(), message?.ToString(), ConsoleColor.Magenta); } /// Asynchronously log a warning to the console. /// The message to log. public static void Warning(object message) { - Log.AsyncColour("[WARN] " + message, ConsoleColor.DarkRed); + Log.WarnDeprecated(); + Log.Monitor.LegacyLog(Log.GetModName(), message?.ToString(), ConsoleColor.Yellow, LogLevel.Warn); } /// Asynchronously log an error to the console. /// The message to log. public static void Error(object message) { - Log.AsyncR("[ERROR] " + message); + Log.WarnDeprecated(); + Log.Monitor.LegacyLog(Log.GetModName(), message?.ToString(), ConsoleColor.Red, LogLevel.Error); } /// Asynchronously log a success message to the console. /// The message to log. public static void Success(object message) { + Log.WarnDeprecated(); Log.AsyncG(message); } @@ -139,31 +154,44 @@ namespace StardewModdingAPI /// The message to log. public static void Info(object message) { - Log.AsyncY(message); + Log.WarnDeprecated(); + Log.Monitor.LegacyLog(Log.GetModName(), message?.ToString(), ConsoleColor.White, LogLevel.Info); } /// Asynchronously log a debug message to the console. /// The message to log. public static void Debug(object message) { - Log.AsyncColour(message, ConsoleColor.DarkGray); + Log.WarnDeprecated(); + Log.Monitor.LegacyLog(Log.GetModName(), message?.ToString(), ConsoleColor.DarkGray); } /// Asynchronously log a message to the file that's not shown in the console. /// The message to log. internal static void LogToFile(string message) { - Task.Run(() => { Log.PrintLog(new LogInfo(message) { PrintConsole = false }); }); + Log.WarnDeprecated(); + Log.Monitor.LegacyLog(Log.GetModName(), message, ConsoleColor.DarkGray, LogLevel.Trace); } + /********* ** Private methods *********/ - /// Write a message to the log. - /// The message to write. - private static void PrintLog(LogInfo message) + /// Raise a deprecation warning. + private static void WarnDeprecated() + { + if (!Log.WarnedDeprecated) + { + Log.WarnedDeprecated = true; + Program.DeprecationManager.Warn($"the {nameof(Log)} class", "1.1", DeprecationLevel.Notice); + } + } + + /// Get the name of the mod logging a message from the stack. + private static string GetModName() { - Log.Writer.WriteToLog(message); + return Log.ModRegistry.GetModFromStack() ?? ""; } } } \ No newline at end of file diff --git a/src/StardewModdingAPI/LogWriter.cs b/src/StardewModdingAPI/LogWriter.cs index c8c9b9e1..11bcf5f3 100644 --- a/src/StardewModdingAPI/LogWriter.cs +++ b/src/StardewModdingAPI/LogWriter.cs @@ -1,7 +1,5 @@ using System; -using System.Collections.Concurrent; -using System.IO; -using System.Linq; +using StardewModdingAPI.Framework; namespace StardewModdingAPI { @@ -13,79 +11,51 @@ namespace StardewModdingAPI /********* ** Properties *********/ - /// The queued messages to flush. - private readonly ConcurrentQueue Queue; - - /// The underlying file stream. - private readonly StreamWriter FileStream; + /// Manages reading and writing to the log file. + private readonly LogFileManager LogFile; /********* ** Public methods *********/ /// Construct an instance. - /// The log path to write. - public LogWriter(string path) + /// Manages reading and writing to the log file. + internal LogWriter(LogFileManager logFile) { - // create log directory (required for stream writer) - Directory.CreateDirectory(Path.GetDirectoryName(path)); - - // initialise - this.Queue = new ConcurrentQueue(); - this.FileStream = new StreamWriter(Constants.LogPath, false); + this.WarnDeprecated(); + this.LogFile = logFile; } /// Queue a message for output. /// The message to log. public void WriteToLog(string message) { - lock (this.Queue) - { - var logEntry = new LogInfo(message); - this.Queue.Enqueue(logEntry); - - if (this.Queue.Any()) - this.FlushLog(); - } + this.WarnDeprecated(); + this.WriteToLog(new LogInfo(message)); } /// Queue a message for output. /// The message to log. public void WriteToLog(LogInfo message) { - lock (this.Queue) + this.WarnDeprecated(); + string output = $"[{message.LogTime}] {message.Message}"; + if (message.PrintConsole) { - this.Queue.Enqueue(message); - if (this.Queue.Any()) - this.FlushLog(); + Console.ForegroundColor = message.Colour; + Console.WriteLine(message); + Console.ForegroundColor = ConsoleColor.Gray; } + this.LogFile.WriteLine(output); } - /********* ** Private methods *********/ - /// Flush the underlying queue to the console and file. - private void FlushLog() + /// Raise a deprecation warning. + private void WarnDeprecated() { - lock (this.FileStream) - { - LogInfo entry; - while (this.Queue.TryDequeue(out entry)) - { - string message = $"[{entry.LogTime}] {entry.Message}"; - - if (entry.PrintConsole) - { - Console.ForegroundColor = entry.Colour; - Console.WriteLine(message); - Console.ForegroundColor = ConsoleColor.Gray; - } - - this.FileStream.WriteLine(message); - } - this.FileStream.Flush(); - } + Program.DeprecationManager.Warn($"the {nameof(LogWriter)} class", "1.0", DeprecationLevel.Notice); } } } \ No newline at end of file diff --git a/src/StardewModdingAPI/Program.cs b/src/StardewModdingAPI/Program.cs index 859d3659..f4416e70 100644 --- a/src/StardewModdingAPI/Program.cs +++ b/src/StardewModdingAPI/Program.cs @@ -103,6 +103,10 @@ namespace StardewModdingAPI } } + // initialise legacy log + Log.Monitor = new Monitor("legacy mod", Program.LogFile) { ShowTraceInConsole = Program.DeveloperMode }; + Log.ModRegistry = Program.ModRegistry; + // hook into & launch the game try { -- cgit