带leve 信息输出
直接上代码
#include <stdio.h> #include <stdarg.h> #define Error 0 #define Warning 1 #define Debug 2 #define Info 3 #define NOW Debug int gErrorMsg(char* fmt,...){ va_list vl; va_start(vl,fmt); if(NOW <= Error){ printf("[Error]"); vfprintf( stdout, fmt, vl ); printf("\n"); } va_end(vl); } int gWarningMsg(char* fmt,...){ va_list vl; va_start(vl,fmt); if(NOW <= Warning){ printf("[Warning]"); vfprintf( stdout, fmt, vl ); printf("\n"); } va_end(vl); } int gDebugMsg(char* fmt,...){ va_list vl; va_start(vl,fmt); if(NOW <= Debug){ printf("[Debug]"); vfprintf( stdout, fmt, vl ); printf("\n"); } va_end(vl); } int gInfoMsg(char* fmt,...){ va_list vl; va_start(vl,fmt); if(NOW <= Info){ printf("[Info]"); vfprintf( stdout, fmt, vl ); printf("\n"); } va_end(vl); } int main(){ gErrorMsg("%s", "Hello eMessage!"); gWarningMsg("%s", "Hello eMessage!"); gDebugMsg("%s", "Hello eMessage!"); gInfoMsg("%s", "Hello eMessage!"); return 0; }
如此 消息级别可控 其只会输出 >= 目标级别 的信息 而且目标级别可调 而不需改动代码
运行结果:
Administrator@g-laptop /cygdrive/e/cprogramming/cpp/gMessage
$ ./test
[Debug]Hello eMessage!
[Info]Hello eMessage!