I’m currently writing a program that has debug output strewn throughout it. This is all well and good, but I’d like to be able to advance the tab position for things in different scopes, for instance, this is what I have right now:
#ifndef NDEBUG
printf("Updating player\n");
#endif
player.Update();
#ifndef NDEBUG
printf("Done updating player\n");
#endif
I’d like to have so that all the output called between these two blocks is advanced by one tab position; however, simply adding tabs to the beginning of Player::Update() output is incredibly clunky AND difficult to maintain.
Anybody got any help? (Note: I have no problem with using cout instead; I was just recently lectured about the overhead and insecurity with cout)
You could have a class that essentially maintained a “tab count” and had a
print_linefunction: when called, it would output tab-count tabs, and then print the line. While you could have aincrement_indentfunction, you could create a sister objectTabIndentusing RAII: When it is created, increment the tab, when it is destructed, decrement the indent:Good use of
inlines will let the compiler optimize them out when their bodies are empty. Use#ifndefin theOutputterclass, and let the compiler optimize the rest out.