I decided to make myself a neat C debugging macro, not quite sure what I really wanted (and, being ignorant when it comes to macros), I turned to Google. Some time later, and now I think I know what I want, but not how it works. I was not very lucky to get decent information about macros and debugging methods.
What I used in the past was something like this:
#ifdef DEBUG #define DBG(x) printf x #else #define DBG(x) #endif
The problem is that it can become quite confusing, and eventually you end up commenting on old debugging messages, although they will probably need you later.
The best example I've found is some slides from the advanced c course, which can be found here: http://www.mpi-inf.mpg.de/departments/rg1/teaching/advancedc-ws08/script/lecture07.pdf (the corresponding parts are slide 19-23, but most of them are given below)
Being lecture slides, they, unfortunately, need some explanation. But they mention something that seems quite useful:
DBG((MOD_PARSER , "z = %d\n", z));
Where MOD_PARSER is the debugging module / category, and the rest of the arguments are for printing. A.
And the DBG implementation:
#ifdef PRGDEBUG #define DBG(x) dbg_printer x #else #define DBG(x) #endif void dbg_printer(int module , const char *fmt, ...);
Problem # 1 is writing the dbg_printer function, I'm not sure how to pass a variable number of arguments to the printf statement.
The slides discuss how to gracefully add new modules, and I'm sure I didn't understand that at all, but anyway ...
*How to add new modules elegantly *Add a file debug_modules.def ADD_MOD(0, PARSER) ADD_MOD(1, SOLVER) ADD_MOD(2, PRINTER)
...
*"Generate" an enum with debug modules: debug.h ... #define ADD_MOD(num, id) MOD_ ## id = 1 << num, enum _debug_modules_t { #include "debug_modules.def" }; #undef ADD_MOD ...
...
*Preprocessor yields enum _debug_modules_t { MOD_PARSER = 1 << 0, MOD_SOLVER = 1 << 1, MOD_PRINTER = 1 << 2, };
I don’t understand why you would leave a shift in the values of the enumeration elements, some graceful trick that I am missing?
Apart from the slides above, I have not seen a single example, no article / message, or even mention of it, so maybe this is not even suitable for my purposes. Does this sound reasonable and are similar methods used?
At the moment, the question is how to implement dbg_printer and really, how the numbering of debug modules should work, but seeing how I could misunderstand everything that could change :(