0

I have situation where:

#define DBG_ALOGD(a)   ALOGD("%s:%d: ", __FUNCTION__, __LINE__); ALOGD a
#define DBG_MSG(a)    do { if (debuggable) {DBG_ALOGD(a);} } while (0)

DBG_MSG(a) used by the applications to print the logs.

DBG_MSG("Hi %d", 2); will print as

LOG_TAG: function_name:121
LOG_TAG: Hi 2

I wanted combine FUNCTION:LINE along with the "Hi 2" in single line.

I tried

#define DBG_ALOGD(a)   ALOGD("%s:%d:%s", __FUNCTION__, __LINE__, a)
#define DBG_MSG(a)    do { if (debuggable) {DBG_ALOGD(a);} } while (0)

It didn't worked as a is not a plain string it also have %d Please suggest how can I change the #define to have a combination of these.

Mathieu
  • 8,840
  • 7
  • 32
  • 45
Suman
  • 3
  • 2

1 Answers1

0

I think you can use the macro variable parameters (variadics) :

#include <stdio.h>

#define DBG_ALOGD(fmt, ...)   ALOGD("%s:%d: " fmt, __FUNCTION__, __LINE__, __VA_ARGS__ );
#define DBG_MSG(fmt, ...)    do { if (debuggable ) {DBG_ALOGD(fmt, __VA_ARGS__ );} } while (0)

int main(void)
{
    int debuggable = 1;
    DBG_MSG("%s(%d)\n", "test", 0);
    DBG_MSG("%s", "hello");

    return 0;
}

I tested it with printf function instead of ALOGD, but I think that the result will be the same.

Warning, you will not be able to call directly DBG_MSG("simple string") since the compiler will wait for something not empty in ...

Mathieu
  • 8,840
  • 7
  • 32
  • 45