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 +++++++++++++++++++++++++++++--------------- 1 file changed, 57 insertions(+), 29 deletions(-) (limited to 'src/StardewModdingAPI/Log.cs') 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 -- cgit