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
You need to keep track of the call depth:
Usage example:
Output:
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_logactually perform the logging, but for exposition purposes, this is the simplest way to demonstrate what you are trying to do.)