利用 __FUNCTION__ 宏打印函数调用信息

__FUNCTION__ 宏表示当前所在函数名;

__FILE__ 宏表示当前所在文件路径;

__LING__ 宏表示当前所在行;

利用对象离开函数时调用析构函数销毁的特点,打印出函数执行结束的信息

代码:

#include <iostream>
#include <cstdio>

class FunctionCallLogger
{
public:
    FunctionCallLogger(const char* functionName)
    {
        strcpy(m_functionName, functionName);
        printf("%s:  %s Enter!   line:%d 
", __FILE__, m_functionName, __LINE__);
    }
    ~FunctionCallLogger()
    {
        printf("%s   %s Leave!   line:%d 
", __FILE__, m_functionName, __LINE__);
    }
private:
    char m_functionName[256];
};
#define LOG_FUNCTIONCALL FunctionCallLogger(__FUNCTION__);

int main()
{
    LOG_FUNCTIONCALL
    return 0;
}
原文地址:https://www.cnblogs.com/zuibunan/p/3515965.html