2022-07-07 14:58:12 +02:00
// <copyright file="VoiceAttackLog.cs" company="alterNERDtive">
// Copyright 2022 alterNERDtive.
//
// This file is part of YAVAPF.
//
// YAVAPF is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// YAVAPF is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with YAVAPF. If not, see <https://www.gnu.org/licenses/>.
// </copyright>
using System ;
using VoiceAttack ;
namespace alterNERDtive.Yavapf
{
2022-07-08 20:22:50 +02:00
/// <summary>
/// Exposes methods for writing to the VoiceAttack event log.
/// </summary>
2022-07-07 14:58:12 +02:00
public class VoiceAttackLog
{
private static readonly string [ ] LogColour = { "red" , "yellow" , "green" , "blue" , "gray" } ;
private static LogLevel ? logLevel ;
private readonly VoiceAttackInitProxyClass vaProxy ;
private readonly string id ;
2022-07-08 20:22:50 +02:00
/// <summary>
/// Initializes a new instance of the <see cref="VoiceAttackLog"/> class.
/// </summary>
/// <param name="vaProxy">A VoiceAttackInitProxyClass object to interface with.</param>
/// <param name="name">The plugin name.</param>
public VoiceAttackLog ( VoiceAttackInitProxyClass vaProxy , string name )
2022-07-07 14:58:12 +02:00
{
this . vaProxy = vaProxy ;
2022-07-08 20:22:50 +02:00
this . id = name ;
2022-07-07 14:58:12 +02:00
}
2022-07-08 20:22:50 +02:00
/// <summary>
/// Gets or sets the current log level. Default is NOTICE.
/// </summary>
2022-07-07 14:58:12 +02:00
public LogLevel ? LogLevel
{
get = > logLevel ? ? Yavapf . LogLevel . NOTICE ;
set
{
2022-07-11 01:46:42 +02:00
if ( value ! = logLevel )
{
logLevel = value ;
this . vaProxy . SetText ( $"{this.id}.loglevel#" , value . ToString ( ) . ToLower ( ) ) ;
this . Notice ( $"Log level set to {value ?? Yavapf.LogLevel.NOTICE}." ) ;
}
2022-07-07 14:58:12 +02:00
}
}
2022-07-08 20:22:50 +02:00
/// <summary>
/// Sets the current log level.
///
/// Valid values are ERROR, WARN, NOTICE, INFO and DEBUG.
/// </summary>
/// <param name="level">The new log level.</param>
2022-07-11 01:46:42 +02:00
/// <exception cref="ArgumentException">Thrown when <paramref
/// name="level"/>is not a valid log level.</exception>
public void SetLogLevel ( string? level )
2022-07-07 14:58:12 +02:00
{
if ( level = = null )
{
this . LogLevel = null ;
}
else
{
this . LogLevel = ( LogLevel ) Enum . Parse ( typeof ( LogLevel ) , level . ToUpper ( ) ) ;
}
}
2022-07-08 20:22:50 +02:00
/// <summary>
/// Logs a given message with the ERROR log level.
/// </summary>
/// <param name="message">The message to be logged.</param>
2022-07-07 14:58:12 +02:00
public void Error ( string message ) = > this . Log ( message , Yavapf . LogLevel . ERROR ) ;
2022-07-08 20:22:50 +02:00
/// <summary>
/// Logs a given message with the WARN log level.
///
/// If the current log level is ERROR the message will not be logged.
/// </summary>
/// <param name="message">The message to be logged.</param>
2022-07-07 14:58:12 +02:00
public void Warn ( string message ) = > this . Log ( message , Yavapf . LogLevel . WARN ) ;
2022-07-08 20:22:50 +02:00
/// <summary>
/// Logs a given message with the NOTICE log level.
///
/// If the current log level is ERROR or WARN the message will not be
/// logged.
/// </summary>
/// <param name="message">The message to be logged.</param>
2022-07-07 14:58:12 +02:00
public void Notice ( string message ) = > this . Log ( message , Yavapf . LogLevel . NOTICE ) ;
2022-07-08 20:22:50 +02:00
/// <summary>
/// Logs a given message with the INFO log level.
///
/// If the current log level is ERROR, WARN or NOTICE the message will
/// not be logged.
/// </summary>
/// <param name="message">The message to be logged.</param>
2022-07-07 14:58:12 +02:00
public void Info ( string message ) = > this . Log ( message , Yavapf . LogLevel . INFO ) ;
2022-07-08 20:22:50 +02:00
/// <summary>
/// Logs a given message with the DEBUG log level.
///
/// If the current log level is not DEBUG the message will not be logged.
/// </summary>
/// <param name="message">The message to be logged.</param>
2022-07-07 14:58:12 +02:00
public void Debug ( string message ) = > this . Log ( message , Yavapf . LogLevel . DEBUG ) ;
2022-07-11 01:46:42 +02:00
/// <summary>
/// Logs a given message with the specified log level.
///
/// If the current log level is higher than the message’ s log level it
/// will not be logged.
/// </summary>
/// <param name="message">The message to be logged.</param>
/// <param name="level">The desired log level.</param>
/// <exception cref="ArgumentNullException">Thrown when the message is null.</exception>
private void Log ( string message , LogLevel level = Yavapf . LogLevel . INFO )
{
_ = message ? ? throw new ArgumentNullException ( "message" ) ;
if ( level < = this . LogLevel )
{
this . vaProxy . WriteToLog ( $"{level} | {this.id}: {message}" , LogColour [ ( int ) level ] ) ;
}
}
2022-07-07 14:58:12 +02:00
}
/// <summary>
2022-07-08 20:22:50 +02:00
/// Log levels that can be used when writing to the VoiceAttack event log.
2022-07-07 14:58:12 +02:00
/// </summary>
2022-07-08 20:22:50 +02:00
[System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.OrderingRules", "SA1201:Elements should appear in the correct order", Justification = "Well it’ s this or wrong file name, isn’ t it?")]
2022-07-07 14:58:12 +02:00
public enum LogLevel
{
/// <summary>
/// Log level for error messages. Errors cause execution to abort.
/// </summary>
ERROR ,
/// <summary>
/// Log level for warning messages. Warnings should not cause execution
/// to abort.
/// </summary>
WARN ,
/// <summary>
/// Log level for notices. Notices should be noteworthy.
/// </summary>
NOTICE ,
/// <summary>
/// Log level for informational messages. These should not be
/// noteworthy.
/// </summary>
INFO ,
/// <summary>
/// Log level for debug messages. They should be useful only for
/// debugging.
/// </summary>
DEBUG ,
}
}