Printing out function name

T

Travis

Is there a way to print out the name of the function calling another
function? For example:

void foo()
{

debugPrint("Beginning calculation...");

}

void debutPrint(string text)
{
cout << SOMETHING << text << endl;
}

so that i would get something like

"(foo) Beginning Calculation..."
 
K

Keith Willis

Is there a way to print out the name of the function calling another
function? For example:

void foo()
{
debugPrint("Beginning calculation...");
}

void debutPrint(string text)
{
cout << SOMETHING << text << endl;
}

so that i would get something like

"(foo) Beginning Calculation..."

void foo()
{
debugPrint(__FUNCTION__,"Beginning calculation...")
}

void debugPrint(string func, string msg)
{
cout << "(" << func << ") " << msg << endl;
}

Would that do it?
 
R

red floyd

Travis said:
Is there a way to print out the name of the function calling another
function? For example:

void foo()
{

debugPrint("Beginning calculation...");

}

void debutPrint(string text)
{
cout << SOMETHING << text << endl;
}

so that i would get something like

"(foo) Beginning Calculation..."

void foo()
{
debugPrint("foo", "Beginning calculation...");
}

void debugPrint(const std::string& where,
const std::string& text)
{
std::cout << where << ": " << text << std::endl;
}
 
T

Travis

void foo()
{
debugPrint(__FUNCTION__,"Beginning calculation...")

}

void debugPrint(string func, string msg)
{
cout << "(" << func << ") " << msg << endl;

}

Would that do it?

Yeah that's actually what i have implemented now. The __FUNCTION__
thing is great but is a gcc/g++ only thing right? I was more curious
if there was a c++ thing I wasn't aware of.
 
H

hodges.r

Yeah that's actually what i have implemented now. The __FUNCTION__
thing is great but is a gcc/g++ only thing right? I was more curious
if there was a c++ thing I wasn't aware of.- Hide quoted text -

- Show quoted text -

I have used __FUNCTION__ in microsoft vc++
 
G

Guest

I have used __FUNCTION__ in microsoft vc++

I thought __FUNCTION__ was VC and __func__ gcc. Anyway, in the next
version of the standard it will be __func__ (which I personally do not
like since it it is not similar to __LINE__ and __FILE__) as in C.
 
V

Victor Bazarov

Travis said:
Is there a way to print out the name of the function calling another
function? For example:

void foo()
{

debugPrint("Beginning calculation...");

}

void debutPrint(string text)
{
cout << SOMETHING << text << endl;
}

so that i would get something like

"(foo) Beginning Calculation..."

In some compilers you can do this trick:

#define debugPrint(a) debugPrintFunction(__FUNCTION__, a)

void debugPrintFunction(string const& func, string const& text)
{
cout << "(" << func << ") " << text << endl;
}

void foo()
{
debugPrint("blah");
}

The macro __FUNCTION__ is not standard (yet, anyway) but some compilers
do implement it to become the name of the current function (a convention
of sorts). IIRC, anyway.

V
 
B

BobR

Erik Wikström wrote in message...
I thought __FUNCTION__ was VC and __func__ gcc. Anyway, in the next
version of the standard it will be __func__ (which I personally do not
like since it it is not similar to __LINE__ and __FILE__) as in C.

GCC has __FUNCTION__ and __PRETTY_FUNCTION__. __func__ is C99.

From GCC docs:
"
Function Names as Strings
GCC predefines two magic identifiers to hold the name of the current
function. The identifier __FUNCTION__ holds the name of the function as it
appears in the source. The identifier __PRETTY_FUNCTION__ holds the name of
the function pretty printed in a language specific fashion.
"
.... and later:
"
Note that these semantics are deprecated, and that GCC 3.2 will handle
__FUNCTION__ and __PRETTY_FUNCTION__ the same way as __func__. __func__ is
defined by the ISO standard C99:
"
 
J

Justin Spahr-Summers

I thought __FUNCTION__ was VC and __func__ gcc. Anyway, in the next
version of the standard it will be __func__ (which I personally do not
like since it it is not similar to __LINE__ and __FILE__) as in C.

Not really pertinent to the discussion at hand, but I believe it's in
lowercase because it's a "predefined identifier" and not a macro. The
C99 standard describes it as if the following appeared right after the
opening brace of a function definition:

static const char __func__[] = "function-name";
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,197
Messages
2,571,040
Members
47,642
Latest member
arunkumar99

Latest Threads

Top