2017-04-12 17:08:54 +02:00
using System ;
using SweetLib.Utils.Logger.Memory ;
using SweetLib.Utils.Logger.Message ;
namespace SweetLib.Utils.Logger
{
2017-04-16 18:07:30 +02:00
/// <summary>
/// Enum which contains the several log levels.
/// </summary>
2017-04-12 17:08:54 +02:00
[Flags]
public enum LogLevel
{
None = 0 ,
Trace = 1 < < 0 ,
Debug = 1 < < 1 ,
Info = 1 < < 2 ,
Warn = 1 < < 3 ,
2017-04-12 17:44:02 +02:00
Error = 1 < < 4 ,
2017-04-16 18:07:30 +02:00
All = Int32 . MaxValue ,
2017-04-12 17:08:54 +02:00
} ;
2017-04-16 18:07:30 +02:00
/// <summary>
/// Global logger class providing several methods to log events by the application.
/// </summary>
/// <remarks>
/// As <see cref="DefaultLogMemory"/> a <see cref="ArchivableConsoleLogMemory"/> will be used. You can change this to any other implementation at any time while runtime.
/// Default log levels are set as bitflags in <see cref="GlobalLogLevel"/>.
/// </remarks>
2017-04-12 17:08:54 +02:00
public static class Logger
{
2017-04-16 18:07:30 +02:00
/// <summary>
/// The global log level. Only messages with the set <see cref="LogLevel"/> will be procedered.
/// </summary>
2017-04-12 17:08:54 +02:00
public static LogLevel GlobalLogLevel { get ; set ; } = LogLevel . Info | LogLevel . Warn | LogLevel . Error ;
2017-04-12 17:12:35 +02:00
2017-04-16 18:07:30 +02:00
/// <summary>
/// The default <see cref="ILogMemory"/> which will be used for any logging action, if no custom <see cref="ILogMemory"/> is set as parameter.
/// </summary>
2017-04-12 17:08:54 +02:00
public static ILogMemory DefaultLogMemory = new ArchivableConsoleLogMemory ( ) ;
2017-04-16 18:07:30 +02:00
/// <summary>
/// Will log a message into the global <see cref="DefaultLogMemory"/>.
/// </summary>
/// <param name="logLevel">The log level of this message.</param>
/// <param name="message">The message to log.</param>
2017-04-12 17:08:54 +02:00
public static void Log ( LogLevel logLevel , string message )
{
Log ( logLevel , message , DefaultLogMemory ) ;
}
2017-04-16 18:07:30 +02:00
/// <summary>
/// Will log a message into the provided <see cref="logMemory"/>.
/// </summary>
/// <param name="logLevel">The log level of this message.</param>
/// /// <param name="message">The message to log.</param>
/// <param name="logMemory">The <see cref="ILogMemory"/> to store the <see cref="message"/> into.</param>
2017-04-12 17:08:54 +02:00
public static void Log ( LogLevel logLevel , string message , ILogMemory logMemory )
{
Log ( new LogMessage ( logLevel , message ) , logMemory ) ;
}
2017-04-16 18:07:30 +02:00
/// <summary>
/// Will log a message into the provided <see cref="logMemory"/>.
/// </summary>
/// <param name="message">A <see cref="LogMessage"/> object to store.</param>
/// <param name="logMemory">The <see cref="ILogMemory"/> to store the <see cref="message"/> into.</param>
/// <remarks>In general use cases you should either use one of the <see cref="Log(SweetLib.Utils.Logger.LogLevel,string)"/> or <see cref="Log(SweetLib.Utils.Logger.LogLevel,string)"/> methods which will generate a call to this method.</remarks>
2017-04-12 17:08:54 +02:00
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 ) ;
}
2017-04-16 18:07:30 +02:00
/// <summary>
/// Will log a message with the <see cref="LogLevel.Trace"/> log level.
/// </summary>
/// <param name="message">Message to log.</param>
2017-04-12 17:08:54 +02:00
public static void Trace ( string message )
{
Log ( LogLevel . Trace , message ) ;
}
2017-04-16 18:07:30 +02:00
/// <summary>
/// Will log a message with the <see cref="LogLevel.Debug"/> log level.
/// </summary>
/// <param name="message">Message to log.</param>
2017-04-12 17:08:54 +02:00
public static void Debug ( string message )
{
Log ( LogLevel . Debug , message ) ;
}
2017-04-16 18:07:30 +02:00
/// <summary>
/// Will log a message with the <see cref="LogLevel.Info"/> log level.
/// </summary>
/// <param name="message">Message to log.</param>
2017-04-12 17:08:54 +02:00
public static void Info ( string message )
{
Log ( LogLevel . Info , message ) ;
}
2017-04-16 18:07:30 +02:00
/// <summary>
/// Will log a message with the <see cref="LogLevel.Warn"/> log level.
/// </summary>
/// <param name="message">Message to log.</param>
2017-04-12 17:08:54 +02:00
public static void Warn ( string message )
{
Log ( LogLevel . Warn , message ) ;
}
2017-04-16 18:07:30 +02:00
/// <summary>
/// Will log a message with the <see cref="LogLevel.Error"/> log level.
/// </summary>
/// <param name="message">Message to log.</param>
2017-04-12 17:08:54 +02:00
public static void Error ( string message )
{
Log ( LogLevel . Error , message ) ;
}
}
}