Note that there are some explanatory texts on larger screens.

plurals
  1. POC debugging macro (with different debug "sources")
    text
    copied!<p>I set out to get myself a neat C debugging macro, not really sure what I really wanted (and being clueless when it comes to macros) I turned to google. Some time later and I now think I know what I want, but not how it works. I haven't had much luck with getting decent information about macros and techniques for debugging.</p> <p>What I've been using in the past have been something like this:</p> <pre><code>#ifdef DEBUG #define DBG(x) printf x #else #define DBG(x) /* nothing */ #endif </code></pre> <p>Problem is that it can get quite messy and ultimately you end up commenting out old debug messages eventhough you probably will need them later on.</p> <p>The best example I found was from some slides from an advanced c course, which can be found here: <a href="http://www.mpi-inf.mpg.de/departments/rg1/teaching/advancedc-ws08/script/lecture07.pdf" rel="nofollow noreferrer">http://www.mpi-inf.mpg.de/departments/rg1/teaching/advancedc-ws08/script/lecture07.pdf</a> (the relevant parts are slide 19-23, but most of it is included below)</p> <p>Being lecture slides they unfortunately need some explaining to go. But they mention something that seems quite useful:</p> <pre><code>DBG((MOD_PARSER , "z = %d\n", z)); </code></pre> <p>Where MOD_PARSER is a debug module/category and the rest of the arguments are meant to be given to printf.</p> <p>And the implementation of DBG:</p> <pre><code>#ifdef PRGDEBUG #define DBG(x) dbg_printer x #else #define DBG(x) /* nothing */ #endif void dbg_printer(int module , const char *fmt, ...); </code></pre> <p>Problem #1 is to write the dbg_printer function, I'm not sure how to pass the variable number of arguments to an printf statement.</p> <p>The slides go on to discuss how to elegantly add new modules and I'm fairly certain that I haven't understood this at all, but anyway...</p> <pre><code>*How to add new modules elegantly *Add a file debug_modules.def ADD_MOD(0, PARSER) ADD_MOD(1, SOLVER) ADD_MOD(2, PRINTER) </code></pre> <p>...</p> <pre><code>*“Generate” an enum with debug modules: debug.h ... #define ADD_MOD(num, id) MOD_ ## id = 1 &lt;&lt; num, enum _debug_modules_t { #include "debug_modules.def" }; #undef ADD_MOD ... </code></pre> <p>...</p> <pre><code>*Preprocessor yields enum _debug_modules_t { MOD_PARSER = 1 &lt;&lt; 0, MOD_SOLVER = 1 &lt;&lt; 1, MOD_PRINTER = 1 &lt;&lt; 2, }; </code></pre> <p>I don't see why you would left-shift the values of the enumeration elements, some nifty trick I'm missing? </p> <p>Aside from the slides above I haven't seen a single example or article/post/whatever even mentioning this so maybe this isn't even suitable for my purposes. Does this sound reasonable and are similar techniques actually used?</p> <p>As of now the question is how to implement dbg_printer and really how the debug modules enumeration is supposed to work, but seeing as how I might have misunderstood everything that might change :(</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload