Chinaunix首页 | 论坛 | 博客
  • 博客访问: 349892
  • 博文数量: 43
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 585
  • 用 户 组: 普通用户
  • 注册时间: 2013-03-29 20:57
文章分类

全部博文(43)

文章存档

2013年(43)

我的朋友

分类: C/C++

2013-07-20 11:43:52

分享自己写了个方便打印调试的一点代码,可用于Windows和Linux平台,希望对某些朋友有用。
C语言初学者,希望大家多多指点下

点击(此处)折叠或打开

  1. /****************************************************************//**
  2.     @file debug-inl.h
  3.     @author idi0t
  4.     @brief as printf();
  5.     @time 2013-7-1
  6.     @version 1.0
  7. *********************************************************************/
  8. #ifndef _HAVE_DEBUG_H
  9. #define _HAVE_DEBUG_H

  10. #include <stdio.h>
  11. #include <string.h>
  12. #ifdef WIN32
  13.     #include <windows.h>
  14. // #define _MSC_VER 1200 //VC6.0
  15. // #define _MSC_VER 1400 //VC7.0
  16. #endif

  17. #define PRINT_LEN 2048
  18. #define HAVE_USE_COLOR 1
  19. #define HAVE_SHOW_DBG 1 /* Show filename and line */

  20. #ifdef HAVE_SHOW_DBG
  21. #define SHOW_LINE do { printf(":%d] ",__LINE__); } while(0)
  22.     #ifdef WIN32
  23.         #define SHOW_FILE do { \
  24.             printf(strrchr(__FILE__,'\\')?strrchr(__FILE__,'\\')+1:__FILE__); \
  25.         }while(0)
  26.         #if MSC_VER > 1200
  27.             #define SHOW_FUNC do { printf(__FUNCTION__); } while(0)
  28.         #else
  29.             #define SHOW_FUNC
  30.         #endif
  31.     #else
  32.         #define SHOW_FILE do { printf(strrchr(__FILE__,'/')+1); } while(0)
  33.         #define SHOW_FUNC do { printf(__FUNCTION__); } while(0)
  34.     #endif
  35.     #define SHOW_FILE_LINE do {SHOW_FILE; SHOW_LINE; } while(0)
  36. #else
  37.     #define SHOW_FILE
  38.     #define SHOW_LINE
  39.     #define SHOW_FUNC
  40.     #define SHOW_FILE_LINE
  41. #endif /* ^HAVE_SHOW_DBG */

  42. #ifdef HAVE_USE_COLOR
  43.     #ifdef WIN32
  44.         #define cRST (FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE)
  45.         #define cBLK 0
  46.         #define cRED (FOREGROUND_RED)
  47.         #define cGRN (FOREGROUND_GREEN)
  48.         #define cBLU (FOREGROUND_BLUE)
  49.         #define cYEL (FOREGROUND_RED | FOREGROUND_GREEN )
  50.         #define cMGN (FOREGROUND_RED | FOREGROUND_BLUE)
  51.         #define cCYA (FOREGROUND_GREEN | FOREGROUND_BLUE)
  52.         #define cNOR (FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY)
  53.         #define cGRA (FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE)
  54.         #define cLRD (FOREGROUND_RED | FOREGROUND_INTENSITY)
  55.         #define cLGN (FOREGROUND_GREEN | FOREGROUND_INTENSITY)
  56.         #define cLYL (FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY)
  57.         #define cLBL (FOREGROUND_BLUE | FOREGROUND_INTENSITY)
  58.         #define cPIN (FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY)
  59.         #define cLCY (FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY)
  60.         #define cBRI cRST
  61.     #else
  62.         #define cBLK "\x1b[0;30m"
  63.         #define cRED "\x1b[0;31m"
  64.         #define cGRN "\x1b[0;32m"
  65.         #define cYEL "\x1b[0;33m"
  66.         #define cBLU "\x1b[0;34m"
  67.         #define cMGN "\x1b[0;35m"
  68.         #define cCYA "\x1b[0;36m"
  69.         #define cNOR "\x1b[0;37m"
  70.         #define cGRA "\x1b[1;30m"
  71.         #define cLRD "\x1b[1;31m"
  72.         #define cLGN "\x1b[1;32m"
  73.         #define cLYL "\x1b[1;33m"
  74.         #define cLBL "\x1b[1;34m"
  75.         #define cPIN "\x1b[1;35m"
  76.         #define cLCY "\x1b[1;36m"
  77.         #define cBRI "\x1b[1;37m"
  78.         #define cRST "\x1b[0m"
  79.     #endif /* ^WIN32 */
  80. #else
  81.     #ifdef WIN32
  82.         #define cRST (FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE)
  83.         #define cBLK cRST /**/
  84.         #define cRED cRST /**/
  85.         #define cGRN cRST /* 绿 */
  86.         #define cYEL cRST /**/
  87.         #define cBLU cRST /**/
  88.         #define cMGN cRST /**/
  89.         #define cCYA cRST /* 浅蓝 */
  90.         #define cNOR cRST /* 浅灰 */
  91.         #define cGRA cRST /**/
  92.         #define cLRD cRST /* 亮红 */
  93.         #define cLGN cRST /* 亮绿 */
  94.         #define cLYL cRST /* 亮黄 */
  95.         #define cLBL cRST /* 亮蓝 */
  96.         #define cPIN cRST /* 亮紫 */
  97.         #define cLCY cRST /* 亮浅蓝 */
  98.         #define cBRI cRST /**/
  99.         

  100.     #else
  101.         #define cBLK "" /**/
  102.         #define cRED "" /**/
  103.         #define cGRN "" /* 绿 */
  104.         #define cYEL "" /**/
  105.         #define cBLU "" /**/
  106.         #define cMGN "" /**/
  107.         #define cCYA "" /* 浅蓝 */
  108.         #define cNOR "" /* 浅灰 */
  109.         #define cGRA "" /**/
  110.         #define cLRD "" /* 亮红 */
  111.         #define cLGN "" /* 亮绿 */
  112.         #define cLYL "" /* 亮黄 */
  113.         #define cLBL "" /* 亮蓝 */
  114.         #define cPIN "" /* 亮紫 */
  115.         #define cLCY "" /* 亮浅蓝 */
  116.         #define cBRI "" /**/
  117.         #define cRST ""
  118.     #endif /* ^WIN32 */
  119. #endif /* ^HAVE_USE_COLOR */

  120. #ifdef WIN32
  121.     #if _MSC_VER > 1200
  122.         #define _win_printf(clr, x...) do { \
  123.             HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE); \
  124.             SetConsoleTextAttribute(hOut, clr); \
  125.             printf(x); \
  126.             SetConsoleTextAttribute(hOut, cRST); \
  127.         } while(0)
  128.     #else
  129.         static inline void _win_printf(unsigned short clr, char *fmt, ...)
  130.         {
  131.             HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
  132.             char buf[PRINT_LEN] = {0};
  133.             va_list args;

  134.             va_start(args, fmt);
  135.             _vsnprintf(buf, sizeof(buf)-1, fmt, args);
  136.             va_end(args);

  137.             SetConsoleTextAttribute(hOut, clr);
  138.             printf("%s",buf);
  139.             SetConsoleTextAttribute(hOut, cRST);
  140.         }
  141.         static inline _abt_printf(char *fmt, ...)
  142.         {
  143.             char buf[PRINT_LEN] = {0};
  144.             va_list args;

  145.             va_start(args, fmt);
  146.             _vsnprintf(buf, sizeof(buf)-1, fmt, args);
  147.             va_end(args);
  148.             
  149.             printf("%s",buf);
  150.             abort();
  151.         }
  152.     #endif /* >MSC_VER */
  153.     #ifndef NDEBUG
  154.         #if _MSC_VER > 1200
  155.             #define clr_print _win_printf
  156.             #define abt_print(x...) do { \
  157.                     _win_printf(cLRD, x); abort(); \
  158.                 } while(0)
  159.             #define dbg_print(x...) do { \
  160.                     _win_printf(cLGN, "[DBG]:"); SHOW_FILE_LINE; printf(x); \
  161.                 }while(0)
  162.             #define inf_print(x...) do { \
  163.                     _win_printf(cLYL, "[INF]:"); SHOW_FILE_LINE; printf(x); \
  164.                 }while(0)
  165.             #define err_print(x...) do { \
  166.                     _win_printf(cLRD, "[ERR]:"); SHOW_FILE_LINE; printf(x); \
  167.                 }while(0)
  168.             #define dbg_printc(clr, x...) do { \
  169.                     _win_printf(cLGRN, "[DBG]:"); SHOW_FILE_LINE; \
  170.                     _win_printf(clr, x); \
  171.                 }while(0)
  172.             #define inf_printc(clr, x...) do { \
  173.                     _win_printf(cLYL, "[INF]:"); SHOW_FILE_LINE; \
  174.                     _win_printf(clr, x); \
  175.                 }while(0)
  176.             #define err_printc(clr, x...) do { \
  177.                     _win_printf(cLRD, "[ERR]:"); SHOW_FILE_LINE; \
  178.                     _win_printf(clr, x); \
  179.                 }while(0)
  180.         #else
  181.             #define clr_print _win_printf
  182.             #define abt_print _abt_printf
  183.             #define dbg_print _win_printf(cLGN, "[DBG]:"), printf
  184.             #define inf_print _win_printf(cLYL, "[INF]:"), printf
  185.             #define err_print _win_printf(cLRD, "[ERR]:"), printf
  186.             #define dbg_printc _win_printf(cLGN, "[DBG]:"), _win_printf
  187.             #define inf_printc _win_printf(cLYL, "[INF]:"), _win_printf
  188.             #define err_printc _win_printf(cLRD, "[ERR]:"), _win_printf
  189.         #endif /* >_MSC_VER */
  190.     #else
  191.         #if _MSC_VER > 1200
  192.             #define clr_print(x...)
  193.             #define dbg_print(x...)
  194.             #define inf_print(x...)
  195.             #define err_print(x...)
  196.             #define dbg_printc(x...)
  197.             #define inf_printc(x...)
  198.             #define err_printc(x...)
  199.         #else
  200.             #define clr_print
  201.             #define dbg_print
  202.             #define inf_print
  203.             #define err_print
  204.             #define dbg_printc
  205.             #define inf_printc
  206.             #define err_printc
  207.         #endif /* >_MSC_VER */
  208.         
  209.     #endif /* ^NDEBUG */
  210.     
  211. #else
  212. /*
  213.     static inline void _lnx_printf(int clr, char *fmt, ...)
  214.     {
  215.         char buf[PRINT_LEN] = {0};
  216.         va_list args;

  217.         va_start(args, fmt);
  218.         vsnprintf(buf, sizeof(buf)-1, fmt, args);
  219.         va_end(args);
  220.         printf("%s%s",clr,buf);
  221.         printf(cRST);
  222.     }
  223. */
  224.     #define _lnx_printf(clr, x...) do { \
  225.         printf((clr)); \
  226.         printf(x); \
  227.         printf(cRST); \
  228.     } while(0)
  229.     #ifndef NDEBUG
  230.         #define clr_print _lnx_printf
  231.         #define abt_print(x...) do { \
  232.                 _lnx_printf(cLRD, x); abort(); \
  233.             } while(0)
  234.         #define dbg_print(x...) do { \
  235.                 _lnx_printf(cLGN,"[DBG]:"); SHOW_FILE_LINE; \
  236.                 printf(x); \
  237.             }while(0)
  238.         #define inf_print(x...) do { \
  239.                 _lnx_printf(cLYL,"[INF]:"); SHOW_FILE_LINE; \
  240.                 printf(x); \
  241.             }while(0)
  242.         #define err_print(x...) do { \
  243.                 _lnx_printf(cLRD,"[ERR]:"); SHOW_FILE_LINE; \
  244.                 printf(x); \
  245.             }while(0)
  246.         #define dbg_printc(clr,x...) do { \
  247.                 _lnx_printf(cLGN,"[DBG]:"); SHOW_FILE_LINE; \
  248.                 _lnx_printf(clr,x); \
  249.             }while(0)
  250.         #define inf_printc(clr,x...) do { \
  251.                 _lnx_printf(cLYL,"[INF]:"); SHOW_FILE_LINE; \
  252.                 _lnx_printf(clr,x); \
  253.             }while(0)
  254.         #define err_printc(clr,x...) do { \
  255.                 _lnx_printf(cLRD,"[ERR]:"); SHOW_FILE_LINE; \
  256.                 _lnx_printf(clr,x); \
  257.             }while(0)
  258.     #else
  259.         #define clr_print(x...)
  260.         #define dbg_print(x...)
  261.         #define inf_print(x...)
  262.         #define err_print(x...)
  263.         #define dbg_printc(x...)
  264.         #define inf_printc(x...)
  265.         #define err_printc(x...)
  266.     #endif /* ^NDEBUG */
  267. #endif /* ^WIN32 */

  268. #ifndef NDEBUG
  269. /* //for test
  270.     static inline void debug_test()
  271.     {
  272.         int i=0;
  273.         err_print("hello world\n");
  274.         dbg_print("hello world\n");
  275.         inf_print("hello world\n");
  276.         err_printc(cLRD,"hello world\n");
  277.         dbg_printc(cLGN,"hello world\n");
  278.         inf_printc(cLYL,"hello world\n");

  279.         inf_printc(cRED,"cRED hello world\n");
  280.         inf_printc(cGRN,"cGRN hello world\n");
  281.         inf_printc(cYEL,"cYEL hello world\n");
  282.         inf_printc(cBLU,"cBLU hello world\n");
  283.         inf_printc(cMGN,"cMGN hello world\n");
  284.         inf_printc(cCYA,"cCYA hello world\n");
  285.         inf_printc(cNOR,"cNOR hello world\n");
  286.         inf_printc(cGRA,"cGRA hello world\n");
  287.         inf_printc(cLRD,"cLRD hello world\n");
  288.         inf_printc(cLGN,"cLGN hello world\n");
  289.         inf_printc(cLYL,"cLYL hello world\n");
  290.         inf_printc(cLBL,"cLBL hello world\n");
  291.         inf_printc(cPIN,"cPIN hello world\n");
  292.         inf_printc(cLCY,"cLCY hello world\n");
  293.         inf_printc(cBRI,"cBRI hello world\n");
  294.     }
  295. */
  296. #endif
  297. #endif /* ! _HAVE_DEBUG_H */

阅读(2403) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~