You might have
#ifdef NDEBUG
#define LOGD(...) do {} while(0)
#else
#define LOGD(...) do {rtt_printf(TERMINAL_DEBUG, ##__VA_ARGS__); \
} while(0)
#endif
following the convention on NDEBUG
used by assert(3)
If you want to use the arguments to make your compiler happy and have it check the arguments, you could try (in the NDEBUG
case)
#define LOGD(...) do { if (false) printf(__VA_ARGS__); }while(0)
So the optimizing compiler won't emit any call to printf
(if using GCC, be sure to pass -Wall -O2
at least to g++
)
BTW, I would suggest instead (for the debugging case without NDEBUG
):
#define LOGD(Fmt,...) do { rtt_printf(TERMINAL_DEBUG, "%s:%d " Fmt "\n", \
__FILE__, __LINE__, ##_VA_ARGS); } while(0)
then use LOGD("x=%d", x)
in your code. This would show the source line position.
At last, in genuine C++11, you'll better use output streams from <ostream>
and code (for the debugging case):
#define LOGOUTD(Out) do {std::clog << __FILE__ << ":" << __LINE__ \
<< " " << Out << std::endl;}while(0)
and later use LOGOUTD("x="<<x)
. Then, if you have defined appropriate std::ostream& operator << (std::ostream&, const Foo&)
for your class Foo
, you could have x
being an instance of it.
PS. See this for the explanation about do{
...}while(0)
in macros.