144 lines
4.5 KiB
C#
144 lines
4.5 KiB
C#
// Copyright (c) 2012-2021 Wojciech Figat. All rights reserved.
|
|
|
|
using System;
|
|
using System.IO;
|
|
|
|
namespace Flax.Build
|
|
{
|
|
/// <summary>
|
|
/// The logging interface for the build system
|
|
/// </summary>
|
|
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;
|
|
|
|
/// <summary>
|
|
/// The indent applied to the log messages.
|
|
/// </summary>
|
|
public static string Indent = string.Empty;
|
|
|
|
/// <summary>
|
|
/// If set to true the console will be colored for warning and errors messages.
|
|
/// </summary>
|
|
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);
|
|
_logFileWriter = new StreamWriter(_logFile);
|
|
}
|
|
}
|
|
|
|
internal static void Dispose()
|
|
{
|
|
if (_logFile != null)
|
|
{
|
|
_logFileWriter.Dispose();
|
|
_logFile.Dispose();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Writes the specified message to the log.
|
|
/// </summary>
|
|
/// <param name="message">The message.</param>
|
|
/// <param name="color">The color.</param>
|
|
/// <param name="consoleLog">If set to <c>true</c> console will get the log.</param>
|
|
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);
|
|
}
|
|
}
|
|
|
|
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)
|
|
{
|
|
_logFileWriter.WriteLine(prefix + Indent + message);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Logs the verbose message.
|
|
/// </summary>
|
|
/// <param name="message">The message.</param>
|
|
public static void Verbose(string message)
|
|
{
|
|
Write(message, _defaultColor, Configuration.ConsoleLog && Configuration.Verbose);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Logs the information.
|
|
/// </summary>
|
|
/// <param name="message">The message.</param>
|
|
public static void Info(string message)
|
|
{
|
|
Write(message, _defaultColor, Configuration.ConsoleLog);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Logs the warning message.
|
|
/// </summary>
|
|
/// <param name="message">The message.</param>
|
|
public static void Warning(string message)
|
|
{
|
|
Write(message, ConsoleColor.Yellow, true);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Logs the error message.
|
|
/// </summary>
|
|
/// <param name="message">The message.</param>
|
|
public static void Error(string message)
|
|
{
|
|
Write(message, ConsoleColor.Red, true);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Logs the exception.
|
|
/// </summary>
|
|
/// <param name="ex">The exception.</param>
|
|
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);
|
|
}
|
|
}
|
|
}
|
|
}
|