wrapper printf function that filters according to user preferences

前端 未结 3 837
死守一世寂寞
死守一世寂寞 2021-02-04 01:42

My program writes to a log and to stdout. Every message, however, has a certain priority and the user specifies in Preferences which priorities go to which stream (log or stdout

3条回答
  •  太阳男子
    2021-02-04 02:47

    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)
    

提交回复
热议问题