One way is to create an RAII class that counts the depth, and instantiate it at the top of each function you wish to track. I've done this sort of thing for debugging purposes.
class DepthCounter
{
static int depth;
public:
DepthCounter(const std::string& name)
{
std::cout << std::string(depth*2, ' ') << name << " // depth " << depth << '\n';
++depth;
}
~DepthCounter()
{
--depth;
}
};
int DepthCounter::depth = 0;
You can instantiate this in each function with something like:
DepthCounter dc(__func__);
You can name the instance whatever you wish, but without giving it a variable name it will just be a temporary and get destroyed essentially at the semicolon. By making it a variable, it lives until the function exits by whatever means, whether by falling off the end, an explicit return, or via an exception.
I didn't show all the code (have to leave something for you to do), but the output I got looks like:
func0 // depth 0
func1 // depth 1
func2 // depth 1
func3 // depth 2
func4 // depth 1
func5 // depth 2
func6 // depth 3
func7 // depth 4
func8 // depth 2
func9 // depth 0