// Copyright (c) 2012-2023 Wojciech Figat. All rights reserved. using System; using System.IO; namespace Flax.Build { /// /// The logging interface for the build system /// public static class Log { private static object _consoleLocker = new object(); private static FileStream _logFile; private static StreamWriter _logFileWriter; private static DateTime _startTime; private static ConsoleColor _defaultColor; /// /// The indent applied to the log messages. /// public static string Indent = string.Empty; /// /// If set to true the console will be colored for warning and errors messages. /// public static bool ApplyConsoleColors = true; internal static void Init() { _startTime = DateTime.Now; _defaultColor = Console.ForegroundColor; if (!string.IsNullOrEmpty(Configuration.LogFile)) { var path = Path.GetDirectoryName(Configuration.LogFile); if (!string.IsNullOrEmpty(path) && !Directory.Exists(path)) Directory.CreateDirectory(path); _logFile = new FileStream(Configuration.LogFile, FileMode.Create, FileAccess.Write, FileShare.Read); _logFileWriter = new StreamWriter(_logFile); } } internal static void Dispose() { if (_logFile != null) { _logFileWriter.Dispose(); _logFile.Dispose(); } } /// /// Writes the specified message to the log. /// /// The message. /// The color. /// If set to true console will get the log. public static void Write(string message, ConsoleColor color, bool consoleLog) { if (consoleLog) { lock (_consoleLocker) { if (ApplyConsoleColors) Console.ForegroundColor = color; Console.WriteLine(Indent + message); if (ApplyConsoleColors) Console.ResetColor(); if (System.Diagnostics.Debugger.IsAttached) System.Diagnostics.Debug.WriteLine(Indent + message); } } else if (Configuration.LogFileWithConsole) return; if (_logFile != null) { TimeSpan span = DateTime.Now - _startTime; string prefix = string.Format("[{0:00}:{1:00}:{2:000}] ", span.Minutes, span.Seconds, span.Milliseconds); lock (_logFile) { if (Configuration.LogFileWithConsole) _logFileWriter.WriteLine(message); else _logFileWriter.WriteLine(prefix + Indent + message); } } } /// /// Logs the verbose message. /// /// The message. public static void Verbose(string message) { Write(message, _defaultColor, Configuration.ConsoleLog && Configuration.Verbose); } /// /// Logs the information. /// /// The message. public static void Info(string message) { Write(message, _defaultColor, Configuration.ConsoleLog && !Configuration.LogMessagesOnly); } /// /// Logs the information. /// /// The message. public static void Message(string message) { Write(message, _defaultColor, Configuration.ConsoleLog); } /// /// Logs the warning message. /// /// The message. public static void Warning(string message) { Write(message, ConsoleColor.Yellow, true); } /// /// Logs the error message. /// /// The message. public static void Error(string message) { Write(message, ConsoleColor.Red, true); } /// /// Logs the verbose message. /// /// The message. /// The flag used to indicate whether this log was already sent. public static void VerboseOnce(string message, ref bool flag) { if (flag) return; flag = true; Write(message, _defaultColor, Configuration.ConsoleLog && Configuration.Verbose); } /// /// Logs the information. /// /// The message. /// The flag used to indicate whether this log was already sent. public static void InfoOnce(string message, ref bool flag) { if (flag) return; flag = true; Write(message, _defaultColor, Configuration.ConsoleLog); } /// /// Logs the warning message. /// /// The message. /// The flag used to indicate whether this log was already sent. public static void WarningOnce(string message, ref bool flag) { if (flag) return; flag = true; Write(message, ConsoleColor.Yellow, true); } /// /// Logs the error message. /// /// The message. /// The flag used to indicate whether this log was already sent. public static void ErrorOnce(string message, ref bool flag) { if (flag) return; flag = true; Write(message, ConsoleColor.Red, true); } /// /// Logs the exception. /// /// The exception. public static void Exception(Exception ex) { Write(string.Format("Exception: {0}", ex.Message), ConsoleColor.Red, true); Write("Stack trace:", ConsoleColor.Yellow, true); Write(ex.StackTrace, ConsoleColor.Yellow, true); if (ex.InnerException != null) { Warning("Inner exception:"); Exception(ex.InnerException); } } } }