1

I want to develop indented trace for our large C++ code base that will be perticularly helpful for developers to find the issues. I want to have indented trace functionality. E.g Consider following code:-

 void FunctionA()
{
     TR_ENTER("Function A");
     TR_PRINT("Dignostic message Function A");
     FunctionB(); // Call function B   
}

 void FunctionB()
{
    TR_ENTER("Function B");
    TR_PRINT("Dignostic message Function B");
    FunctionC(); // Call function B  
}

void FunctionC()
{
   TR_ENTER("Function C");
   TR_PRINT("Dignostic message Function C");          
}

As you can see the calls above are nested within each other. I want to generate trace log as shown below:

Function A - Start
Dignostic message Function A
     Function B - Start
     Dignostic message Function B
            Function C - Start
            Dignostic message Function C
            Function C - End
     Function B - End
Function A - End

TR_ENTER and TR_PRINT are some macros that I have use as example. To say that the function start I have use TR_ENTER and for printing some dignostic messages I have used TR_PRINT.

As you can see traces for nested function call are indented within each other. May I know is there anything already available so that I could prevent myself to work on reinventing the wheel.

Thanks, Omky

Omkar
  • 2,129
  • 8
  • 33
  • 59

2 Answers2

7

You need to keep track of the call depth:

class trace_entry;

class trace_log {
public:
    trace_log() : depth_(0) { }

private:
    // noncopyable so we don't accidentally copy it
    trace_log(trace_log&);
    void operator=(trace_log);

    friend trace_entry;

    int depth_;
};

class trace_entry {
public:
    trace_entry(trace_log& log, const std::string& frame)
        : log_(log), frame_(frame) {
        std::cout << std::string(4 * log.depth_, ' ') 
                  << "ENTER " << frame_ << std::endl;
        ++log_.depth_;
    }

    ~trace_entry() {
        --log_.depth_;
        std::cout << std::string(4 * log_.depth_, ' ') 
                  << "EXIT " << frame_ << std::endl;
    }
private:
    // noncopyable so we don't accidentally copy it
    trace_entry(trace_entry&);
    void operator=(trace_entry);

    trace_log& log_;
    std::string frame_;
};

Usage example:

void a(trace_log& log) {
    trace_entry e(log, "a");
}

void b(trace_log& log) { 
    trace_entry e(log, "b");
    return a(log);
}

int main() {
    trace_log log;
    trace_entry e(log, "main");
    b(log);
}

Output:

ENTER main
    ENTER b
        ENTER a
        EXIT a
    EXIT b
EXIT main

This is easily extensible to support alternative forms of logging, allowing additional log messages, and just about anything else you want to do. (It would be far better to have the trace_log actually perform the logging, but for exposition purposes, this is the simplest way to demonstrate what you are trying to do.)

James McNellis
  • 348,265
  • 75
  • 913
  • 977
  • Thank you very much James... I will try this approach. – Omkar Jun 02 '11 at 06:26
  • 1
    If you know you're single threaded you can make your `trace_log` a global variable. If you're multithreaded you can probably use perthread variables to get the same effect. That way you wont need to modify every function call in your application to add and pass the `trace_log` variable. – Michael Anderson Jun 02 '11 at 07:41
  • Another thing to watch out for.. the compiler wont complain if you write `trace_entry(log,"foo");` rather than the correct `trace_entry e(log,"foo");`. The first creates a temporary and immediately destroys it, outputting both ENTER and EXIT logs immediately. The second is the correct form and outputs the ENTER on object creation, and EXIT when it goes out of scope (usually at function exit). – Michael Anderson Jun 02 '11 at 07:44
  • Even though it performs the task, the question is how does it fit into your current architecture. Someday you will need logging anyways and then you want to control the output stream, the format etc... Finally ending up writing any existing logging library again ;) – grundprinzip Jun 02 '11 at 08:00
0

There are a lot of logging libraries for C++ out there, in particular I can recommend Log4cxx. They will help you building and configuring the log message output of you application. To analyze the log files you will need an additional tool like Apache Chainsaw.

See as well Is there a log file analyzer for log4j files?

Community
  • 1
  • 1
grundprinzip
  • 2,471
  • 1
  • 20
  • 34