• 概述
  • 评论
  • 评论

MT4 Logger

Description

The CLogger class provides a simple but sophisticated logging utility that you can use to output messages. The messages have associated levels, such as INFO or ERROR that indicate their importance. You can then give the CLogger a level, and only messages at this level or higher will be saved.

The levels are:

  • DEBUG: Low-level information for developers.
  • INFO: Generic (useful) information about system operation.
  • WARNING: A warning.
  • ERROR: A handleable error condition.

Public methods

  1. LOGGER_START(minimumLevel, folderName=NULL)—Starts logger function.

    - minimumLevel: Messages at this level or higher will be saved.

    - folderName: Path of subfolder under <Data folder>\MQL4\Files\ folder.

  2. LOGGER_STOP()—Stops logger function.

  3. LOGGER_WRITE(level, message)—Writes a line into the log file.

    - level: Type of message.

    - message: The text of the message.

  4. LOGGER_DELETE()—Closes the logger.

You can find the saved log files in (in the Terminal menu, select to view "File" -> "Open Data Folder"):

<Data folder>\MQL4\Files\


Example

<Data folder>\MQL4\Include\CLogger.mqh

enum LoggerLevel
{
    DEBUG,
    INFO,
    WARNING,
    ERROR
};

#import "CLogger.ex4"
   void LOGGER_START(LoggerLevel, string);
   void LOGGER_STOP(void);
   void LOGGER_WRITE(LoggerLevel, string);
   void LOGGER_DELETE(void);
#import

<Data folder>\MQL4\Experts\LoggerTest.mq4

#include <CLogger.mqh>
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int OnInit()
  {
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   LOGGER_DELETE();
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnTick()
  {
   logStart();

   static int i=1;
   if(i<=10 || (i>=20 && i<=30)) Log(INFO,StringConcatenate(i,". ",GetTickCount()));
   i++;

   logStop();
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void logStart()
  {
   LOGGER_START(DEBUG,StringConcatenate(Symbol(),"\\"));
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void logStop()
  {
   LOGGER_STOP();
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void Log(LoggerLevel _type,string _text)
  {
   LOGGER_WRITE(_type,_text);

  }

See result on Screenshot.

For correct function call, the library should be used on build 670 and higher.
无评论