using System;
using SweetLib.Utils.Logger.Memory;
using SweetLib.Utils.Logger.Message;
namespace SweetLib.Utils.Logger
{
///
/// Enum which contains the several log levels.
///
[Flags]
public enum LogLevel
{
None = 0,
Trace = 1 << 0,
Debug = 1 << 1,
Info = 1 << 2,
Warn = 1 << 3,
Error = 1 << 4,
All = Int32.MaxValue,
};
///
/// Global logger class providing several methods to log events by the application.
///
///
/// As a will be used. You can change this to any other implementation at any time while runtime.
/// Default log levels are set as bitflags in .
///
public static class Logger
{
///
/// The global log level. Only messages with the set will be procedered.
///
public static LogLevel GlobalLogLevel { get; set; } = LogLevel.Info | LogLevel.Warn | LogLevel.Error;
///
/// The default which will be used for any logging action, if no custom is set as parameter.
///
public static ILogMemory DefaultLogMemory = new ArchivableConsoleLogMemory();
///
/// Will log a message into the global .
///
/// The log level of this message.
/// The message to log.
public static void Log(LogLevel logLevel, string message)
{
Log(logLevel, message, DefaultLogMemory);
}
///
/// Will log a message into the provided .
///
/// The log level of this message.
/// /// The message to log.
/// The to store the into.
public static void Log(LogLevel logLevel, string message, ILogMemory logMemory)
{
Log(new LogMessage(logLevel, message), logMemory);
}
///
/// Will log a message into the provided .
///
/// A object to store.
/// The to store the into.
/// In general use cases you should either use one of the or methods which will generate a call to this method.
public static void Log(LogMessage message, ILogMemory logMemory)
{
if (message == null)
throw new ArgumentNullException(nameof(message));
if (logMemory == null)
throw new ArgumentNullException(nameof(logMemory));
if ((GlobalLogLevel & message.LogLevel) == LogLevel.None)
return;
logMemory.Remember(message);
}
///
/// Will log a message with the log level.
///
/// Message to log.
public static void Trace(string message)
{
Log(LogLevel.Trace, message);
}
///
/// Will log a message with the log level.
///
/// Message to log.
public static void Debug(string message)
{
Log(LogLevel.Debug, message);
}
///
/// Will log a message with the log level.
///
/// Message to log.
public static void Info(string message)
{
Log(LogLevel.Info, message);
}
///
/// Will log a message with the log level.
///
/// Message to log.
public static void Warn(string message)
{
Log(LogLevel.Warn, message);
}
///
/// Will log a message with the log level.
///
/// Message to log.
public static void Error(string message)
{
Log(LogLevel.Error, message);
}
}
}