wrapper printf function that filters according to user preferences

If you want the PRIO_* definitions to be used as bit (using | and &), you must give each one of them its own bit:

unsigned short PRIO_HIGH = 0x0001;
unsigned short PRIO_NORMAL = 0x0002;
unsigned short PRIO_LOW = 0x0004;

The macro can be improved by using the do { } while (0) notation. It makes the calls to write_log act more like a statement.

#define write_log(priority,format,args...) do { \
    if (priority & PRIO_LOG) { \
        printf(format, ## args); \
    } while(0)

You want to call vprintf() instead of printf() using the variable arguments "varargs" capabilities of C.

#include <stdarg.h>

int write_log(int priority, const char *format, ...)
{
    va_list args;
    va_start(args, format);

    if(priority & PRIO_LOG)
            vprintf(format, args);

    va_end(args);
}

For more information, see something along the lines of this.


I think Jeff's idea is the way to go, but you can also accomplish this with a macro without using vprintf. This might require gcc:

#define write_log(priority,format,args...)        \
                  if (priority & PRIO_LOG) {      \ 
                      printf(format, ## args);    \
                  }

Check here for info about how this works.