generating method name in a macro
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 开发者_StackOverflow中文版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?
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;
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.
精彩评论