在linux下,可以使用一些宏,加上自定义格式输出,让输出更易于调试:
排版出来可能有些乱,注意do{ }while(0);是在一行里就可以了。
- #include <stdio.h>
- #include <stdlib.h>
- #define DEBUG1( fmt, arg ... ) \
- do{printf("[DEBUG] " fmt , ##arg ); }while(0);
- #define DEBUG2( fmt, arg ... ) \
- do{printf("[%s: %s: line %d]" fmt ,\
- __FILE__, __FUNCTION__, __LINE__, ##arg ); }while(0);
- #define PrintColor1( fmt, arg ... ) \
- do{printf("\033[30m""[%s: %s: line %d]" fmt"\033[0m" ,\
- __FILE__, __FUNCTION__, __LINE__, ##arg ); }while(0);
- #define PrintColor2( fmt, arg ... ) \
- do{printf("\033[31m""[%s: %s: line %d]" fmt"\033[0m" ,\
- __FILE__, __FUNCTION__, __LINE__, ##arg ); }while(0);
- #define PrintColor3( fmt, arg ... ) \
- do{printf("\033[32m""[%s: %s: line %d]" fmt"\033[0m" ,\
- __FILE__, __FUNCTION__, __LINE__, ##arg ); }while(0);
- #define PrintColor4( fmt, arg ... ) \
- do{printf("\033[33m""[%s: %s: line %d]" fmt"\033[0m" ,\
- __FILE__, __FUNCTION__, __LINE__, ##arg ); }while(0);
- #define PrintColor5( fmt, arg ... ) \
- do{printf("\033[34m""[%s: %s: line %d]" fmt"\033[0m" ,\
- __FILE__, __FUNCTION__, __LINE__, ##arg ); }while(0);
- #define PrintColor6( fmt, arg ... ) \
- do{printf("\033[35m""[%s: %s: line %d]" fmt"\033[0m" ,\
- __FILE__, __FUNCTION__, __LINE__, ##arg ); }while(0);
- #define PrintColor7( fmt, arg ... ) \
- do{printf("\033[36m""[%s: %s: line %d]" fmt"\033[0m" ,\
- __FILE__, __FUNCTION__, __LINE__, ##arg ); }while(0);
- int main()
- {
- printf("I AM IN macro.c\n");
- DEBUG1("I AM IN macro.c\n");
- DEBUG2("I AM IN macro.c\n");
- PrintColor1("I AM IN macro.c\n");
- PrintColor2("I AM IN macro.c\n");
- PrintColor3("I AM IN macro.c\n");
- PrintColor4("I AM IN macro.c\n");
- PrintColor5("I AM IN macro.c\n");
- PrintColor6("I AM IN macro.c\n");
- PrintColor7("I AM IN macro.c\n");
- return 0;
- }
输出结果:
- http://blog.csdn.net/xiangpingli/article/details/7914133