Calling function referencing?

Hi, all:

As my login name shows, I am more familiar with Java then C++, but am currently in a C++ project.

My problem is to add debug logging without being too intrusive, so I want to write a printDebug function that takes the text (from a .h file that has a reference like "MEM_LOC_ACCESS" and the corresponding "human-friendly" string) to be put into the log as an argument. A debug flag is set in a .h file whenever this printing should occur.

All that is working fine. But to add an element of granularity (since some of the debug messages can come from different functions), I want to be able to also print the calling function's name in the debug log. Without the calling function having to pass anything. So how do I access the call stack to print the next element on the stack - which should be the calling method, right?

Any help would be greatly appreciated.
Addresses get pushed onto the stack, not function names. The best you can do is print the return address.
At which point this is all not only compiler-specific, but even compiler option specific.
This might be what you're looking for.

1
2
3
4
5
6
7
8
9
#include <iostream>

void func1(){
    std::cout << __FILE__ << '\n' <<__FUNCTION__ << '\n' << __LINE__ << '\n';
}

int main(){
    func1();
}
Topic archived. No new replies allowed.