tags:

views:

762

answers:

5

I want to output the function name each time it is called, I can easily copy and paste the function name, however I wondered if there was a shortcut that would do the job for me?

At the moment I am doing:

SlideInfoHeader* lynxThreeFile::readSlideInfoHeader(QDataStream & in)
{
    qDebug("lynxThreeFile::readSlideInfoHeader");
}

but what I want is something generic:

SlideInfoHeader* lynxThreeFile::readSlideInfoHeader(QDataStream & in)
{
    qDebug(this.className() + "::" + this.functionName());
}
+18  A: 

"__FUNCTION__" is supported by both MSVC and GCC and should give you the information you need.

Torbjörn Gyllebring
Hadn't realized it was supported on MSVC
Robert Gould
It was supported from VC7. Version 6 did not support "__FUNCTION__"
David Dibben
+4  A: 

If you check out boost there is a macro BOOST_CURRENT_FUNCTION that is portable across platforms. In the C99 standard there is a compiler variable __func__ that has the desired effect. I believe has been accepted into the C++0x standard. A reasonable number of compilers will already support this.

Matt Price
+6  A: 

I see from your example that you are using QT. In which case your best best is to use Q_FUNC_INFO found in <QGlobal>. Here's the description:

Expands to a string that describe the function the macro resides in. How this string looks more specifically is compiler dependent. With GNU GCC it is typically the function signature, while with other compilers it might be the line and column number.

Evan Teran
+6  A: 

If you are using gcc you may find "__PRETTY_FUNCTION__" more to your liking.

A: 

__func__ is c99 ( which does in turn mean it might not work with visual studio - but hey it's standard :o) )

__FUNCTION__ works pretty much everywhere

__PRETTY_FUNCTION__ is gnu specific and returns the full qualified name with (namespaces?), classname, returntype, functionname, parameterslist

Ronny