//+------------------------------------------------------------------+ //| SimpleLogger.mqh | //| Copyright 2025, Niquel Mendoza. | //| https://www.mql5.com/es/users/nique_372/news | //+------------------------------------------------------------------+ #property copyright "Copyright 2025, Niquel Mendoza." #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 VERBOSE_LOG_LEVEL_WARNINGS_INFO = LOG_LEVEL_WARNING | LOG_LEVEL_INFO, // Warnings + Info + Errors VERBOSE_LOG_LEVEL_WARNINGS_CAUTION = LOG_LEVEL_WARNING | LOG_LEVEL_CAUTION, // Warnings + Caution + Errors 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 }; #define WARNING_TEXT "WARNING" #define CAUTION_TEXT "CAUTION" #define INFO_TEXT "INFO" #define ERROR_TEXT "ERROR" #define CRITICAL_ERROR_TEXT "CRITICAL ERROR" #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 __forceinline void LogWarning(const string &message, const string &function) const; __forceinline void LogInfo(const string &message, const string &function) const; __forceinline void LogCaution(const string &message, const string &function) const; public: CLoggerBase(); //--- static __forceinline void LogError(const string &message, const string &function); static __forceinline void LogFatalError(const string &message, const string &function); static __forceinline void LogCriticalError(const string &message, const string &function); //--- Methods to check if a flag is active __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(); }; //+------------------------------------------------------------------+ //| Constructor | //+------------------------------------------------------------------+ CLoggerBase::CLoggerBase(void) { this.m_log_flags = LOG_LEVEL_ERROR; this.m_last_flags = LOG_LEVEL_ERROR; UpdateLogLevels(); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void CLoggerBase::DisableAllLogs() { m_log_flags = LOG_LEVEL_ERROR; UpdateLogLevels(); } //+------------------------------------------------------------------+ void CLoggerBase::EnableAllLogs(void) { m_log_flags |= LOG_ALL; UpdateLogLevels(); } //+------------------------------------------------------------------+ inline void CLoggerBase::AddLogFlags(const uint8_t flags) { this.m_last_flags = m_log_flags; m_log_flags |= flags; UpdateLogLevels(); } //+------------------------------------------------------------------+ void CLoggerBase::RemoveLogFlags(const uint8_t flags) { this.m_last_flags = m_log_flags; uint8_t safe_flags = flags & ~LOG_LEVEL_ERROR; RemoveFlag(safe_flags); m_log_flags |= LOG_LEVEL_ERROR; UpdateLogLevels(); } //+------------------------------------------------------------------+ //| Método principal de logging | //+------------------------------------------------------------------+ #define FastLog(function, class_info, message) ::Print("[", class_info, "] ", function, " | ", message) //+------------------------------------------------------------------+ //| Métodos específicos por flag | //+------------------------------------------------------------------+ static __forceinline void CLoggerBase::LogError(const string &message, const string& function) { FastLog(function, ERROR_TEXT, message); } //+------------------------------------------------------------------+ static __forceinline void CLoggerBase::LogCriticalError(const string &message, const string& function) { FastLog(function, CRITICAL_ERROR_TEXT, message); } //+------------------------------------------------------------------+ static __forceinline void CLoggerBase::LogFatalError(const string &message, const string& function) { FastLog(function, FATAL_ERROR_TEXT, message); } //+------------------------------------------------------------------+ __forceinline void CLoggerBase::LogWarning(const string &message, const string& function) const { if(m_warning_enable) FastLog(function, WARNING_TEXT, message); } //+------------------------------------------------------------------+ __forceinline void CLoggerBase::LogInfo(const string &message, const string& function) const { if(m_info_enable) FastLog(function, INFO_TEXT, message); } //+------------------------------------------------------------------+ __forceinline void CLoggerBase::LogCaution(const string &message, const string& function) const { if(m_caution_enable) FastLog(function, CAUTION_TEXT, message); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void CLoggerBase::UpdateLogLevels(void) { this.m_warning_enable = (m_log_flags & LOG_LEVEL_WARNING) != 0; this.m_info_enable = (m_log_flags & LOG_LEVEL_INFO) != 0; this.m_caution_enable = (m_log_flags & LOG_LEVEL_CAUTION) != 0; } #endif //+------------------------------------------------------------------+