In order to debug a recursive program, I find it useful to visualize how deeply nested my function calls are. I'd love to have something like __func__
, but for how deep my stack trace is instead of what my function name is. I understand that it would be impossible for a compiler to simply know this, because how nested you are is a dynamically generated value. But it wouldn't be hard for a compiler to add capabilities to implement this, you could simply add 1 to a global counter before each call
and subtract 1 before eat ret
.
I'm using the following debug statement (found here):
#define printdbg(Level, formatString, ...) \
do { \
fprintf(stderr, "%s:%d %s: " formatString "\n", \
__FILE__, __LINE__, __func__, ##__VA_ARGS__); \
if (Level == LEVEL_ERROR) { printf("quitting\n"); exit(1); }\
} while (0)
I'd love to add an additional Predefined Identifier at the start, where I can leverage something of the form printf("%*s", __NEST__+1, ":")
, to print a total of __NEST__
spaces at the beginning of each debug statement, to let me visualize how deep into the stack each debug statement was made from.
I know I could simply have a global counter that I ++
at the start of each function, and --
at the end, but I just learned about predefined identifiers and they're so cool! Also, no need to re-invent the wheel.
I can't find a list of supported predefined identifiers anywhere online. All I've found is this and this, neither of which claim to be comprehensive. If the equivalent of __NEST__
exists, somebody here probably knows the one word I'm looking for. If it doesn't exist, then where can I find a well-documented list of all the predefined identifiers?