My teacher always says if you need to use a debugger, you don’t understand your program. So, maybe I’m a shitty programmer, but these macros have helped me out.
Sometimes you need to check variables:
#define LOG(x, fmt) fprintf(stderr, "LOG: %s : %" #fmt "\n", #x, x) LOG(butt, f);
Sometimes you just want to print something, to see if a loop or branch is being executed:
#define CHECK(x) fprintf(stderr, "CHECK: %s\n", #x) CHECK(hi);
Then you pass into CHECK whatever string you want to be printed.
Note I didn’t put a semicolon at the end of the macros. If I did, then I would have to remember to not put one when I used the macros, otherwise I would end up with two semicolons when the macro expanded!
I’ve seen a lot more complicated debugging systems implemented in larger projects, but these will work fine for now.
Tags: programming debugging C macros preprocessor shitty-programming logging macro