using System; using System.Threading; 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 { /********* ** Accessors *********/ /// The underlying logger. internal static Monitor Monitor; /// Tracks the installed mods. internal static ModRegistry ModRegistry; /********* ** Public methods *********/ /**** ** Exceptions ****/ /// Log an exception event. /// The event sender. /// The event arguments. public static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) { Log.WarnDeprecated(); Log.Monitor.Log($"Critical app domain exception: {e.ExceptionObject}", LogLevel.Error); } /// Log a thread exception event. /// The event sender. /// The event arguments. public static void Application_ThreadException(object sender, ThreadExceptionEventArgs e) { Log.WarnDeprecated(); Log.Monitor.Log($"Critical thread exception: {e.Exception}", LogLevel.Error); } /**** ** Synchronous logging ****/ /// Synchronously log a message to the console. NOTE: synchronous logging is discouraged; use asynchronous methods instead. /// The message to log. /// The message color. public static void SyncColour(object message, ConsoleColor color) { Log.WarnDeprecated(); Log.Monitor.LegacyLog(Log.GetModName(), message?.ToString(), color); } /**** ** Asynchronous logging ****/ /// Asynchronously log a message to the console with the specified color. /// The message to log. /// The message color. public static void AsyncColour(object message, ConsoleColor 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.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.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.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.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.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.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.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.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.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); } /// Asynchronously log an info message to the console. /// The message to log. public static void Info(object 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.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) { Log.WarnDeprecated(); Log.Monitor.LegacyLog(Log.GetModName(), message, ConsoleColor.DarkGray, LogLevel.Trace); } /********* ** Private methods *********/ /// Raise a deprecation warning. private static void WarnDeprecated() { Program.DeprecationManager.Warn("an unknown mod", $"the {nameof(Log)} class", "1.1", DeprecationLevel.Notice); } /// Get the name of the mod logging a message from the stack. private static string GetModName() { return Log.ModRegistry.GetModFromStack() ?? ""; } } }