1

I want to print some strings in functions, but indented depending on how far the depth of the function calls has gone. My solution so far includes a class from this thread like the following:

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;

void func1(){
    DepthCounter dc("name1");
}

void func2(){
    DepthCounter dc("name2");
    func1();
}

so the print in the first construction will have 1 depth and the second one 2 depth (indent). but I want to make this print multiple times. that is

void func2(){
    DepthCounter dc("name0");
    DepthCounter dc1("name1");
    DepthCounter dc2("name2");
    DepthCounter dc3("name3");
    func1();
}

but I don't find it nice, let alone the fact that this construction increases the depth although it is still in the same function. Is there a better way do have such functionality?

ideally I want something like:

void func1(){
    funcX("name5");
}

void func2(){
    funcX("name0");
    funcX("name1");
    funcX("name2");
    funcX("name3");
    func1();
}

Does anyone know an alternative way?

Alejandro
  • 879
  • 11
  • 27
  • If you want multiple depth counters, you could make it a template with an `int` parameter. This would allow to manage multiple of such counters concurrently without the necessity to change your concept much. – Scheff's Cat Oct 21 '21 at 09:53
  • Maybe, you're looking for a kind of stack trace (aka. call stack) like you may have seen already in gdb. I roughly remember that there already was once such a question. The stack trace part of gdb is provided by a library which could be used in your application as well. Nevertheless, it will be a platform-dependent solution always probably, as such details are usually subject of implementation detail rather than the C++ standard. – Scheff's Cat Oct 21 '21 at 09:59
  • FYI: [SO: print call stack in C or C++](https://stackoverflow.com/q/3899870/7478597) (found by [google "site:stackoverflow.com c++ using stack trace in application"](https://www.google.com/search?q=site%3Astackoverflow.com+c%2B%2B+using+stack+trace+in+application)) – Scheff's Cat Oct 21 '21 at 10:01
  • Alipapa: Did the answer help? Please ask if it was unclear. – Ted Lyngmo Oct 24 '21 at 16:23

1 Answers1

2

You could store the function name inside the DepthCounter and provide an operator<< overload to use inside the functions:

class DepthCounter {
    static int depth;
    std::string m_name;
public:
    DepthCounter(const std::string& name) : m_name(name) {
        std::cout << std::string(depth*2, ' ') << "->" << m_name << '\n';
        ++depth;
    }
    ~DepthCounter() {
        --depth;
        std::cout << std::string(depth*2, ' ') << "<-" << m_name << '\n';
    }

    template<class T>
    friend DepthCounter& operator<<(DepthCounter& dc, const T& val) {
        std::cout << std::string(depth*2, ' ') << dc.m_name << ": " << val << '\n';
        return dc;
    }
};

int DepthCounter::depth = 0;

And use it like so:

void func1(){
    DepthCounter dc(__func__);
    dc << "here's something";
}

void func2(){
    DepthCounter dc(__func__);
    dc << "name1";
    dc << "name2";
    func1();
    dc << "some more after calling func1";
}

Demo

Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108