Macro auto-injecting argument without VARIADIC support

204 views Asked by At

I have a macro with varargs that auto injects some arguments, like the first below injecting the argument "__FNAME__":

#ifdef VERBOSE
#define logdbg(format, ...) debugff(__FNAME__, format, ##__VA_ARGS__)
#elif defined(NORMAL)
#define logdbg(format, ...) debugf(format, ##__VA_ARGS__)
#else
#define logdbg(format, ...) /* debud off */
#endif

But I need to keep this macro working with compilers without MACRO VARIADIC support (in SCO Unix and AIX 4.3 Copmiler v3).

In these environments I have now:

#ifdef VERBOSE
#define logdbg debugff(__FNAME__, format, ##__VA_ARGS__)
#elif defined(NORMAL)
#define logdbg debugf
#else
#define logdbg if(1);else debugf
#endif

These compilers didn't accepted the comment in the last macro definition, and I get the if(1);else blablabla that works fine from https://stackoverflow.com/a/687412/926064

But I need yet a solution to the first case, where an argument is "injected" by macro.

Some workaround to do that ?

EDIT:

As it isn't a software with multithread support, I'm thinking to change the debug 'framework' to inject the arguments using side functions to set values in 'context' variables (global, static, etc):

#define logdbg pass_args(__FNAME__); debugf

More possibles workarounds ?

1

There are 1 answers

7
jxh On

Assuming it is impossible to use a different compiler (which seems a dubious requirement, but let's put that aside), for certain, you will need a different function for logdbg to expand into. Probably, that function would take the __FNAME__ argument from another source, like a global variable.

#define logdbg ((logdbg_fname__ = __FNAME__), debugff_broken)

void debugff_broken(const char *fmt, ...) {
    extern const char *logdbg_fname__;
    va_list ap;
    va_start(ap, fmt);
    vdebugff(logdbg_fname__, fmt, ap);
    va_end(ap);
}

Where vdebugff is like debugff except it takes a va_list.

If thread safety is required, use thread specific storage instead of a common global.