我想实现一个函数跟踪器,它可以跟踪函数执行所花费的时间。我有以下相同的课程:-
class FuncTracer
{
public:
FuncTracer(LPCTSTR strFuncName_in)
{
m_strFuncName[0] = _T('\0');
if( strFuncName_in ||
_T('\0') != strFuncName_in[0])
{
_tcscpy(m_strFuncName,strFuncName_in);
TCHAR strLog[MAX_PATH];
_stprintf(strLog,_T("Entering Func:- <%s>"),m_strFuncName);
LOG(strLog)
m_dwEnterTime = GetTickCount();
}
}
~FuncTracer()
{
TCHAR strLog[MAX_PATH];
_stprintf(strLog,_T("Leaving Func:- <%s>, Time inside the func <%d> ms"),m_strFuncName, GetTickCount()-m_dwEnterTime);
LOG(strLog)
}
private:
TCHAR m_strFuncName[MAX_PATH];
DWORD m_dwEnterTime;
};
void TestClass::TestFunction()
{
// I want to avoid writing the function name maually..
// Is there any macro (__LINE__)or some other way to
// get the function name inside a function ??
FuncTracer(_T("TestClass::TestFunction"));
/*
* Rest of the function code.
*/
}
我想知道是否有什么方法可以从函数内部获取函数的名称?基本上我希望我的班级的用户简单地创建一个相同的对象。他们可能不传递函数名称。
C99 has __func__
,但对于 C++,这将是特定于编译器的。从好的方面来说,一些特定于编译器的版本提供了额外的类型信息,当您在模板化函数/类中进行跟踪时,这尤其有用。
-
MSVC http://msdn.microsoft.com/en-us/library/b0084kay.aspx:
__FUNCTION__
, __FUNCDNAME__
, __FUNCSIG__
-
GCC http://gcc.gnu.org/onlinedocs/gcc/Function-Names.html:
__func__
, __FUNCTION__
, __PRETTY_FUNCTION__
Boost库已经定义了宏BOOST_CURRENT_FUNCTION
对于标头中的大多数 C++ 编译器升压/current_function.hpp http://www.boost.org/doc/libs/1_62_0/boost/current_function.hpp。如果编译器太旧而无法支持此功能,则结果将为“(未知)”。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)