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

时间:2023-03-09 04:01:29
利用 __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 \n", __FILE__, m_functionName, __LINE__);
}
~FunctionCallLogger()
{
printf("%s %s Leave! line:%d \n", __FILE__, m_functionName, __LINE__);
}
private:
char m_functionName[];
};
#define LOG_FUNCTIONCALL FunctionCallLogger(__FUNCTION__); int main()
{
LOG_FUNCTIONCALL
return ;
}