MQLArticles/Utils/FA/SimpleLogger.mqh

197 lines
16 KiB
MQL5

2025-10-01 12:22:08 -05:00
<EFBFBD><EFBFBD>//+------------------------------------------------------------------+
//| SimpleLogger.mqh |
//| Copyright 2025, Niquel Mendoza. |
//| https://www.mql5.com/es/users/nique_372/news |
//+------------------------------------------------------------------+
2025-11-10 09:33:53 -05:00
#property copyright "Copyright 2025, Niquel Mendoza."
2025-10-01 12:22:08 -05:00
#property link "https://www.mql5.com/es/users/nique_372/news"
#property strict
#ifndef MQLARTICLES_UTILS_FA_SIMPLELOGGER_MQH
#define MQLARTICLES_UTILS_FA_SIMPLELOGGER_MQH
//+------------------------------------------------------------------+
//| Defines |
//+------------------------------------------------------------------+
//--- Macro
#define FUNCION_ACTUAL __FUNCTION__
//--- Log Flags
#define LOG_LEVEL_ERROR 1 // 0001 - Critical errors
#define LOG_LEVEL_WARNING 2 // 0010 - Warnings
#define LOG_LEVEL_INFO 4 // 0100 - General information
#define LOG_LEVEL_CAUTION 8 // 1000 - Cautions
#define LOG_ALL LOG_LEVEL_CAUTION|LOG_LEVEL_INFO|LOG_LEVEL_WARNING
//---
enum ENUM_VERBOSE_LOG_LEVEL
{
VERBOSE_LOG_LEVEL_ERROR_ONLY = LOG_LEVEL_ERROR, // Only Errors
VERBOSE_LOG_LEVEL_WARNINGS = LOG_LEVEL_WARNING, // Warnings + Errors
VERBOSE_LOG_LEVEL_INFO = LOG_LEVEL_INFO, // Info + Errors
VERBOSE_LOG_LEVEL_CAUTION = LOG_LEVEL_CAUTION, // Caution + Errors
2025-12-24 22:28:26 -05:00
VERBOSE_LOG_LEVEL_WARNINGS_INFO = LOG_LEVEL_WARNING | LOG_LEVEL_INFO, // Warnings + Info + Errors
2025-10-01 12:22:08 -05:00
VERBOSE_LOG_LEVEL_WARNINGS_CAUTION = LOG_LEVEL_WARNING | LOG_LEVEL_CAUTION, // Warnings + Caution + Errors
2026-01-02 10:22:09 -05:00
VERBOSE_LOG_LEVEL_INFO_CAUTION = LOG_LEVEL_INFO | LOG_LEVEL_CAUTION, // Info + Caution + Errors
VERBOSE_LOG_LEVEL_ALL = (LOG_LEVEL_WARNING | LOG_LEVEL_INFO | LOG_LEVEL_CAUTION) // All: Warnings + Info + Caution + Errors
};
2025-12-24 22:28:26 -05:00
2025-10-01 12:22:08 -05:00
#define WARNING_TEXT "WARNING"
2025-12-24 22:28:26 -05:00
#define CAUTION_TEXT "CAUTION"
2026-01-02 10:22:09 -05:00
#define INFO_TEXT "INFO"
2025-10-01 12:22:08 -05:00
#define ERROR_TEXT "ERROR"
2026-01-02 10:22:09 -05:00
#define CRITICAL_ERROR_TEXT "CRITICAL ERROR"
2025-10-01 12:22:08 -05:00
#define FATAL_ERROR_TEXT "FATAL ERROR"
//+------------------------------------------------------------------+
//| Base Logging Class with Flag System |
//+------------------------------------------------------------------+
class CLoggerBase
{
private:
uint8_t m_log_flags; // Active logging flags
uint8_t m_last_flags; // Last active logging flags
//---
bool m_warning_enable;
bool m_caution_enable;
bool m_info_enable;
//--- Main logging method with flags
inline void RemoveFlag(uint8_t flags) { this.m_log_flags &= ~flags; }
void UpdateLogLevels();
protected:
//--- Flag-specific methods
inline void LogWarning(const string &message, const string &function) const;
inline void LogInfo(const string &message, const string &function) const;
inline void LogCaution(const string &message, const string &function) const;
public:
CLoggerBase();
2025-11-17 10:27:47 -05:00
2025-10-01 12:22:08 -05:00
//---
static __forceinline void LogError(const string &message, const string &function);
2025-12-24 22:28:26 -05:00
2025-10-01 12:22:08 -05:00
static __forceinline void LogFatalError(const string &message, const string &function);
static __forceinline void LogCriticalError(const string &message, const string &function);
2025-12-24 22:28:26 -05:00
2025-10-01 12:22:08 -05:00
//--- Methods to check if a flag is active
2025-12-24 22:28:26 -05:00
2025-10-01 12:22:08 -05:00
__forceinline bool IsWarningLogEnabled() const { return m_warning_enable; }
__forceinline bool IsInfoLogEnabled() const { return m_info_enable; }
__forceinline bool IsCautionLogEnabled() const { return m_caution_enable; }
//--- Getters
__forceinline uint8_t LogFlags() const { return m_log_flags; }
inline bool IsLogEnabled(uint8_t flag) const { return (m_log_flags & flag) != 0; }
//--- Main configuration using flags
virtual inline void AddLogFlags(const uint8_t flags);
virtual void RemoveLogFlags(const uint8_t flags);
inline void ResetLastStateFlags() { this.m_log_flags = m_last_flags; }
//--- Disbale and Enable all logs
virtual void EnableAllLogs();
virtual void DisableAllLogs();
2025-12-24 22:28:26 -05:00
};
2025-10-01 12:22:08 -05:00