I'm currently finishing a library and wanted that my "Debug" logs functions were optional, at compiling time.
What I thought was: check if DEBUG
is defined, and then, define my custom debug
function.
Here is what I have done (a part)
#if defined(DEBUG)
#define debug(s) Serial.println(s)
#define debug(s,t) Serial.print(s);Serial.println(t)
#else
#define debug(s) // s
#define debug(s,t) // s t
#endif
(I'm compiling for Arduino; that's why I need to separate the function in two.)
As I use lots of time, Serial.print
succeded by a Serial.println
, I wanted that debug(s)
, also accepted two "parameters".
So, inserting debug("ParamOne");
and debug("ParamOne", "ParamTwo");
would result in the defined function.
But, apparently, only the last defined debug is valid, overriding the first one.
What should I do, to keep the SAME name of the funcion, or is there any way more "correct" to do it?
#define macro names are unique, these are not function definitions, so your second #define is overwriting the first. you may want to do something like
#if defined(DEBUG)
inline void debug(const char *s) { Serial.println(s); }
inline void debug(const char *s, char *t) { Serial.print(s);Serial.println(t); }
#else
inline void debug(const char *s) { }
inline void debug(const char *s, const char *t) { }
#endif