多等级debug输出的简单例子(c编译器: gcc(支持C99标准))
2012-07-20 16:58
363 查看
利用C99标准支持的__VA_ARGS__宏 (C99 宏支持取参数 #define Macro(...) __VA_ARGS__ 使用宏的时候,参数如果不写,宏里用 #,## 这样的东西会扩展成空串。(以前会出错的)),做的带多等级debug输出的简单例子,便于程序开发!
#include <stdio.h>
#include <stdarg.h>
#define BUFFER_SIZE 1024
#define DEBUG_LEVEL 4
typedef enum{
DEBUG_LEVEL_ERROR=0,
DEBUG_LEVEL_WARNING,
DEBUG_LEVEL_NOTICE,
DEBUG_LEVEL_INFO,
DEBUG_LEVEL_DEBUG
}ErrorLevel;
#define transDebug(level) \
do{ \
switch(level) \
{ \
case 0: \
printf("Error: "); \
break; \
case 1: \
printf("Warning: "); \
break; \
case 2: \
printf("Notice: "); \
break; \
case 3: \
printf("Information: "); \
break; \
case 4: \
printf("Debug: "); \
break; \
default: \
printf("Unknown Level: "); \
} \
}while(0)
#define debugPrint(debugLevel,format,...) \
do \
{ \
if(debugLevel<DEBUG_LEVEL) \
{ \
printf("%s(line:%d): ",__FILE__,__LINE__,debugLevel); \
transDebug(debugLevel); \
printf(format,##__VA_ARGS__); \
} \
}while(0)
int main(int argc,char **argv)
{
int string[3];
const char *hello="hello the world";
int num=5+3;
debugPrint(5,"hello the world\n");
//debugPrint(2); error
debugPrint(1,"hello:%s\n",hello);
debugPrint(3,"%s--->%d\n",hello,sizeof(string)/sizeof(int));
return 0;
}
结果:
lin@lin-host:~$ gcc test.c
lin@lin-host:~$ ./a.out
test.c(line:57): Warning: hello:hello the world
test.c(line:58): Information: hello the world--->3
#include <stdio.h>
#include <stdarg.h>
#define BUFFER_SIZE 1024
#define DEBUG_LEVEL 4
typedef enum{
DEBUG_LEVEL_ERROR=0,
DEBUG_LEVEL_WARNING,
DEBUG_LEVEL_NOTICE,
DEBUG_LEVEL_INFO,
DEBUG_LEVEL_DEBUG
}ErrorLevel;
#define transDebug(level) \
do{ \
switch(level) \
{ \
case 0: \
printf("Error: "); \
break; \
case 1: \
printf("Warning: "); \
break; \
case 2: \
printf("Notice: "); \
break; \
case 3: \
printf("Information: "); \
break; \
case 4: \
printf("Debug: "); \
break; \
default: \
printf("Unknown Level: "); \
} \
}while(0)
#define debugPrint(debugLevel,format,...) \
do \
{ \
if(debugLevel<DEBUG_LEVEL) \
{ \
printf("%s(line:%d): ",__FILE__,__LINE__,debugLevel); \
transDebug(debugLevel); \
printf(format,##__VA_ARGS__); \
} \
}while(0)
int main(int argc,char **argv)
{
int string[3];
const char *hello="hello the world";
int num=5+3;
debugPrint(5,"hello the world\n");
//debugPrint(2); error
debugPrint(1,"hello:%s\n",hello);
debugPrint(3,"%s--->%d\n",hello,sizeof(string)/sizeof(int));
return 0;
}
结果:
lin@lin-host:~$ gcc test.c
lin@lin-host:~$ ./a.out
test.c(line:57): Warning: hello:hello the world
test.c(line:58): Information: hello the world--->3
相关文章推荐
- 1.编写一个简单的C语言程序:根据输入的两个整数求平均值并且在终端输出,通过gcc编译器得到它的汇编程序文件。 2.编写一个C语言程序:打印输出所有“水仙花数”,用gdb调试程序(给出步骤)。所谓“
- 1.编写一个简单的C语言程序:根据输入的两个整数求平均值并且在终端输出,通过gcc编译器得到它的汇编程序文件。 2.编写一个C语言程序:打印输出所有“水仙花数”,用gdb调试程序(给出步骤)。所谓“
- 通过一个简单的例子学习编译器是如何执行程序的
- 将十进制整形数转换成二进制,然后通过字符型输出 自己实现的一个简单的例子
- [C] 让VC支持C99的整数类型V1.01。避免包含目录问题,更名auto_stdint.h、auto_inttypes.h(在VC6至VC2012、GCC、BCB等编译器下测试通过)
- MinGW/GCC 编译器修改 gettext 初始化路径使之能在任意位置输出中文消息
- php 无限级分类,超级简单的无限级分类,支持输出树状图
- PHP循环输出指定目录下的所有文件和文件夹路径例子(简单实用)
- linux下用宏代替printf perror,实现日志输出(gcc编译器适用)
- iText输出PDF翻页控制简单例子
- 支持各子线程输出提示信息的toast例子
- 编译器工具 Flex Bison for Windows 简单入门例子
- Delphi使窗口支持文件拖放的简单例子,附源代码
- linux环境下 gcc 编译器的简单使用
- iText输出PDF翻页控制简单例子
- Scanner类的简单用法(这里举个读取控制台输入的内容,然后输出到控制台的简单例子)
- shell 不同颜色输出结果的简单例子
- ARM学习笔记002之gcc-4.3.2编译器不支持硬件除法运算的解决方法
- iis不支持aspx“编译器错误消息: CS0016: 未能写入输出文件”的解决办法
- iText输出PDF翻页控制简单例子