Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to get function name inside a C++ function?

I want to implement a function tracer, which would trace how much time a function is taking to execute. I have following class for the same:-

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.      */ } 

I want to know if there is any way to get the name of the function from inside of a function? Basically I want the users of my class to simply create an object the same. They may not pass the function name.

like image 287
Canopus Avatar asked Apr 09 '09 06:04

Canopus


People also ask

How can I call a function given its name as a string in C?

Put all the functions you want to select from into a dll, then use dlsym (or GetProcAddress on Windows, or whatever other API your system offers) to get the function pointer by name, and call using that.

How do I get the name of a function in C++?

You cannot get the name of the function in C++, but you can print the pointer and later check the binary (if not stripped) for the function name. The signature can be printed exactly as you are doing, just that the type name is not really 'human readable'.

Can a function have any name in C?

Every function has a name by which it is known to the rest of the program. The name of a function in C can be anything from a single letter to a long word. The ANSI Standard, however, only guarantees that C will be able to distinguish the first 31 letters of identifiers , or function and variable names.


1 Answers

C99 has __func__, but for C++ this will be compiler specific. On the plus side, some of the compiler-specific versions provide additional type information, which is particularly nice when you're tracing inside a templatized function/class.

  • MSVC: __FUNCTION__, __FUNCDNAME__, __FUNCSIG__
  • GCC: __func__, __FUNCTION__, __PRETTY_FUNCTION__

Boost library has defined macro BOOST_CURRENT_FUNCTION for most C++ compilers in header boost/current_function.hpp. If the compiler is too old to support this, the result will be "(unknown)".

like image 53
bk1e Avatar answered Sep 21 '22 10:09

bk1e