forked from nique_372/MQLArticles
		
	
		
			
				
	
	
		
			194 lines
		
	
	
	
		
			16 KiB
		
	
	
	
		
			MQL5
		
	
	
	
	
	
			
		
		
	
	
			194 lines
		
	
	
	
		
			16 KiB
		
	
	
	
		
			MQL5
		
	
	
	
	
	
//+------------------------------------------------------------------+
 | 
						|
//|                                                 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 UTILS_FA_SIMPLE_LOGGER_MQH
 | 
						|
#define UTILS_FA_SIMPLE_LOGGER_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  
 | 
						|
  static inline void LogError(const string &message, const string &function);
 | 
						|
  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;
 | 
						|
  static inline void LogFatalError(const string &message, const string &function);
 | 
						|
  static inline void LogCriticalError(const string &message, const string &function);
 | 
						|
 | 
						|
 | 
						|
public:
 | 
						|
                     CLoggerBase();
 | 
						|
 | 
						|
  //--- Methods to check if a flag is active 
 | 
						|
  inline bool        IsWarningLogEnabled() const { return m_warning_enable; }
 | 
						|
  inline bool        IsInfoLogEnabled() const { return m_info_enable; }
 | 
						|
  inline bool        IsCautionLogEnabled() const { return m_caution_enable; }
 | 
						|
 | 
						|
  //--- Getters
 | 
						|
  inline 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 inline void CLoggerBase::LogError(const string &message, const string& function)
 | 
						|
 {
 | 
						|
  FastLog(function, ERROR_TEXT, message);
 | 
						|
 }
 | 
						|
//+------------------------------------------------------------------+
 | 
						|
static inline void CLoggerBase::LogCriticalError(const string &message, const string& function)
 | 
						|
 {
 | 
						|
  FastLog(function, CRITICAL_ERROR_TEXT, message);
 | 
						|
 }
 | 
						|
//+------------------------------------------------------------------+
 | 
						|
static inline void CLoggerBase::LogFatalError(const string &message, const string& function)
 | 
						|
 {
 | 
						|
  FastLog(function, FATAL_ERROR_TEXT, message);
 | 
						|
 }
 | 
						|
//+------------------------------------------------------------------+
 | 
						|
inline void CLoggerBase::LogWarning(const string &message, const string& function) const
 | 
						|
 {
 | 
						|
  if(m_warning_enable)
 | 
						|
    FastLog(function, WARNING_TEXT, message);
 | 
						|
 }
 | 
						|
//+------------------------------------------------------------------+
 | 
						|
inline void CLoggerBase::LogInfo(const string &message, const string& function) const
 | 
						|
 {
 | 
						|
  if(m_info_enable)
 | 
						|
    FastLog(function, INFO_TEXT, message);
 | 
						|
 }
 | 
						|
//+------------------------------------------------------------------+
 | 
						|
inline 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 
 | 
						|
//+------------------------------------------------------------------+
 |