Using #define to include Serial.printf() or not

I'm using Ralph Bacons YouTube video that explains how to incorporate Serial.printf() or not.
This works very well and I've increased the number of #define required in order to select the correct Serial.printf().

The code is as follows:

#define DEBUG1  0       // if 0 then DEBUG(n) does not display, if 1 then it does
#define DEBUG2  0
#define DEBUG3  0
#define DEBUG4  0 

#if DEBUG1 == 1
#define debug1(a)    Serial.printf(a)
#else
#define debug1(a) 
#endif

#if DEBUG2 == 1
#define debug2(a,b)    Serial.printf(a,b)
#else
#define debug2(a,b) 
#endif

#if DEBUG3 == 1
#define debug3(a,b,c)  Serial.printf(a,b,c)
#else 
#define debug3(a,b,c) 
#endif

#if DEBUG4 == 1
#define debug4(a,b,c,d)  Serial.printf(a,b,c,d)
#else 
#define debug4(a,b,c,d) 
#endif

so in the code you use debug1(), debug2(), debug3() and debug(4).
Whilst this works fine I was thinking if there was a way to determine which debug(n) was required so you could just use debug() and the # code would be simpler.

You could use a macro with variadic arguments that replaces all your variants

#define FOO(...) printf(__VA_ARGS__)
#define DEBUG 1  // or 2 or 4  etc.

#ifdef DEBUG == 1
// do debug 1 stuff
#endif

#ifdef DEBUG == 2
// do debug 2 stuff
#endif

-or-
you can use more complex logic to test
for debug modes e.g.

#ifdef (DEBUG && 3) == 3
// do both 1 and stuff
#endif

There are a lot of ways to do this
using binary or other logical constructs.

Thank you for your prompt reply. To test your suggestion I've written this test code:

#define DEBUG  1       // if 0 then debug does not display

#if DEBUG == 1
#define debug(...)    Serial.printf(__VA_ARGS__)
#else
#define debug(...) 
#endif

String test = "hello";

void setup() {
  Serial.begin(115200);
  while (!Serial);
     ;
  debug("this is a one test \n");
  debug("this is a two test %s \n", test );
  debug("three things  %d %d  \n", 1, 2);
  debug("four things  %d %d  \n", 3, 4, 5);
}

That works except with 4 variables it only prints 3. So I increased the number of dots from
2 to 4

#define debug(....)    Serial.printf(__VA_ARGS__)

but now I get a compile error

Compilation error: expected ')' after "..."

Would appreciate your comment please.

Do you mean that it not print "5" in the line below?

Do you know the syntax of the printf operator? It prints as many variables as the number of "%xx" placeholders you specified.
So you need to add another %d in your line that way:

debug("four things %d %d %d\n", 3, 4, 5);

Sorry, my error. Fixed it

debug("four things %d %d %d \n", 3, 4, 5); 

So just use 3 dots and now works just fine.

Many thanks!

Thanks! I'm summarizing the good solution:

#define DEBUG_SWITCH true    // Switch on or off the debugging logs
#if DEBUG_SWITCH == true
    #define DEBUG_printf(...) Serial.printf(__VA_ARGS__)
#else
    #define DEBUG_printf(...)
#endif
// #include <LibPrintf.h>   // If your Arduino IDE does not know "printf", seek library "arduino-printf" at GitHub

while (!Serial) { }    // Wait until the serial bus to your Laptop is ready for transmission. Some types of microprocessor require this delay.
DEBUG_printf(F("\n[SETUP] This is the log of sketch '" __FILE__ "', compilled on " __DATE__ " " __TIME__ "\n"));

DEBUG_printf("[LOOP] I send integer numbers (e.g. %d), float numbers (%3.2f), hex numbers (0x%X) and strings (%s) to the serial port.\n", 1, 2.2, 3*3*3, "Four");

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.