tags:

views:

173

answers:

1

With gcc versions before 3.3 and with the MS compiler I use the following macro:

DEBUG_WARNING(...) printf(">WARNING: "__FUNCTION__"() " __VA_ARGS__);

Use:

DEBUG_WARNING("someFunction returned %d", ret);

Output:

>WARNING: Class::FunctionName() someFunction returned -1

Its extremely handy when we have lots of systems, all sending output. Its a single line macro, that allows us to filter the output accordingly. Small code, big use, happy me.

As the __FUNCTION__ (and __func__ in C++) definition has changed (to make it standards compliant I believe) it has also made that macro unworkable.

I've got it working using a function that builds the string by hand, but I like my macro.

Am I missing an easy way to get this simple one line macro to still work under Gcc 3.3?

: D

+2  A: 

Since __FUNCTION__ and __func__ is a predefined identifier and not a string literal, you cannot use it in preprocessor string literal concatenation. But you can use it in printf formatting. Also note the use of ##args instead of __VA_ARGS__ to use GNU style variadic macro arguments to work around the issue with the comma between __func__ and possibly zero args.

#define DEBUG_WARNING(fmt, args...) \
  printf(">WARNING: %s() " fmt "\n", __func__, ##args)
laalto
That works a treat, with a slight modification: `printf(">WARNING: %s() " fmt, FUNCTION, VA_ARGS)` the comma is required on the platforms I'm building on. Thanks for the help! : D
Danny Parker
Thanks for the tip on the ## trick.That allows me to make my macro totally platform independent without headaches. This is how my final macro looks:`#define DEBUG_WARNING(fmt, ...) printf(">WARNING %s() " fmt, __FUNCTION__ , ## __VA_ARGS__ );`This compiles fine on GCC 4.1.1, and also the microsoft compilers used on the other 2 platforms.
Danny Parker