// Copyright (c) 2012-2024 Wojciech Figat. All rights reserved.
using System;
using System.Runtime.CompilerServices;
namespace FlaxEngine
{
///
/// Initializes a new instance of the Logger.
///
[HideInEditor]
public class Logger : ILogger
{
private const string TagFormat = "{0}: {1}";
///
/// Create a custom Logger.
///
/// Pass in default log handler or custom log handler.
public Logger(ILogHandler logHandler)
{
LogHandler = logHandler;
LogEnabled = true;
FilterLogType = LogType.Info;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static string GetString(object message)
{
return message?.ToString() ?? "null";
}
///
/// To selective enable debug log message.
///
public LogType FilterLogType { get; set; }
///
/// To runtime toggle debug logging [ON/OFF].
///
public bool LogEnabled { get; set; }
///
/// Set Logger.ILogHandler.
///
public ILogHandler LogHandler { get; set; }
///
/// Check logging is enabled based on the LogType.
///
/// The type of the log message.
/// Returns true in case logs of LogType will be logged otherwise returns false.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool IsLogTypeAllowed(LogType logType)
{
return LogEnabled && logType >= FilterLogType;
}
///
/// Logs message to the Flax Console using default logger.
///
/// The type of the log message.
/// String or object to be converted to string representation for display.
public void Log(LogType logType, object message)
{
if (IsLogTypeAllowed(logType))
LogHandler.Log(logType, null, GetString(message));
}
///
/// Logs message to the Flax Console using default logger.
///
/// The type of the log message.
/// String or object to be converted to string representation for display.
/// Object to which the message applies.
public void Log(LogType logType, object message, Object context)
{
if (IsLogTypeAllowed(logType))
LogHandler.Log(logType, context, GetString(message));
}
///
/// Logs message to the Flax Console using default logger.
///
/// The type of the log message.
///
/// Used to identify the source of a log message. It usually identifies the class where the log call
/// occurs.
///
/// String or object to be converted to string representation for display.
public void Log(LogType logType, string tag, object message)
{
if (IsLogTypeAllowed(logType))
LogHandler.Log(logType, null, string.Format(TagFormat, tag, GetString(message)));
}
///
/// Logs message to the Flax Console using default logger.
///
/// The type of the log message.
///
/// Used to identify the source of a log message. It usually identifies the class where the log call
/// occurs.
///
/// String or object to be converted to string representation for display.
/// Object to which the message applies.
public void Log(LogType logType, string tag, object message, Object context)
{
if (IsLogTypeAllowed(logType))
LogHandler.Log(logType, context, string.Format(TagFormat, tag, GetString(message)));
}
///
/// Logs message to the Flax Console using default logger.
///
/// String or object to be converted to string representation for display.
public void Log(object message)
{
if (IsLogTypeAllowed(LogType.Info))
LogHandler.Log(LogType.Info, null, GetString(message));
}
///
/// Logs message to the Flax Console using default logger.
///
///
/// Used to identify the source of a log message. It usually identifies the class where the log call
/// occurs.
///
/// String or object to be converted to string representation for display.
public void Log(string tag, object message)
{
if (IsLogTypeAllowed(LogType.Info))
LogHandler.Log(LogType.Info, null, string.Format(TagFormat, tag, GetString(message)));
}
///
/// Logs message to the Flax Console using default logger.
///
///
/// Used to identify the source of a log message. It usually identifies the class where the log call
/// occurs.
///
/// String or object to be converted to string representation for display.
/// Object to which the message applies.
public void Log(string tag, object message, Object context)
{
if (IsLogTypeAllowed(LogType.Info))
LogHandler.Log(LogType.Info, context, string.Format(TagFormat, tag, GetString(message)));
}
///
/// A variant of Logger.Info that logs an error message.
///
///
/// Used to identify the source of a log message. It usually identifies the class where the log call
/// occurs.
///
/// String or object to be converted to string representation for display.
public void LogError(string tag, object message)
{
if (IsLogTypeAllowed(LogType.Error))
LogHandler.Log(LogType.Error, null, string.Format(TagFormat, tag, GetString(message)));
}
///
/// A variant of Logger.Info that logs an error message.
///
///
/// Used to identify the source of a log message. It usually identifies the class where the log call
/// occurs.
///
/// String or object to be converted to string representation for display.
/// Object to which the message applies.
public void LogError(string tag, object message, Object context)
{
if (IsLogTypeAllowed(LogType.Error))
LogHandler.Log(LogType.Error, context, string.Format(TagFormat, tag, GetString(message)));
}
///
/// A variant of Logger.Info that logs an exception message.
///
/// Runtime Exception.
public void LogException(Exception exception)
{
if (LogEnabled)
LogHandler.LogException(exception, null);
}
///
/// A variant of Logger.Info that logs an exception message.
///
/// Runtime Exception.
/// Object to which the message applies.
public void LogException(Exception exception, Object context)
{
if (LogEnabled)
LogHandler.LogException(exception, context);
}
///
public void Log(LogType logType, Object context, string message)
{
if (IsLogTypeAllowed(logType))
LogHandler.Log(logType, context, message);
}
///
public void LogFormat(LogType logType, string format, params object[] args)
{
if (IsLogTypeAllowed(logType))
LogHandler.Log(logType, null, string.Format(format, args));
}
///
/// Logs a formatted message.
///
/// The type of the log message.
/// A composite format string.
/// Format arguments.
public void Log(LogType logType, string format, params object[] args)
{
if (IsLogTypeAllowed(logType))
LogHandler.Log(logType, null, string.Format(format, args));
}
///
/// Logs a formatted message.
///
/// The type of the log message.
/// Object to which the message applies.
/// A composite format string.
/// Format arguments.
public void Log(LogType logType, Object context, string format, params object[] args)
{
if (IsLogTypeAllowed(logType))
LogHandler.Log(logType, context, string.Format(format, args));
}
///
/// A variant of Logger.Info that logs an warning message.
///
///
/// Used to identify the source of a log message. It usually identifies the class where the log call
/// occurs.
///
/// String or object to be converted to string representation for display.
public void LogWarning(string tag, object message)
{
if (IsLogTypeAllowed(LogType.Warning))
LogHandler.Log(LogType.Warning, null, string.Format(TagFormat, tag, GetString(message)));
}
///
/// A variant of Logger.Info that logs an warning message.
///
///
/// Used to identify the source of a log message. It usually identifies the class where the log call
/// occurs.
///
/// String or object to be converted to string representation for display.
/// Object to which the message applies.
public void LogWarning(string tag, object message, Object context)
{
if (IsLogTypeAllowed(LogType.Warning))
LogHandler.Log(LogType.Warning, context, string.Format(TagFormat, tag, GetString(message)));
}
}
}