Question

I'm trying to create LOGDEBUG macro:

#ifdef DEBUG
#define DEBUG_TEST 1
#else
#define DEBUG_TEST 0
#endif

#define LOGDEBUG(...) do { if (DEBUG_TEST) syslog(LOG_MAKEPRI(LOG_SYSLOG, LOG_DEBUG),  __VA_ARGS__); } while (0)

...

size_t haystack_len = fminl(max_haystack_len, strlen(haystack_start));
LOGDEBUG(("haystack_len %ld\n", haystack_len));

I am not using # or ## parameters to stringify the arguments, and yet g++ apparently tries to stringify them:

numexpr/interpreter.cpp:534:5: error: invalid conversion from ‘size_t {aka long unsigned int}’ to ‘const char*’ [-fpermissive]

Note that haystack_len is size_t and I do not convert it to char* in the macro, yet compiler sees it as such. Does g++ implicitly tries to convert macro arguments to strings?

How to fix that? I mean, I'm using gnu LOG_MAKEPRI macro for syslogging, is it this macro that may be causing trouble? Also, is there some way to see the macro-expanded code?

Was it helpful?

Solution

How to fix that?

LOGDEBUG(("haystack_len %ld\n", haystack_len)); call the macro with one unique argument. So it will produce:

do { if (DEBUG_TEST) syslog(LOG_MAKEPRI(LOG_SYSLOG, LOG_DEBUG), ("haystack_len %ld\n", haystack_len)); } while (0);

And ("haystack_len %ld\n", haystack_len) use comma operator and result in haystack_len

So you have to call it that way: LOGDEBUG("haystack_len %ld\n", haystack_len);

Also, is there some way to see the macro-expanded code?

gcc -E may help.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top