1

Possible Duplicate:
Macro / keyword which can be used to print out method name?

Is there an effective way in C++ to retrieve full signature of a function at run-time?

I need this for logging purpose. I'm aware of macro like __FUNCTION__, but doesn't serve the purpose as it just returns the function names. There could be many overloaded versions of same function, I want to log the full function signature. I'm looking for a solution that work correctly if even function signature is modified. The solution should always log the current function signature

void log(const char* const message)    
    {
        cout << message << endl;
    }

    void ABC(const int& number)
    {
        Log(???); // what should I pass to this function so full signature of the ABC function is logged??
    }
Community
  • 1
  • 1
  • 1
    Do you want to print the parameter names (e.g. "number") or the parameter values (e.g. "42")? – selbie Feb 05 '13 at 06:17
  • Parameter values are not part of the signature... – Basile Starynkevitch Feb 05 '13 at 06:18
  • Yes, I want full signature. With above example, output should be "void ABC(const int& number)" – Santosh Chauhan Feb 05 '13 at 06:20
  • Take a look at this blog: [How to get function's name from function's pointer in C? (Windows)](http://ivbel.blogspot.com/2012/02/how-to-get-functions-name-from.html) It shows that if you link with Dbghelp.lib and use `SymGetSymFromAddr64` you can get the full signature of a function at run-time... also see http://stackoverflow.com/questions/6205981/windows-c-stack-trace-from-a-running-app – amdn Feb 05 '13 at 06:51

2 Answers2

3

The Predefined Macros (C/C++) page on MSDN has a list of all the available macros. The one you are looking for is, likely, __FUNCSIG__.

Yakov Galka
  • 70,775
  • 16
  • 139
  • 220
1

In general there is not, but if using GCC you could use the __PRETTY_FUNCTION__ pseudo macro....

 void ABC(const int& number) {
     Log(__PRETTY_FUNCTION__);
     /// etc...
 }

The Clang/LLVM compiler also has it.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547