织梦CMS - 轻松建站从此开始!

罗索

一个debug的trace模块

落鹤生 发布于 2012-05-07 09:33 点击:次 
写程序的过程中,debug是不可避免的,printf可以说是最常用的debug方法(当然还有其它...),不过若是程序里面printf满天飞的话,将是一件非常头疼的事情,最后发布时printf怎么处理?我在实际的工作中做了一套trace模块,便于调试,与大家分享,也希望大家一起来补充完
TAG:

写程序的过程中,debug是不可避免的,printf可以说是最常用的debug方法(当然还有其它...),不过若是程序里面printf满天飞的 话,将是一件非常头疼的事情,最后发布时printf怎么处理?我在实际的工作中做了一套trace模块,便于调试,与大家分享,也希望大家一起来补充完 善它。


使用方法就不说了,看源码。

  1. #ifndef __CS_TRACE_H__ 
  2. #define __CS_TRACE_H__ 
  3.  
  4. #if defined(__cplusplus) 
  5. extern "C" { 
  6. #endif 
  7.  
  8.  
  9. /* colors used in console */ 
  10. #define CLR_BLACK       0 
  11. #define CLR_RED         1 
  12. #define CLR_GREEN       2 
  13. #define CLR_MAGENTA     3 
  14. #define CLR_BLUE        4 
  15. #define CLR_PINK        5 
  16. #define CLR_CYAN        6 
  17. #define CLR_WHITE       7 
  18. #define CLR_NONE        8 
  19. #define CLR_DEFAULT     8 
  20.  
  21. /* verbose trace levels */ 
  22. #define DBG_NONE        0 
  23. #define DBG_MESSAGE     1 
  24. #define DBG_FATAL       2 
  25. #define DBG_ERROR       3 
  26. #define DBG_WARNNING    4 
  27. #define DBG_NOTICE      5 
  28. #define DBG_INFO        6 
  29. #define DBG_DEBUG       7 
  30. #define DBG_LOG         8 
  31. #define DBG_VERBOSE     9 
  32. #define DBG_MIN         DBG_NONE 
  33. #define DBG_MAX         DBG_VERBOSE 
  34.  
  35. #define CS_TRACE_DFT    0   /* default solution */ 
  36. #define CS_TRACE_FTP    1   /* ftp solution */ 
  37.  
  38. #define CS_LOG_FILE     "/tmp/cs_log.txt"   /* log file */ 
  39.  
  40. /* define debug functions */ 
  41. #define cs_message(format, arg...)  cs_printf( CS_TRACE_DFT, DBG_MESSAGE, format, ##arg ) 
  42. #define cs_fatal(format, arg...)    cs_printf( CS_TRACE_DFT, DBG_FATAL, format, ##arg ) 
  43. #define cs_error(format, arg...)    cs_printf( CS_TRACE_DFT, DBG_ERROR, format, ##arg ) 
  44. #define cs_warning(format, arg...)  cs_printf( CS_TRACE_DFT, DBG_WARNING, format, ##arg )  
  45. #define cs_notice(format, arg...)   cs_printf( CS_TRACE_DFT, DBG_NOTICE, format, ##arg ) 
  46. #define cs_info(format, arg...)     cs_printf( CS_TRACE_DFT, DBG_INFO, format, ##arg ) 
  47. #define cs_debug(format, arg...)    cs_printf( CS_TRACE_DFT, DBG_DEBUG, format, ##arg ) 
  48. #define cs_log(format, arg...)      cs_printf( CS_TRACE_DFT, DBG_LOG, format, ##arg ) 
  49. #define cs_verbose(format, arg...)  cs_printf( CS_TRACE_DFT, DBG_VERBOSE, format, ##arg ) 
  50. #define cs_logf(format, arg...)     cs_logfile( CS_TRACE_DFT, DBG_LOG, format, ##arg ) 
  51.  
  52. int cs_init(char *magic, int debug, int log, int fg, int bg, char *fname); 
  53.  
  54.  
  55. #if defined(__cplusplus) 
  56. #endif 
  57.  
  58. #endif /* __CS_TRACE_H__ */ 

源码文件trace.c:

  1. #include <stdarg.h> 
  2. #include <stdio.h> 
  3. #include <errno.h> 
  4. #include <time.h> 
  5. #include <string.h> 
  6. #include <unistd.h> 
  7. #include <sys/types.h> 
  8. #include <sys/ipc.h> 
  9. #include <sys/msg.h> 
  10. #include <trace.h> 
  11.  
  12. typedef struct { 
  13.     char magic[3];  /*!< magic num */ 
  14.     int  dlevel;    /*!< debug level */ 
  15.     int  llevel;    /*!< log level */ 
  16.     int  fg;        /*!< foreground color */ 
  17.     int  bg;        /*!< background color */ 
  18. }cs_unit; 
  19.  
  20. /*! initial valstplication's display solution */ 
  21. cs_unit _cs_solution[] = { 
  22.     { "csn", DBG_VERBOSE, DBG_INFO, CLR_WHITE, CLR_RED },       /* default solution */ 
  23.     { "ftp", DBG_VERBOSE, DBG_VERBOSE, CLR_WHITE, CLR_BLACK },  /* ftp solution */ 
  24. }; 
  25.  
  26. static char _logfile[1024]; 
  27. static char _magic[3]   = "_"
  28. static int  _fgcolor    = CLR_CYAN + 30; 
  29. static int  _bgcolor    = CLR_BLUE + 40; 
  30. static int  _dlevel     = DBG_MAX; 
  31. static int  _llevel     = DBG_MIN; 
  32. static int  _cs_inited  = 0; 
  33. static int  _solulen    = sizeof(_cs_solution)/sizeof(_cs_solution[0]); 
  34.  
  35. /*********************************************************** 
  36. *Description: 
  37. *   it is used to get current time and save it in a buffer. 
  38. * 
  39. *Parameters: 
  40. *   buf:    allocated buffer. 
  41. * 
  42. *Returns: 
  43. *   return the current time in string format. 
  44. * 
  45. ***************************************************/ 
  46. char *cs_strtime(char *buf) 
  47. struct tm ltm; 
  48. time_t t; 
  49.  
  50.     t = time(NULL); 
  51.     bzero(&ltm, sizeof(ltm)); 
  52.     localtime_r(&t, &ltm); 
  53.     sprintf(buf, "%02d:%02d:%02d", ltm.tm_hour, ltm.tm_min, ltm.tm_sec); 
  54.  
  55.     return buf; 
  56.  
  57.  
  58.  
  59.  
  60. /************************************************ 
  61. *Description: 
  62. *   it is used to init the debug system. 
  63. * 
  64. *Parameters: 
  65. *   magic:  solution's string id(3 char at most) 
  66. *   debug:  debug level 
  67. *   log:    log level 
  68. *   fg:     forground color 
  69. *   bg:     background color 
  70. * 
  71. *Returns: 
  72. *   return 0 if success, 1 if failed. 
  73. * 
  74. ******************************************/ 
  75. int cs_init(char *magic, int debug, int log, int fg, int bg, char *fname) 
  76.     _magic[0]   = magic[0];  
  77.     _magic[1]   = magic[1];  
  78.     _magic[2]   = magic[2];  
  79.     _magic[3]   = '\0'
  80.     _fgcolor    = 30 + fg;  
  81.     _bgcolor    = 40 + bg; 
  82.     _dlevel     = debug; 
  83.     _llevel     = log; 
  84.     _cs_inited  = 1; 
  85.     strcpy(_logfile, fname); 
  86.  
  87.     return 0; 
  88. }
  89.  
  90. /******************************************** 
  91. *Description: 
  92. *   it is used to output debug information to console. 
  93. * 
  94. *Parameters: 
  95. *   index:  solution index. 
  96. *   level:  current level 
  97. *           level < cur_debug_level, output it on console. 
  98. *   format: output format. 
  99. * 
  100. *Returns: 
  101. *   return 0 if success, 1 if failed. 
  102. * 
  103. ********************************************/ 
  104. int cs_printf(int index, int level, char *format, ...)  
  105. int fg      = 0; 
  106. int bg      = 0; 
  107. int dlevel  = 0;  
  108. int llevel  = 0; 
  109. char *fname = 0; 
  110. char *ps    = 0; 
  111. char *ts    = 0; 
  112. char *magic = 0; 
  113. char msgbuf[1024]; 
  114. char timbuf[32]; 
  115. va_list valst; 
  116.  
  117.     ts = cs_strtime(timbuf); 
  118.     ps = msgbuf; 
  119.     fname = CS_LOG_FILE; 
  120.     if ( strlen(_logfile) ) { 
  121.         fname = _logfile; 
  122.     } 
  123.  
  124.     cs_unit *pm = _cs_solution; 
  125.     if(index) { 
  126.         if(index < _solulen) { 
  127.             pm += index; 
  128.         } 
  129.         fg      = pm->fg + 30; 
  130.         bg      = pm->bg + 40; 
  131.         magic   = pm->magic; 
  132.         dlevel  = pm->dlevel; 
  133.         llevel  = pm->llevel; 
  134.     } else { 
  135.         if ( _cs_inited ) { 
  136.             fg      = _fgcolor;  
  137.             bg      = _bgcolor;  
  138.             magic   = _magic; 
  139.             dlevel  = _dlevel;  
  140.             llevel  = _llevel; 
  141.         } else { 
  142.             fg      = pm->fg + 30; 
  143.             bg      = pm->bg + 40; 
  144.             magic   = pm->magic; 
  145.             dlevel  = pm->dlevel; 
  146.             llevel  = pm->llevel; 
  147.         } 
  148.     } 
  149.  
  150.     va_start( valst, format ); 
  151.  
  152.     // Conosle 
  153.     if( level <= dlevel ) { 
  154.         fprintf(stderr, "[%d;%dm[%s|%s] ", fg, bg, magic, ts); 
  155.         vfprintf(stderr, format, valst); 
  156.     } 
  157.  
  158.     // Logfile 
  159.     if(level <= llevel) { 
  160.         if(level) { ps += sprintf(ps, "[%s|%s] ", magic, ts); } 
  161.         vsprintf(ps, format, valst); 
  162.          
  163.         FILE *fp = fopen(fname, "at"); 
  164.         if(fp) { 
  165.             int len = strlen(ps); 
  166.             fwrite(ps, 1, len, fp); 
  167.             fclose(fp); 
  168.         } 
  169.     } 
  170.  
  171.     va_end(valst); 
  172.  
  173.     return 0; 
  174. }
  175.  
  176.  
  177. /********************************************** 
  178. *Description: 
  179. *   it is used to output debug information to logfile. 
  180. * 
  181. *Parameters: 
  182. *   index:  solution index. 
  183. *   level:  current level 
  184. *           level < cur_log_level, output it to logfile. 
  185. *   format: output format. 
  186. * 
  187. *Returns: 
  188. *   return 0 if success, 1 if failed. 
  189. * 
  190. **********************************************/ 
  191. int cs_logfile(int index, int level, char *format, ...)  
  192. int llevel  = 0; 
  193. char *fname = 0; 
  194. char *ps    = 0; 
  195. char msgbuf[1024]; 
  196. va_list valst; 
  197.  
  198.     ps = msgbuf; 
  199.     fname = CS_LOG_FILE; 
  200.     if ( strlen(_logfile) ) { 
  201.         fname = _logfile; 
  202.     } 
  203.  
  204.     cs_unit *pm = _cs_solution; 
  205.     if(index) { 
  206.         if(index < _solulen) { 
  207.             pm += index; 
  208.         } 
  209.         llevel  = pm->llevel; 
  210.     } else { 
  211.         if ( _cs_inited ) { 
  212.             llevel = pm->llevel; 
  213.         } else { 
  214.             llevel  = _llevel; 
  215.         } 
  216.     } 
  217.  
  218.     // Logfile 
  219.     if(level <= llevel) { 
  220.         va_start( valst, format ); 
  221.         vsprintf(ps, format, valst); 
  222.         va_end(valst); 
  223.          
  224.         FILE *fp = fopen(fname, "at"); 
  225.         if(fp) { 
  226.             int len = strlen(ps); 
  227.             fwrite(ps, 1, len, fp); 
  228.             fclose(fp); 
  229.         } 
  230.     } 
  231.  
  232.     return 0; 

 

(stavy.sun)
本站文章除注明转载外,均为本站原创或编译欢迎任何形式的转载,但请务必注明出处,尊重他人劳动,同学习共成长。转载请注明:文章转载自:罗索实验室 [http://www.rosoo.net/a/201205/15982.html]
本文出处:blog.chinaunix.net 作者:stavy.sun 原文
顶一下
(1)
100%
踩一下
(0)
0%
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:点击我更换图片
栏目列表
将本文分享到微信
织梦二维码生成器
推荐内容