Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 663289
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T23:28:24+00:00 2026-05-13T23:28:24+00:00

I set out to get myself a neat C debugging macro, not really sure

  • 0

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.

What I’ve been using in the past have been something like this:

#ifdef DEBUG
 #define DBG(x) printf x
#else
 #define DBG(x) /* nothing */
#endif

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.

The best example I found was from some slides from an advanced c course, which can be found here:
http://www.mpi-inf.mpg.de/departments/rg1/teaching/advancedc-ws08/script/lecture07.pdf
(the relevant parts are slide 19-23, but most of it is included below)

Being lecture slides they unfortunately need some explaining to go. But they mention something that seems quite useful:

DBG((MOD_PARSER , "z = %d\n", z));

Where MOD_PARSER is a debug module/category and the rest of the arguments are meant to be given to printf.

And the implementation of DBG:

#ifdef PRGDEBUG
 #define DBG(x) dbg_printer x
#else
 #define DBG(x) /* nothing */
#endif
void dbg_printer(int module , const char *fmt, ...);

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.

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…

*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 see why you would left-shift the values of the enumeration elements, some nifty trick I’m missing?

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?

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 🙁

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T23:28:24+00:00Added an answer on May 13, 2026 at 11:28 pm

    To pass the variable number of arguments from dbg_printer, you call vfprintf instead of fprintf. Something like this:

    #include <stdarg.h>
    
    extern int dbg_mod_stderr;
    extern int dbg_mod_file;
    extern FILE *dbg_file;
    
    void dbg_printer(int module , const char *fmt, ...)
    {
        if (dbg_mod_stderr & module)
        {
            va_list ap;
    
            va_start(ap, fmt);
            vfprintf(stderr, fmt, ap);
            va_end(ap);
        }
    
        if (dbg_file != NULL && (dbg_mod_file & module))
        {
            va_list ap;
    
            va_start(ap, fmt);
            vfprintf(dbg_file, fmt, ap);
            va_end(ap);
        }
    }
    

    (This example show how to allow one set of debugging messages to go to stderr, and a different set to a debug file)

    The left-shifting of the constants allows you to have any subset of modules selected, not just a single one. For example, with the above code, you would turn on debugging of the parser and solver to stderr like so:

    dbg_mod_stderr |= MOD_PARSER;
    dbg_mod_stderr |= MOD_SOLVER;
    

    and you could later turn off debugging of the solver like this:

    dbg_mod_stderr &= ~MOD_SOLVER;
    

    (This works because each MOD_ constant has only a single, unique bit set in the binary representation).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.