In C++ object constructors and destructors can be used to trace scopes during code execution, something like this:
#define TRACE(msg) Helper trace_helper(msg);
class Helper {
const char* _name;
Helper(const car* name) {
_name = name;
printf(“enter %s”, name);
}
~Helper() { printf(“exit %s”, _name); }
};
This allows me to trace my code, like for example
main() {
TRACE(“main”)
for(...) {
foo();
}
}
void foo() {
TRACE(“foo”)
do some stuff
if(condition) {
TRACE(“inner block”)
do some stuff
}
}
This is useful to profile code for example.
Now, not having constructors and destructors available in C, i wonder if it’s still possible to create a TRACE macro that takes a single line and will also trace the exit of the scope?