To give a background I want to visually print the log messages based on the call stack in a neat way.
For example.
Should give the output as
foo(){
printf("Foo was called");
bar();
}
bar(){
printf("bar was called");
jai();
}
jai(){
printf("Jai was called");
}
<<< OUTPUT >>>
Foo was called.
bar was called
jai was called.
I don't want to pass the number of spaces as the argument to these function or maintain a global count of the same.
I was thinking that if we can get the position of the function in the call stack then we can fill in the same number of spaces before the text to get the required format of output.
I see backtrace function call provides that with the whole stack name returned as well. It returns the total number of functions in the call as well.
Is there any functions that returns the position only ?
They are one and the same.
If the call stack is:
and you want to know that
jai
is 4 levels deep, then total number of functions is the level ofjai
.If you want to ignore
main
and only count "your" functions, subtract 1 from total number of functions.