实现方法1:
#include <stdio.h>
unsigned int debugflags = 1; //这里设计debug值,我默认全部输出
#define mprintf(level,args...) \
do { if((debugflags & level)) printf(args);} while(0);
//或者使用此方式
#define mprintf(level,arg...) \
do { \
if(debugflags >= level) \
printf(args); \
}while(0);
int main(void)
{
int level = 1;
mprintf(level,"this is a test %d\n",1);
return 0;
}
程序输出结果: this a test 1
实现方法2:
#include <stdio.h>
#include <stdarg.h>
void mprintf(int flags,const char *fmt,...)
{
if(!flags){
return ;
}
va_list args;
va_start(args,fmt);
printf(fmt,args);
va_end(args);
}
int main(void)
{
int flags = 1;
mprintf(flags,"here is test %d\n",1);
return 0;
}
具体debug等级相关细节就不写了