3

I am dealing with the log macros that are defined something like:

#define LOGD(...)         rtt_printf(TERMINAL_DEBUG, ##__VA_ARGS__)
#define LOGV(...)         rtt_printf(TERMINAL_NORMAL, ##__VA_ARGS__)

It is all good, but I would like to disable debug logs for release build. But since I am using -Werror and -Wall I simply get errors when I define LOGD without the rtt_printf(). Is there any way at all (more dynamic way) other than explicitly declaring (void)var; before the LOG call to suppress the unused parameter warnings?

LPs
  • 16,045
  • 8
  • 30
  • 61
arapEST
  • 491
  • 1
  • 4
  • 16

1 Answers1

0

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.

Community
  • 1
  • 1
Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547