Macro question again

Joe MacDonald joe@deserted.net
Fri May 10 12:28:00 GMT 2002


Hey everyone,

About six months ago I asked almost exactly the same question and got an
answer from John Love-Jensen, (if you're still out there John, thanks!)
but now I'm in a slightly different situation and, while not pressing,
I'd really like to know if anyone has come up with an alternative way to
do this.

I have the following macro:

#define trace(level, format, ...)                                    \
do {                                                                 \
   if (level < debugLevel)                                           \
   {                                                                 \
       fprintf(stderr, "%s:%s:%d> ", __func__, __FILE__, __LINE__);  \
       fprintf(stderr, format, ##__VA_ARGS__);                       \
   }                                                                 \
} while(0)

So that I can make calls like:

   trace(5, "some trace information = %d\n", anInteger);

Throughout my code.  It works fine, but I get a pile of "pasting would
not give a valid preprocessing token" warnings from gcc 2.96 (ugh) and
3.0.1 on my RH 7.0 linux box.  John had suggested a bit of a macro-dance
using vfprintf(...) but I'm afraid I haven't been able to arrange the
same functionality without the warnings.  Is this approach fundamentally
flawed?  Can someone suggest a better (but still C) way to do the same
thing with a macro?

Thanks,
-Joe.

--
Joe MacDonald
:wq



More information about the Gcc-help mailing list