Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find the name of the current function at runtime?

After years of using the big ugly MFC ASSERT macro, I have finally decided to ditch it and create the ultimate ASSERT macro.

I am fine with getting the file and line number, and even the expression that failed. I can display a messagebox with these in, and Abort/Retry/Cancel buttons.

And when I press Retry the VS debugger jumps to the line containing the ASSERT call (as opposed to the disassembly somewhere like some other ASSERT functions). So it's all pretty much working.

But what would be really cool would be to display the name of the function that failed.

Then I can decide whether to debug it without trying to guess what function it's in from the filename.

e.g. if I have the following function:

int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct) {    ASSERT(lpCreateStruct->cx > 0);    ... } 

Then when the ASSERT fires, the messagebox would show something like:

Function = CMainFrame::OnCreate 

So, what's the simplest way of finding out the current function name, at runtime?

It should not use MFC or the .NET framework, even though I do use both of these.
It should be as portable as possible.

like image 409
demoncodemonkey Avatar asked Mar 24 '09 20:03

demoncodemonkey


People also ask

How do you find the current function name?

In this case, we used the methodName property (getMethodName() in Java) to find the current function name.

What is __ func __ in C++?

(C++11) The predefined identifier __func__ is implicitly defined as a string that contains the unqualified and unadorned name of the enclosing function.

How do you print a function name in Python?

Use the __name__ Property to Get the Function Name in Python __name__ . It will then return the function name as a string. The below example declares two functions, calls them, and prints out their function names. Note that this solution also works with the imported and pre-defined functions.


2 Answers

Your macro can contain the __FUNCTION__ macro. Make no mistake, the function name will be inserted into the expanded code at compile time, but it will be the correct function name for each call to your macro. So it "seems like" it happens in run-time ;)

e.g.

#define THROW_IF(val) if (val) throw "error in " __FUNCTION__  int foo() {     int a = 0;     THROW_IF(a > 0); // will throw "error in foo()" } 
like image 101
Assaf Lavie Avatar answered Sep 23 '22 18:09

Assaf Lavie


The C++ preprocessor macro __FUNCTION__ gives the name of the function.

Note that if you use this, it's not really getting the filename, line number, or function name at runtime. Macros are expanded by the preprocessor, and compiled in.

The __FUNCTION__ macro, like __LINE__, and __FILE__, is part of the language standard, and is portable.

Example program:

#include <iostream> #using namespace std;  void function1() {         cout << "my function name is: " << __FUNCTION__ << "\n"; } int main() {         cout << "my function name is: " << __FUNCTION__ << "\n";         function1();         return 0; } 

output:

 my function name is: main my function name is: function1 
like image 25
YenTheFirst Avatar answered Sep 22 '22 18:09

YenTheFirst