This is a logging library to quickly add logging capabilities to your code withoutΒ much hassle. The library’s default behavior matches most cases however most of the functionality can be customized using pre-processor substitutions (#define)
See the example scripts on how to use library.
How to use
#include <Logger.mqh> //+------------------------------------------------------------------+ //| Script program start functionΒ Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β | //+------------------------------------------------------------------+ void OnStart() Β Β { Β Β DEBUG("This id debug"); Β Β INFO("This is info"); Β Β ERROR("This is an error"); Β Β } //+------------------------------------------------------------------+
Customization notes
- The logger uses following format to print logs
// [time, except LOGGER_PRINT,LOGGER_ALERT] [level] [prefix] [message] [last error, only Logger::Error]
sample output: 2021.06.08 17:35:34 | Error | OnStart | This is a test error | invalid function parameter value
- Use following logging functions/digits to assign them to logging levels. define as 0 if the level needs to be disabled
#define LOGGER_PRINTΒ Β Β Β 1Β Β Β Β // print to log #define LOGGER_ALERTΒ Β Β Β 2Β Β Β Β // trigger alert #define LOGGER_FILEΒ Β Β Β 4Β Β Β Β // write to a file #define LOGGER_NOTIFYΒ Β 8Β Β Β Β // send a notification
example:
#define LOGGER_SET_DEBUG 0Β Β Β Β Β Β // DEBUG is disabled #define LOGGER_SET_INFOΒ Β 1|2Β Β Β Β // Print and alert #define LOGGER_SET_ERROR 1|2|4Β Β // Print, alert and file write
- Customize the prefix section as follows
#define LOGGER_PREFIX __FILE__ + " | Line:" + IntegerToString(__LINE__) // [filename] | [Line number]an example prefix output will be : with_customization.mq4 | Line:36
- Customize file name by a string constant or a function call
#define LOGGER_FILENAME "backtest_debug.log" // or #define LOGGER_FILENAME IntegerToString(AccountNumber())
Notes:
- #define before the Logger.mqh is included.
- To disable a logger level set the level as zero.
- LOGGER_NOTIFY and LOGGER_FILE execution errors are handled as a print statements. Do not customize it to use library’s own error handling, the program might trigger an infinite loop or a stack overflow.