开发者

generating method name in a macro

开发者 https://www.devze.com 2023-01-04 07:11 出处:网络
I need to automatically prependmethod 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\'

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号