Reading from the Log File in Real Time

 

I've written an EA (with help of existing code I have found on the forum and the net) that reads in information from the experts log file.

However, I need the information in real time, and the log file is only updated in intervals throughout the day I think, when the terminal is closed, or when you right click on the log in the terminal and click open.

I've found this piece of code someone has written - https://www.mql5.com/en/forum/131720 - to perform a similar task where it accesses the terminal journal - repeatedly opening and closing the file to update it.

Does anyone know how I can modify this to perform the same task with the experts log file? (rather than the journal)

This is my first time using these dll functions. I'm guessing it's in the  "PostMessageA(hwnd,WM_COMMAND ,33101,0);" part, but I don't understand how it can know where the journal is from this command.

Any help would be massively appreciated! Thanks in advance!

 

( *edit - updated the code below to use PostMessageW and FindWindowW instead of PostMessageA and FindWindowA... though I'm not sure what the difference is between these two and what they actually do!... )

#include <WinUser32.mqh>

#import "user32.dll"
  int GetAncestor(int hWnd, int gaFlags);
#import
//+------------------------------------------------------------------+
//| script program start function                                    |
//+------------------------------------------------------------------+
void start()
{
  int hwnd = GetAncestor(WindowHandle(Symbol(),Period()),2); // find the Terminal handle no.
  PostMessageW(hwnd,WM_COMMAND ,33101,0); // open the folder contains the log files
  //-----
  while (FindIfDirOpened() == 0) // check if the folder opened
      {
      Sleep(100);
      FindIfDirOpened();
      }
   PostMessageW(FindIfDirOpened(), WM_CLOSE, 0, 0); //close the folder
}

int FindIfDirOpened()
   {
   string log = "logs";
   string i;
   int handle = FindWindowW(i ,log);
   return(handle);
   }
reading the Journal in real time - MQL4 forum
  • www.mql5.com
reading the Journal in real time - MQL4 forum
 
James Parker:

I've written an EA (with help of existing code I have found on the forum and the net) that reads in information from the experts log file.

However, I need the information in real time, and the log file is only updated in intervals throughout the day I think, when the terminal is closed, or when you right click on the log in the terminal and click open.

I've found this piece of code someone has written - https://www.mql5.com/en/forum/131720 - to perform a similar task where it accesses the terminal journal - repeatedly opening and closing the file to update it.

Does anyone know how I can modify this to perform the same task with the experts log file? (rather than the journal)

This is my first time using these dll functions. I'm guessing it's in the  "PostMessageA(hwnd,WM_COMMAND ,33101,0);" part, but I don't understand how it can know where the journal is from this command.

Any help would be massively appreciated! Thanks in advance!

 

Try to replace PostMessageA(..) by PostMessageW(..)
 
calli:
Try to replace PostMessageA(..) by PostMessageW(..)

Just tried this and it's still opening and closing the journal, rather than the experts log. I need to it open and close the experts log file instead.

Forgot to mention I had to amend the code above to change the FindWindowA to FindWindowW in order to get it to work, the FindWindowA was giving an error.

Does anyone know how to tell it to look for the experts log instead? 

 
You can use Spy++ or similar utility to monitor system messages between windows and catch command number you need.
Introducing Spy++
Introducing Spy++
  • msdn.microsoft.com
There are two versions of Spy++. The first version, named Spy++ (spyxx.exe), is designed to display messages sent to a window that is running in a 32-bit process. For example, Visual Studio runs in a 32-bit process. Therefore, you can use Spy++ to display messages sent to Solution Explorer. Because the default configuration for most builds in...
 
Stanislav Korotky:
You can use Spy++ or similar utility to monitor system messages between windows and catch command number you need.

Hi Stanislav,

Thank you for responding. Unfortunately I don't have any experience with Visual Studio or the programming that would be required for Spy++. I'm having a look into it now though to see if it something that I could pick up easily.

Please could you elaborate on how this would work? What is the catch command number, is that the "33101"? If so, I'll still need to find the command number for the experts log.

Thanks. 

 
James Parker:

Hi Stanislav,

Thank you for responding. Unfortunately I don't have any experience with Visual Studio or the programming that would be required for Spy++. I'm having a look into it now though to see if it something that I could pick up easily.

Please could you elaborate on how this would work? What is the catch command number, is that the "33101"? If so, I'll still need to find the command number for the experts log.

Thanks. 

Well, you might optionally use trial&error metod and scan surrounding numbers from the last known 33063 - (History to Last Month) to the next known 33134 - (D1 timeframe), to test which one opens the log folder.
 

I know this is an old thread, but there was an unanswered question in it that I know the answer to.  FindWindowA vs FindWindowW: Two versions of the same function.  One uses ANSI strings and the other uses Unicode strings.  The "W" stands for "Wide".  The version you have to use depends on the coding environment making the call.  I believe MT4 used to use ANSI strings but now uses Unicode strings.  The documentation for these Windows functions can be found on MSDN at https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-findwindowa

I am also needing to read the journal live from an EA.  Trying to get around the random bug where signals don't reliably copy due to the symbol names randomly failing to be translated.  All the signal's trades are logged directly to the journal, along with the copy failure errors.  My investigative work reveals that the journal contents are written to a Windows ListView control buried three child windows down (MT4\Terminal\Terminal\SysListView32).  This means it should be readable live using the Windows ListView control messages.  But coding MT4 for this could get a little complicated (if it's capable).

FindWindowA function
FindWindowA function
  • 2018.10.05
  • windows-sdk-content
  • docs.microsoft.com
Retrieves a handle to the top-level window whose class name and window name match the specified strings. This function does not search child windows. This function does not perform a case-sensitive search.
 

To open experts log file you must change from 33101 to 35435 in line

PostMessageW(hwnd,WM_COMMAND ,33101,0); // open the folder contains the log files
 

I have met the same problem too. So i come to give my thanks to all the authors above!

Besides, i have made a little adjustment to the EA log refreshing codes. complete version is below:

void Log_Flush(int flag)   //flag 33101=sys_log   35435=mql_log
{
    int hwnd = GetAncestor(WindowHandle(Symbol(),Period()),2);  // find the Terminal handle no.
    PostMessageW(hwnd,WM_COMMAND,flag,0);                     // open the folder contains the log files
    Sleep(100);
    //-----
    bool check = FindIfDirOpened();         //check if the folder opened.
    while(check==false) check = FindIfDirOpened();                            //keep checking until the open folder found 
    while(FindIfDirOpened()) PostMessageW(FindIfDirOpened(), WM_CLOSE, 0, 0); //keep closing until the open folder closed

}

int FindIfDirOpened()
{
    string log = "logs";
    string i;
    int handle = FindWindowW(i ,log);
    return(handle);
}

this method may not be the best, though, it is practicable.

 
Hao Tan:

I have met the same problem too. So i come to give my thanks to all the authors above!

Besides, i have made a little adjustment to the EA log refreshing codes. complete version is below:

this method may not be the best, though, it is practicable.

Is it possible to place order from reading the log file?
 
kajironpu:
Is it possible to place order from reading the log file?

Of course.

But why.

Reason: