Is it possible to use __func__ in gcc 3.3+ in the old way? (C ++)

With gcc versions up to 3.3 and with the MS compiler, I use the following macro:

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

Using:

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

Output:

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

It is extremely convenient when we have many systems, all sending data. Its a one-line macro that allows us to filter the output accordingly. Small code, great use, nice to me.

As the definition of __FUNCTION__ (and __func__ in C ++) changed (to make it conform to standards), it also made this macro inoperative.

I have work using a function that builds a string manually, but I like my macro.

I skipped the easy way to get this simple single line macro that still works under Gcc 3.3?

: D

+4
source share
1 answer

Since __FUNCTION__ and __func__ are a predefined identifier, not a string literal, you cannot use it to concatenate preprocessor string literals. But you can use it in printf format. Also note the use of ##args instead of __VA_ARGS__ to use GNU style macro variables to solve the comma problem between __func__ and possibly null arguments.

 #define DEBUG_WARNING(fmt, args...) \ printf(">WARNING: %s() " fmt "\n", __func__, ##args) 
+5
source

All Articles