0

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?

1
  • I think you have 2 extra parenthesis in LOGDEBUG call. (you pass only 1 argument)
    – Jarod42
    Commented Feb 18, 2014 at 11:15

1 Answer 1

1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.