views:

1296

answers:

8

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?

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

+12  A: 

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()"
}
Assaf Lavie
Unfortunately, `__FUNCTION__` is not standard. `__func__` *is* standard, but it is not a string literal; it acts like a variable, so you can't concatenate it easily.
ephemient
right. but luckily the OP is using MFC and so cross-platformness is probably not a problem.
Assaf Lavie
I mentioned it because OP asked for portability.
ephemient
Unfortunately, __func__ is not part of Standard C++.
anon
__FUNCTION__ is the winner, and it works brilliantly. It even tells you the namespace. I don't require true cross-platformness but only some of the projects in my solution use MFC, so the macro has to work without MFC (that's what I meant by "as portable as possible"). Thanks to all who contributed!
demoncodemonkey
Oh right, `__func__` is specified C99 but not (yet) in C++. Sigh... I guess `__FUNCTION__` is the winner, then.
ephemient
There's also `__FUNCSIG__` (again, VC++ specific) - this one includes the full signature of function, not just its name, and is handy to distinguish overloaded functions.
Pavel Minaev
+4  A: 

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
YenTheFirst
__FUNCTION__ is not part of the C++ language standard (I didn't downvote yoou for this, but I guess someone else did)
anon
good point, I think I misread something.according to http://gcc.gnu.org/onlinedocs/gcc/Function-Names.html,__func__ is part of the C99 standard (although not necessarily everything supports it). __FUNCTION__ is not, although if the big compliers (MSDN/GCC) support it, it's effectively portable.
YenTheFirst
Note that __func__ is not part of C++ either.
anon
Thanks, nice answer. And I'll let you off for saying __FUNCTION__ is portable. It's portable enough for me anyway :)
demoncodemonkey
Thanks. :) I think I realize my mistake, too. C99 is a C standard, not C++.
YenTheFirst
+2  A: 

You can use the FUNCTION macro which at compile time will be expanded to the name of the function.

Here's an example of how to use it in an assert macro.

#define ASSERT(cond) \
    do { if (!(cond)) \
    MessageBoxFunction("Failed: %s in Function %s", #cond, __FUNCTION__);\
    } while(0)

void MessageBoxFunction(const char* const msg,  ...)
{
    char szAssertMsg[2048];

    // format args
    va_list vargs;
    va_start(vargs, msg);
    vsprintf(szAssertMsg, msg, vargs);
    va_end(vargs);

    ::MessageBoxA(NULL, szAssertMsg, "Failed Assertion", MB_ICONERROR | MB_OK);
}
Andrew Grant
Argh at the "..." but otherwise good answer, thanks.
demoncodemonkey
+3  A: 

There is no portable way of doing this. See the other aswers here for non-portable solutions.

anon
"It should be as portable as possible" - I guessed there wouldn't be a perfect solution! Luckily these answers are good enough for me though.
demoncodemonkey
+1  A: 

If you have access to the John Robbins' Debugging Applications for Microsoft® .NET and Microsoft Windows® you should definitely look at the assertions from BugSlayerUtil library on the supplied CD. They are Windows-specific, but really ultimate.

Paul
I had a look at http://www.koders.com/cpp/fid3653A5E08C30DB8B7551729FBED0BC3D51B19AD8.aspx but it didn't seem to show the function name. Therefor I think my version is more ultimate than his :D
demoncodemonkey
It shows stacktrace. Stacktrace contains function names, doesn't it?
Paul
Ah I didn't notice that. Hmmmmmmm could be useful, but wouldn't that make your lovely messagebox quite unreadable? Point taken though. Maybe in v2 of my macro ;)
demoncodemonkey
For some reason link to the book is not visible in the answer, here it is: http://www.amazon.com/Debugging-Applications-Microsoft%C2%AE-Microsoft-Pro-Developer/dp/0735615365/
Paul
+6  A: 

There's no standard solution. However, BOOST_CURRENT_FUNCTION is portable for all practical purposes. The header does not not depend on any of the other Boost headers, so can be used standalone if the overhead of the whole library is unacceptable.

fizzer
Thanks, I don't use Boost (yet) but at least I know there's a substitute for __FUNCTION__ if I ever have the need to port it.
demoncodemonkey
A: 

In GCC you can use the __PRETTY_FUNCTION__ macro.
Microsoft also have an equivalent __func__ macro although I don't have that available to try.

e.g. to use __PRETTY_FUNCTION__ putting something like this at the beginning of your functions and you'll get a complete trace

void foo(char* bar){
  cout << __PRETTY_FUNCTION__ << std::endl
}

which will output

void foo(char* bar)

You also have the __FILE__ and __LINE__ macros available under all standard c/c++ compilers if you want to output even more information.

In practice I have a special debugging class which I use instead of cout. By defining appropriate environment variables I can get a full program trace. You could do something similar. These macros are incredibly handy and it's really great to be able to turn on selective debugging like this in the field.

EDIT: apparently __func__ is part of the standard? didn't know that. Unfortunately, it only gives the function name and not the parameters as well. I do like gcc's __PRETTY_FUNC__ but it's not portable to other compilers.

GCC also supports __FUNCTION__.

Matt H