Reputation: 327
I need to automatically prepend method name to some logging messages. I've been using __FUNCTION__
to do it but it generates the fully qualified name of the method ( namespace::class:method ). So it's wasting a lot of space and makes logs less readable. Is there any way to append only the method name in a MACRO, without any unnecessary qualifiers?
Upvotes: 2
Views: 542
Reputation: 23619
Write a function that takes a char* argument and returns a pointer to the function name in it. Then write
MyFunction(FUNCTION)
Instead of
FUNCTION
This has also the advantage that you can dynamically switch between short and long names.
Upvotes: 1
Reputation:
If your logging code looks like this:
#define LOGCALL \
clog << "Called " << __FUNCTION__ << endl;
then you can simply write a global function to trim the function name as required and say:
#define LOGCALL \
clog << "Called " << MyTrim( __FUNCTION__ ) << endl;
Upvotes: 1