How can I create a fuction with a macro for printing?

 

Hi everyone,


I need to create a macro for printing en MQL5, but I don't know how to do it. This is what I was trying but it didn't work:


#define PRINT(debug,...) ((debug == 1)?Print(...))


So you can use it in that way:


PRINT(1, "Hello World!"); //Print

PRINT(0, "Hello World!"); //Don't print

 
Oscar Gomez Fuente:

Hi everyone,


I need to create a macro for printing en MQL5, but I don't know how to do it. This is what I was trying but it didn't work:


#define PRINT(debug,...) ((debug == 1)?Print(...))


So you can use it in that way:


PRINT(1, "Hello World!"); //Print

PRINT(0, "Hello World!"); //Don't print

Make a bool for the value und do it into if(....

 
amando:

Make a bool for the value und do it into if(....

Hi amando,


Something like that, but it doesn't work:


#define PRINT(debug,...) if (debug == true) Print(...);


The problem is that part: " ,...)" I don't know how to do it.

 

Do you mean like this?

#define PRINT(debug,msg) if(debug==1) Print(msg)

PRINT(1,"Hello World!");
 
lippmaje:

Do you mean like this?

Thanks is advanced lippmaje.

YES and NO.

I was programming the macro for the print function, but I was thinking about PrintFormat:

https://www.mql5.com/en/docs/common/printformat

#define PRINT(debug, msg) if (debug == 1) Print(msg); ---->   It works perfectly.

#define PRINTF(debug, msg, ...) if (debug == 1) PrintFormat(msg, ...); ----> It doesn't compile.

Documentation on MQL5: Common Functions / PrintFormat
Documentation on MQL5: Common Functions / PrintFormat
  • www.mql5.com
PrintFormat - Common Functions - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Oscar Gomez Fuente:

Thanks is advanced lippmaje.

YES and NO.

I was programming the macro for the print function, but I was thinking about PrintFormat:

https://www.mql5.com/en/docs/common/printformat

#define PRINT(debug, msg) if (debug == 1) Print(msg); ---->   It works perfectly.

#define PRINTF(debug, msg, ...) if (debug == 1) PrintFormat(msg, ...); ----> It doesn't compile.

Just read the documention of PrintFormat().

Documentation on MQL5: Common Functions / PrintFormat
Documentation on MQL5: Common Functions / PrintFormat
  • www.mql5.com
PrintFormat - Common Functions - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Oscar Gomez Fuente:

#define PRINT(debug, msg) if (debug == 1) Print(msg); ---->   It works perfectly.

#define PRINTF(debug, msg, ...) if (debug == 1) PrintFormat(msg, ...); ----> It doesn't compile.

That's a limitation of the macro preprocessor. You cannot define a variable amount of macro parameters, same as in C and C++.

What's your exact use case?

I've recently published a library to address debug logging, Log4Mql (MT4: https://www.mql5.com/en/code/31425 MT5: https://www.mql5.com/en/code/31452), check it out.

Log4mql (MT4)
Log4mql (MT4)
  • www.mql5.com
A logging library similar to Log4j but for MQL.
 
Carl Schreiber:

Just read the documention of PrintFormat().

How is this related to OP's topic?
 

This can be made in c:

#define PRINTF(debug, msg, ...) if (debug) printf(msg, ##__VA_ARGS__)

The question is:

Is there any method to do the same in MQL5?


Best regards.

 

As said earlier, MQL doesn't allow for variadic macros.

You could do this:

#define PRINT(debug,msg) if(debug)Print(msg)

PRINT(1,StringFormat("bid:%f ask:%f",bid,ask));

or

#define PRINT(debug,msg) if(debug)printf msg

PRINT(1,("bid:%f ask:%f",bid,ask));

or

#define PRINT if(g_debug)printf

int g_debug=1; // global

PRINT("bid:%f ask:%f",bid,ask);

But any of this is just a workaround, more or less.

 
lippmaje:

As said earlier, MQL doesn't allow for variadic macros.

You could do this:

or

or

But any of this is just a workaround, more or less.


Ok, thank you very much.