Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the function name while in a function for debug strings?

Tags:

c++

debugging

qt

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());
}
like image 463
Phil Hannent Avatar asked Oct 06 '08 10:10

Phil Hannent


People also ask

How do you name a string as a function?

Use the __name__ Property to Get the Function Name in Python To access the __name__ property, just put in the function name without the parentheses and use the property accessor . __name__ . It will then return the function name as a string.

How do you return a name in Python?

Using __name__ method, we can return the function name in the python program. In our example, we have created a function with the name 'basket' and then we have returned basket. __name__ . Everytime this function will be called function name will be printed on the display screen.


2 Answers

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

like image 56
Torbjörn Gyllebring Avatar answered Oct 04 '22 22:10

Torbjörn Gyllebring


I see from your example that you are using Qt. In which case your best bet 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.

like image 29
Evan Teran Avatar answered Oct 04 '22 20:10

Evan Teran