using System; using System.Collections.Generic; using System.Linq; namespace StardewModdingAPI.Framework { /// Encapsulates monitoring and logic for a given module. internal class Monitor : IMonitor { /********* ** Properties *********/ /// The name of the module which logs messages using this instance. private readonly string Source; /// The log file to which to write messages. private readonly LogFileManager LogFile; /// The maximum length of the values. private static readonly int MaxLevelLength = (from level in Enumerable.Cast(Enum.GetValues(typeof(LogLevel))) select level.ToString().Length).Max(); /// The console text color for each log level. private static readonly Dictionary Colors = new Dictionary { [LogLevel.Trace] = ConsoleColor.DarkGray, [LogLevel.Debug] = ConsoleColor.DarkGray, [LogLevel.Info] = ConsoleColor.White, [LogLevel.Warn] = ConsoleColor.Yellow, [LogLevel.Error] = ConsoleColor.Red, [LogLevel.Alert] = ConsoleColor.Magenta }; /********* ** Accessors *********/ /// Whether the current console supports color codes. internal static readonly bool ConsoleSupportsColor = Monitor.GetConsoleSupportsColor(); /// Whether to show trace messages in the console. internal bool ShowTraceInConsole { get; set; } /// Whether to write anything to the console. This should be disabled if no console is available. internal bool WriteToConsole { get; set; } = true; /********* ** Public methods *********/ /// Construct an instance. /// The name of the module which logs messages using this instance. /// The log file to which to write messages. public Monitor(string source, LogFileManager logFile) { // validate if (string.IsNullOrWhiteSpace(source)) throw new ArgumentException("The log source cannot be empty."); if (logFile == null) throw new ArgumentNullException(nameof(logFile), "The log file manager cannot be null."); // initialise this.Source = source; this.LogFile = logFile; } /// Log a message for the player or developer. /// The message to log. /// The log severity level. public void Log(string message, LogLevel level = LogLevel.Debug) { this.LogImpl(this.Source, message, Monitor.Colors[level], level); } /// Immediately exit the game without saving. This should only be invoked when an irrecoverable fatal error happens that risks save corruption or game-breaking bugs. /// The reason for the shutdown. public void ExitGameImmediately(string reason) { Program.ExitGameImmediately(this.Source, reason); Program.gamePtr.Exit(); } /// Log a fatal error message. /// The message to log. internal void LogFatal(string message) { Console.BackgroundColor = ConsoleColor.Red; this.LogImpl(this.Source, message, ConsoleColor.White, LogLevel.Error); } /// Log a message for the player or developer, using the specified console color. /// The name of the mod logging the message. /// The message to log. /// The console color. /// The log level. [Obsolete("This method is provided for backwards compatibility and otherwise should not be used. Use " + nameof(Monitor) + "." + nameof(Monitor.Log) + " instead.")] internal void LegacyLog(string source, string message, ConsoleColor color, LogLevel level = LogLevel.Debug) { this.LogImpl(source, message, color, level); } /********* ** Private methods *********/ /// Write a message line to the log. /// The name of the mod logging the message. /// The message to log. /// The console color. /// The log level. private void LogImpl(string source, string message, ConsoleColor color, LogLevel level) { // generate message string levelStr = level.ToString().ToUpper().PadRight(Monitor.MaxLevelLength); message = $"[{DateTime.Now:HH:mm:ss} {levelStr} {source}] {message}"; // log if (this.WriteToConsole && (this.ShowTraceInConsole || level != LogLevel.Trace)) { if (Monitor.ConsoleSupportsColor) { Console.ForegroundColor = color; Console.WriteLine(message); Console.ResetColor(); } else Console.WriteLine(message); } this.LogFile.WriteLine(message); } /// Test whether the current console supports color formatting. private static bool GetConsoleSupportsColor() { try { Console.ForegroundColor = Console.ForegroundColor; return true; } catch (Exception) { return false; // Mono bug } } } }