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.
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"); // Add a keyword to filter logs logger.AddFilterKeyword("success"); // Log a message that will be filtered out logger.Log(INFO, "Operation failed"); // Log a message that will pass the filter logger.Log(INFO, "Operation successful"); // Remove a keyword from the filter logger.RemoveFilterKeyword("success"); // Initialize using the generic Log function logging.Initialize(WARNING, true, "warnings.log", true, TIME_SECONDS, true, false, true, true); // Log a warning using the generic Log function Log(WARNING, "This is a warning message");
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() { //--- Initialize the logger with INFO level, logging to a file //--- Including timestamps and saving in CSV format int log_options = 0; // FILENAME | LINE | FUNCSIG; logging.Initialize(INFO, true, "example_log.txt", true, TIME_DATE | TIME_MINUTES | TIME_SECONDS, false, log_options, 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."); //--- Demonstrate logging with different combinations of options logging.Initialize(INFO, true, "log_with_options.txt", true, TIME_DATE | TIME_MINUTES, false, FILENAME | LINE, true); Log(INFO, "This log includes only the file name and line number."); logging.Initialize(INFO, true, "log_with_funcsig.txt", true, TIME_DATE | TIME_MINUTES | TIME_SECONDS, false, FUNCSIG, true); Log(INFO, "This log includes only the function signature."); logging.Initialize(INFO, true, "log_custom_order.txt", true, TIME_MINUTES, false, LINE | FILENAME | FUNCSIG, true); Log(INFO, "This log includes line number, file name, and function signature in a custom order."); //--- Add a keyword to filter logs containing 'important' logging.AddFilterKeyword("important"); //--- Log some messages to demonstrate the filter Log(INFO, "This is an important message."); // This message will be visible Log(INFO, "This is a regular message."); // This message will not be visible //--- Remove the filter keyword to show all logs logging.RemoveFilterKeyword("important"); //--- Log a final message indicating the end of the script Log(INFO, "Script execution completed."); }
Timestamp,Level,Message "2024.09.01 18:31:44","INFO","Script started successfully." "2024.09.01 18:31:44","WARNING","This is a warning message." "2024.09.01 18:31:44","ERROR","This is an error message." "2024.09.01 18:31:44","DEBUG","This is a debug message for debugging purposes." "2024.09.01 18:31:44","INFO","User entered password: 1234" "2024.09.01 18:31:44","INFO","This message is logged using the generic Log function." "2024.09.01 18:31:44","INFO","This message is logged using the Print macro."
Timestamp,Level,Message,Filename,Line "2024.09.01 18:31","INFO","This log includes only the file name and line number.","Logging.mq5","135"
Timestamp,Level,Message,Funcsig "2024.09.01 18:31:44","INFO","This log includes only the function signature.","void OnStart()"
Timestamp,Level,Message,Filename,Line,Funcsig "18:31","INFO","This log includes line number, file name, and function signature in a custom order.","Logging.mq5","141","void OnStart()" "18:31","INFO","This is an important message.","Logging.mq5","147","void OnStart()" "18:31","INFO","Script execution completed.","Logging.mq5","154","void OnStart()"
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.
HiLo
High and Low Line Indicator
Save OHLCV Data from Chart to CSV File
This script saves all the OHLCV data available on the chart to a CSV file.
Max trade volume checker for your trading account
A dialog to display the maximum lot size permitted on the underlying asset for different types of orders (buy, sell, pending buy, and pending sell).
BuySell + SL + TP
script to open a buy position at the current price in the MT5 window with a specified stop loss (in pips) and take profit (in pips)