Log critical errors

 

My EAs log diagnostic data in a text file. This helps me troubleshoot users problems, monitor the trade execution (slippage, server roundtrip delay), etc. Unfortunately, my EAs are not perfect and might cause a critical error (e.g. "array out of range"). It would be great if the EA would allow me to also log this before terminating. For less technical users, or users running several EAs in parallel, it is hard to find the error message in the Terminal Journal so this requires a lot of support from my end to guide them.

Can I somehow intercept a critical error before the EA terminates? I noticed the OnDeinit() function is not called, neither are destructors. The example code below does not output anything:

class ErrorHandler {
public:
   ~ErrorHandler() {
      Print("ErrorHandler destructor will NOT be called");
   }
};

ErrorHandler errorHandler;

int OnInit() {
   return INIT_SUCCEEDED;
}

void OnDeinit(const int reason) {
   Print("OnDeinit will NOT be called");
}

void OnTick() {
   double a[2];
   double b = a[3]; // critical error
}

Is there a callback function, or hook I can use to intercept the fatal error?

 
yoriz:

My EAs log diagnostic data in a text file. This helps me troubleshoot users problems, monitor the trade execution (slippage, server roundtrip delay), etc. Unfortunately, my EAs are not perfect and might cause a critical error (e.g. "array out of range"). It would be great if the EA would allow me to also log this before terminating. For less technical users, or users running several EAs in parallel, it is hard to find the error message in the Terminal Journal so this requires a lot of support from my end to guide them.

Can I somehow intercept a critical error before the EA terminates? I noticed the OnDeinit() function is not called, neither are destructors. The example code below does not output anything:

Is there a callback function, or hook I can use to intercept the fatal error?

Interesting idea but the newbie mistakes that throw out of range are easy to find with debugger. The more tricky ones happen at the intersection of two different blocks of logic (only while running).
So you would need like a crazy interpreter.

It would be nice if the built in log told you the line in which out of range happened right away though. 

Comes to my mind is it possible to have an event handler say OnDebuggerStop() then a  GetLastLine() function would be nice?

I wonder if MetaQuotes give us this because the compiler knows the line to each statement, that would be neat...
 
pennyhunter #:
Interesting idea but the newbie mistakes that throw out of range are easy to find with debugger. The more tricky ones happen at the intersection of two different blocks of logic (only while running).
So you would need like a crazy interpreter.

It would be nice if the built in log told you the line in which out of range happened right away though. 

Comes to my mind is it possible to have an event handler say OnDebuggerStop() then a  GetLastLine() function would be nice?

I wonder if MetaQuotes give us this because the compiler knows the line to each statement, that would be neat...
Hopefully metaquotes will add support for exceptions in the future since critical errors are just that.
 
You can put the actual states of your program in the Comment() it shows the last situation right before the crash - and it doesn't flood the logs.
 

Can you show us a code that gives you an array out of range error ?


I would suggest to just make sure not to have that error in the first place. You can create for example class functions that handle array insertion and resizing like in the CArray... class. It has been a long time since I got that annoying error.

 

Thank you for your feedback, but I feel you misunderstood my question. Of course I must properly debug my program before releasing it, or can write to Comment(), make a CArray wrapper class, etc.

My point was, that critical errors (that is, error that halt my EA) result in a message in the Journal tab, but do not allow my EA do something before crashing. The destructors are not called, the OnDeinit() is not called, etc.

The only thing I can do is guide my user through thousands of lines in the Journal log to help him find the diagnostic message I need.

I was hoping for something like @pennyhunter suggested: a OnDebuggerStop() callback, or a POSIX signal handler, etc. Or a means to read back the Journal when the EA is restarted (without enabling .dlls to use Windows File I/O, because my users won't do that).

 
Alexandre Borela #:
Hopefully metaquotes will add support for exceptions in the future since critical errors are just that.

Metaquotes CEO repeatedly answered no to that request. Of course it already happened he changed his mind but I seriously doubt it on this specific topic.

 
yoriz #:

Thank you for your feedback, but I feel you misunderstood my question. Of course I must properly debug my program before releasing it, or can write to Comment(), make a CArray wrapper class, etc.

My point was, that critical errors (that is, error that halt my EA) result in a message in the Journal tab, but do not allow my EA do something before crashing. The destructors are not called, the OnDeinit() is not called, etc.

The only thing I can do is guide my user through thousands of lines in the Journal log to help him find the diagnostic message I need.

I was hoping for something like @pennyhunter suggested: a OnDebuggerStop() callback, or a POSIX signal handler, etc. Or a means to read back the Journal when the EA is restarted (without enabling .dlls to use Windows File I/O, because my users won't do that).

The only way to avoid a critical error is to explicitly check for it anytime there is a doubt it may happen. Check your indexes !
Reason: