using System; using System.IO; using System.Threading; using System.Threading.Tasks; using StardewModdingAPI.Framework; namespace StardewModdingAPI { /// A singleton which logs messages to the SMAPI console and log file. public static class Log { /********* ** Properties *********/ /// A pseudorandom number generator used to generate log files. private static readonly Random Random = new Random(); /// The underlying log writer. private static readonly LogWriter Writer = new LogWriter(Constants.LogPath); /********* ** Public methods *********/ /**** ** Exceptions ****/ /// Log an exception event. /// The event sender. /// 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 a thread exception event. /// The event sender. /// 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()); } /**** ** 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.PrintLog(new LogInfo(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) { Task.Run(() => { Log.PrintLog(new LogInfo(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); } /// Asynchronously log a red message to the console. /// The message to log. public static void AsyncR(object message) { Log.AsyncColour(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); } /// Asynchronously log a yellow message to the console. /// The message to log. public static void AsyncY(object message) { Log.AsyncColour(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); } /// Asynchronously log a cyan message to the console. /// The message to log. public static void AsyncC(object message) { Log.AsyncColour(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); } /// Asynchronously log a warning to the console. /// The message to log. public static void Warning(object message) { Log.AsyncColour("[WARN] " + message, ConsoleColor.DarkRed); } /// Asynchronously log an error to the console. /// The message to log. public static void Error(object message) { Log.AsyncR("[ERROR] " + message); } /// Asynchronously log a success message to the console. /// The message to log. public static void Success(object message) { Log.AsyncG(message); } /// Asynchronously log an info message to the console. /// The message to log. public static void Info(object message) { Log.AsyncY(message); } /// Asynchronously log a debug message to the console. /// The message to log. public static void Debug(object message) { Log.AsyncColour(message, 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 }); }); } /********* ** Private methods *********/ /// Write a message to the log. /// The message to write. private static void PrintLog(LogInfo message) { Log.Writer.WriteToLog(message); } } }