CDebugLogger Class: A Comprehensive Logging Utility for MQL4/5
The CDebugLogger class is a powerful and flexible logging utility specifically designed for MQL4/5 environments. It is an essential tool for developers who need to monitor, debug, and track the behavior of their applications with precision. Below, we explore the key features and capabilities of this class.
Key Features
- Multiple Log Levels: The CDebugLogger class supports logging at different levels of importance, including INFO, WARNING, ERROR, and DEBUG. This allows developers to filter and focus on messages of particular significance.
- Timestamp Inclusion: Developers can choose to include timestamps in their log messages, with customizable formats. This feature is crucial for tracking the exact time of events and debugging time-sensitive issues.
- File Logging: The class provides robust support for logging to files. Developers can enable or disable file logging, specify the log file’s path, and choose whether to save logs in a common folder. Additionally, logs can be saved in a CSV format, making them easy to parse and analyze.
- Contextual Information: To enhance the clarity of log messages, the CDebugLogger class allows the inclusion of function signatures, file names, and line numbers. This contextual information helps in pinpointing the exact location of issues within the code.
- Silent Keywords: A unique feature of this class is the ability to silence logs that contain specific keywords. This is particularly useful for preventing sensitive information, such as passwords or confidential data, from being logged.
Example Usage
Below is an example of how to initialize and use the CDebugLogger class:
// Initialize the logger with INFO level logging to a file CDebugLogger logger(INFO, true, "log.txt", true, TIME_DATE | TIME_MINUTES, false, true, true, true); // Log a simple message logger.Log(INFO, "This is an info message"); // Silence a keyword logger.AddSilentKeyword("password"); // Log a message that will be silenced logger.Log(INFO, "User entered password: 1234"); // Enable file logging logger.EnableFileLogging(true, "debug.log", false); // Remove a silenced keyword logger.RemoveSilentKeyword("password"); // Log a message after removing the keyword from the silence list logger.Log(INFO, "User entered password: 1234");
Script Example
To utilize the CDebugLogger class in a script, simply include the necessary library at the beginning of your file, as shown below:
//--- It is important to include this header file before all others #include <Logging.mqh>  //+------------------------------------------------------------------+ //| Script program start function                                    | //+------------------------------------------------------------------+ void OnStart()   TIME_MINUTES, false, true, true, true, true);   //--- Log a simple informational message   Log(INFO, "Script started successfully.");   //--- Log a warning message   Log(WARNING, "This is a warning message.");   //--- Log an error message   Log(ERROR, "This is an error message.");   //--- Log a debug message   Log(DEBUG, "This is a debug message for debugging purposes.");   //--- Add a keyword to silence logs containing 'password'   logging.AddSilentKeyword("password");   //--- Attempt to log a message containing the silenced keyword   Log(INFO, "User entered password: 12348"); //--- This message will be silenced   //--- Remove the silenced keyword   logging.RemoveSilentKeyword("password");   //--- Log the message again, now it will be logged   Log(INFO, "User entered password: 1234");   //--- Use the generic Log function to log a message   Log(INFO, "This message is logged using the generic Log function.");   //--- Use the Print macro to log a message at the INFO level   Print("This message is logged using the Print macro.");  Â
Output CSV example:
Timestamp,Level,Message "2024.08.31 14:26","INFO","Script started successfully." "2024.08.31 14:26","WARNING","This is a warning message." "2024.08.31 14:26","ERROR","This is an error message." "2024.08.31 14:26","DEBUG","This is a debug message for debugging purposes." "2024.08.31 14:26","INFO","User entered password: 1234" "2024.08.31 14:26","INFO","This message is logged using the generic Log function." "2024.08.31 14:26","INFO","This message is logged using the Print macro."
Conclusion
The CDebugLogger class is an invaluable tool for any MQL4/5 developer. With its wide range of customizable features, it enables precise logging and monitoring of applications, facilitating easier debugging and better application performance tracking. Whether you need simple message logging or detailed contextual information, the CDebugLogger class provides a reliable and efficient solution tailored to your development needs.
For more information about the CDebugLogger class or to explore other advanced tools and solutions, visit StormWave Technologies.