Re: how could I write this cpp macro



Bilgehan.Balban@xxxxxxxxx wrote:
Hi,

I'm trying to convert this:

printf("format str %s, %s", "str arg 1", "str arg 2 etc.");

to this:

printf("%s: " "format str %s, %s", __FUNCTION__, "str arg 1", "str arg2
etc.");

All sol'ns I came up had various problems in various printf formats.
The closest I had was something like:

#if DEBUG > 0
#define pdebug(fmt ...) printf("%s: " fmt, \
__FUNCTION__, __VA_ARGS__); \
#endif

But this wont work if there's a printf with no va_args, such as:

printf("fmt string and nothing else\n");

because the comma after __FUNCTION__ will be a syntax error.

Any ideas?


#define pdebug(args...) do { \
printf ("%s:", __FUNCTION__); \
printf (args); \
} while (0);

Should do what you want.

Thanks,
Bahadir

.



Relevant Pages

  • Re: Printf("%d")
    ... > Why does a printfstatement prints this as output and when I do ... How does printf know how many args it's been passed then? ... %s says there's a string %d an int etc. ...
    (comp.lang.c)
  • Re: printf
    ... (printf(# of args, args); ... but do know that printf(); ... Kind form of RTFM, since I don't know how to write the function;-) ... Kind regards, ...
    (alt.comp.lang.learn.c-cpp)
  • A printf wrapper
    ... I'd like to write a wrapper for the printf() function. ... procedure(myPrint(port str @rest args) ... fprintf(port str args) ...
    (comp.cad.cadence)
  • Re: problem using system()
    ... printf "child died with signal %d, %s coredump\n", ... Possible unintended interpolation of @args in string at ... Global symbol "@args" requires explicit package name at ...
    (comp.lang.perl.misc)
  • Passing va_list two times?
    ... my_func (1, args); ... int my_func (int argc, ...) ... printf; ... parsing of args is OK at the first function (e.g. if I cut & paste the ...
    (comp.lang.c)