How to read log files in real time? MQL4

 
Hi guys. I'm trying to read latest line in latest log file. So could anybody help me?
//+------------------------------------------------------------------+
//|                                                        Test1.mq4 |
//|                              Copyright 2019, R.T. Software Corp. |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Copyright 2019, R.T. Software Corp."
#property link      ""
#property version   "1.00"
#property strict
#property indicator_chart_window

#include <WinUser32.mqh>
#import "kernel32.dll"
   int CreateFileW(string, uint, int, int, int, int, int);
   int GetFileSize(int, int);
   int ReadFile(int, uchar&[], int, int&[], int);
   int CloseHandle(int);
#import

//Number of bytes to read from first line of log file
int BytesToRead = 10;

//path where file is stored
string path = "";

string ReadFile(string Filename)
{
   string strFileContents = "";
   int Handle = CreateFileW(Filename, 0x80000000 /*GENERIC_READ*/, 3 /*SHARE READ|WRITE*/, 0, 3 /*OPEN_EXISTING*/, 0, 0);
   
   if (Handle == -1)
   {
     //error opening file
     Print("Error opening log file ", path, " error code: ", IntegerToString(GetLastError()));
     return ("");
   }
   else
   {
      int LogFileSize = GetFileSize(Handle, 0);
      if(LogFileSize <= 0)
      {
         //File empty
         Print("Log file is empty ", path); 
         return ("");
      }
      else
      {
         uchar buffer[];
         ArrayResize(buffer, BytesToRead);
         int read[1];
         ReadFile(Handle, buffer, BytesToRead, read, 0);
         if (read[0] == BytesToRead) 
         {
            strFileContents = CharArrayToString(buffer, 0, read[0]);
         }
         else
         {
            // Read failed
            Print("Error reading log file ",path); 
            return ("");
         }
      }
      CloseHandle(Handle);
   }
   return strFileContents;
}
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
{
//--- indicator buffers mapping
   
//---
   return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
//---
   ReadFile("some.log");
//--- return value of prev_calculated for next call
   return(rates_total);
  }
 
Revazi Tchitanava I'm trying to read latest line in latest log file. So could anybody help me?
If you had used this you would have found,
  1. cant read the log from logs because of sandbox security.
  2. that it can't be reliability done anywhy. The terminal caches the writes.
  3. How to read system log from MT4/mt5 logs folder ? - MT4 - MQL4 and MetaTrader 4 - MQL4 programming forum
 
William Roeder:
If you had used this you would have found,
  1. cant read the log from logs because of sandbox security.
  2. that it can't be reliability done anywhy. The terminal caches the writes.
  3. How to read system log from MT4/mt5 logs folder ? - MT4 - MQL4 and MetaTrader 4 - MQL4 programming forum

Hi William!
Thanks for you respond.
I have indicator witch gives me signals and give PUT and CALL signals only in log files. so my goal is to create indicator that will check stochastic arrow and also this PUT or CALL signals.
I think I made everything correct I just don't know how to read this massage from log. If you could please help me.

//+------------------------------------------------------------------+
//|                                                        Test1.mq4 |
//|                              Copyright 2019, R.T. Software Corp. |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Copyright 2019, R.T. Software Corp."
#property link      ""
#property version   "1.00"
#property strict
#property indicator_chart_window

#include <WinUser32.mqh>
#import "user32.dll"
  int GetAncestor(int, int);
#import
#define MT4_WMCMD_EXPERTS         33020 // Toggle Expert Advisor button
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
{
//--- indicator buffers mapping
   
//---
   return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
//---
   // IsExpertEnabled()
   // sell ê î
   // buy  é ì

   //StringFind(logfile, "CALL", 107);
   //StringFind(logfile, "PUT", 107);

   int main = GetAncestor(WindowHandle(Symbol(), Period()), 2/*GA_ROOT*/);

   if(ObjectGetString(ChartID(), "Binary Strategy"+Timeframe()+"tI0", OBJPROP_TEXT) == "SELL" &&
      IsExpertEnabled() == false &&
      (ObjectGetString(ChartID(), "Binary Strategy"+Timeframe()+"dI0", OBJPROP_TEXT) == "ê" ||
       ObjectGetString(ChartID(), "Binary Strategy"+Timeframe()+"dI0", OBJPROP_TEXT) == "î" )
     )
   {
      PostMessageA(main, WM_COMMAND, MT4_WMCMD_EXPERTS, 0); // Toggle Expert Advisor button
      Alert("SELL");
   }
   
   if(ObjectGetString(ChartID(), "Binary Strategy"+Timeframe()+"tI0", OBJPROP_TEXT) == "BUY" &&
      IsExpertEnabled() == false &&
      (ObjectGetString(ChartID(), "Binary Strategy"+Timeframe()+"dI0", OBJPROP_TEXT) == "é" ||
       ObjectGetString(ChartID(), "Binary Strategy"+Timeframe()+"dI0", OBJPROP_TEXT) == "ì" )
     )
   {
      PostMessageA(main, WM_COMMAND, MT4_WMCMD_EXPERTS, 0); // Toggle Expert Advisor button
      Alert("BUY");
   }
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
string Timeframe()
{
   if(Period() == PERIOD_M1)
   {
      return "M1";
   }
   else if(Period() == PERIOD_M5)
   {
      return "M5";
   }
   else if(Period() == PERIOD_M15)
   {
      return "M15";
   }
   else if(Period() == PERIOD_M30)
   {
      return "M30";
   }
   else if(Period() == PERIOD_H1)
   {
      return "H1";
   }
   else if(Period() == PERIOD_H4)
   {
      return "H4";
   }
   else if(Period() == PERIOD_D1)
   {
      return "D1";
   }
   else if(Period() == PERIOD_W1)
   {
      return "W1";
   }
   else
   {
      return "MN";
   }
   return "Time Frame not defined";
}
//+------------------------------------------------------------------+
 

Hi, i found a solution for your problem.It's tested and works very good.

#property copyright "Copyright 2020, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#include <WinUser32.mqh>
#import "kernel32.dll"
   int CreateFileW(string, uint, int, int, int, int, int);
   int GetFileSize(int, int);
   int ReadFile(int, uchar&[], int, int&[], int);
   int CloseHandle(int);
#import
//59491
int BytesToRead = 0;

 string CovertDateTime(){ //Get your current logs to read for example today is 28.02.2020 so logs will be 20200228
 datetime now = TimeCurrent(),
         yesterday = now - 86400; // PERIOD_D1 * 60
 string logFile2=TimeToStr(yesterday);
 string logFile1=TimeToStr(TimeCurrent(),TIME_DATE);
 StringReplace(logFile1,".","");

 return logFile1;
 }
string File = CovertDateTime();
string terminal_data_path=TerminalInfoString(TERMINAL_DATA_PATH); 
string filename= File +".log";
string path=terminal_data_path+"/logs/"+File+".log";//if dont want with this condition to find your logs please remove this line and put the next line your path;
//string path = "C:/Users/Gavyy/AppData/Roaming/MetaQuotes/Terminal/A270C22676FD87E1F4CE8044BDE1756D2/logs/20200225.log";



int OnInit(){

  string a = ReadFile(path);
   Alert(a);
   
return(INIT_SUCCEEDED);
}

void OnDeinit(const int reason){

   
}

void OnTick(){

   
}

string ReadFile(string Filename){
   string strFileContents = "";
   int Handle = CreateFileW(Filename, 0x80000000 /*GENERIC_READ*/, 3 /*SHARE READ|WRITE*/, 0, 3 /*OPEN_EXISTING*/, 0, 0);
   
   if (Handle == -1){
   
     //error opening file
     Print("Error opening log file ", path, " error code: ", IntegerToString(GetLastError()));
     return ("");
   }
   else{
      int LogFileSize = GetFileSize(Handle, 0);
      BytesToRead = LogFileSize;
      if(LogFileSize <= 0){
         //File empty
         Print("Log file is empty ", path); 
         return ("");
      }
      else{
         uchar buffer[];
         ArrayResize(buffer, BytesToRead);
         int read[1];
         ReadFile(Handle, buffer, BytesToRead, read, 0);
         if (read[0] == BytesToRead){
            strFileContents = CharArrayToString(buffer, 0, read[0]);
         }
         else{
            // Read failed
            Print("Error reading log file ",path); 
            return ("");
         }
      }
      CloseHandle(Handle);
   }
   return strFileContents;
}

 
Gavyy:

Hi, i found a solution for your problem.It's tested and works very good.


You are good and constructive.

I hope people learn from your attitude.

 
Gavyy: Hi, i found a solution for your problem.It's tested and works very good.

Except you can't read new entries because the terminal caches the disk writes, as I previously said in #1.2 three years ago.

Reason: