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(....
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!");
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.
- www.mql5.com
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().
- www.mql5.com
#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.
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.
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.
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
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