Barak
Barak

Reputation: 41

Unused variable warning from debugging code

I've built a logging system that compiles in two platforms:

1) A debug platform where the logging calls are interleaved in the code.

2) An on-chip platform in which the logging calls should not appear in the code, due to hard limitations on code size and run time.

To achieve my goal I used C macros:

#ifdef DEBUG_PLATFORM
#define LOG(log)  std::sstream s; s<<log; log_func(s);
#else
#define LOG(log)  ;
#endif

Alas, the unused variable compiler warning is giving me a hard time. For example the following code will compile in the debug platform but not in the online platform:

int a = 5;
int b = func(1,2,3);
LOG("a: "<<a<<" b: "<< b)

I'd like to free the user from thinking on these issues and doing tricks to avoid the warning (like adding (void)a). Most users don't compile the online platform and these type of errors will be found in retrospective and will cause a lot of inconvenience.

I am not allowed to change the compiler flags, using unused variable warnings is a must.

Does anybody have an idea how to overcome this difficulty? Is there a way to instruct the compiler to ignore the warning for all variables inside some scope?

Upvotes: 2

Views: 628

Answers (1)

Mirko
Mirko

Reputation: 1083

I would suggest you logging a variable at a time:

#ifdef DEBUG_PLATFORM
#define LOG(log)  { std::stringstream s; s<< #log << '=' << log << ' '; log_func(s); }
#else
#define LOG(log) (void) log;
#endif

#log will print the variable name.

(void) log will make the compiler ignore that it has not been used.

You could log more variables provided that you put more macro versions, but it will be messy. With #log and with (void) log, you can no longer pass "a: " << a to LOG

Upvotes: 1

Related Questions