Little help with macro definitions for debug messages

Martin Bittencourt  

Hi,

I have many Print debug messages in some of my codes I'd like to have them removed when compiling a "relase version". In order to avoid comment many lines, the normal strategy I have in mind is to use preprocessor directives as follows:

#define  DEBUG_MSG

#ifdef DEBUG_MSG
Print("Test");
#endif

The problem with the above line of code is that it has 3 lines, making the code a little "dirty", and I'd like to keep it clean by diminishing it to only one line. Using that with and if clause would be simple:

//above code
#define DEBUG_MSG(_M) if (false) _M //true or false depending on me wanting to debug or not

//in code
DEBUG_MSG(Print("Test");)

The problem with that above solution, AFAIK, is that the "if (false)" before every debug message will consume some processing time and I'd like to avoid that unnecessary CPU consumption. A solution like the first one, where the entire print debug code is removed from the compiled code, is preferable. But how to do it? I thought about putting the entire #ifdef inside a define like as follows:

//above code
#define USE_DEBUG_MSG
#define DEBUG_MSG(_M) #ifdef USE_DEBUG_MSG _M #endif

//in code
DEBUG_MSG(Print("Test");)

Now when I compile the code with only the define parts, it compiles fine. If I go to the code and, with a single line, write "

#ifdef USE_DEBUG_MSG Print("Test"); #endif

, it runs fine. But the moment I try to call the debug message, it gives me errors:

macro argument expected instead of 'ifdef'      MW MyIndicator.mq5  39      21
macro argument expected instead of 'endif'      MW MyIndicator.mq5  39      60
'MW SomeLibrary.mq5' SomeLibrary.mq5   1       1
'ifdef' - unexpected token      MW MyIndicator.mq5  574     4
'endif' - unexpected token      MW MyIndicator.mq5  574     4

So, is there a way to implement what I want? Or if my desire is to keep the debug messages in just one line, I'l have to use the if(true) / if(false) way?

Lorentzos Roussos  
Martin Bittencourt:

Hi,

I have many Print debug messages in some of my codes I'd like to have them removed when compiling a "relase version". In order to avoid comment many lines, the normal strategy I have in mind is to use preprocessor directives as follows:

The problem with the above line of code is that it has 3 lines, making the code a little "dirty", and I'd like to keep it clean by diminishing it to only one line. Using that with and if clause would be simple:

The problem with that above solution, AFAIK, is that the "if (false)" before every debug message will consume some processing time and I'd like to avoid that unnecessary CPU consumption. A solution like the first one, where the entire print debug code is removed from the compiled code, is preferable. But how to do it? I thought about putting the entire #ifdef inside a define like as follows:

Now when I compile the code with only the define parts, it compiles fine. If I go to the code and, with a single line, write "

, it runs fine. But the moment I try to call the debug message, it gives me errors:

So, is there a way to implement what I want? Or if my desire is to keep the debug messages in just one line, I'l have to use the if(true) / if(false) way?

Hello , you can do : 

#define DEBUG_MSG true
void DebugPrint(string message){
if(DEBUG_MSG){
Print(message);
}}

then replace all Print( with DebugPrint( 

That is if you don't mind accessing the DebugPrint function

If you do mind use this : 

#define USE_DEBUG_MSG
#ifdef USE_DEBUG_MSG
#define DEBUG_MSG(_M) _M 
#else 
#define DEBUG_MSG(_M)
#endif 

//in code

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- create timer
   EventSetTimer(60);
   Print("Bread Above");
   DEBUG_MSG(Print("Hamburger");)
   Print("Bread Below");
//---
   return(INIT_SUCCEEDED);
  }
Martin Bittencourt  
Lorentzos Roussos #:

Hello , you can do : 

then replace all Print( with DebugPrint( 

That is if you don't mind accessing the DebugPrint function

If you do mind use this : 

Hmm I liked your second alternative! Thanks! :)

Lorentzos Roussos  
Martin Bittencourt #:

Hmm I liked your second alternative! Thanks! :)

yeah my job is inspiring sometimes.

you are welcome :)

Reason: