Chinaunix首页 | 论坛 | 博客
  • 博客访问: 315111
  • 博文数量: 82
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 490
  • 用 户 组: 普通用户
  • 注册时间: 2016-06-13 10:58
文章分类

全部博文(82)

文章存档

2018年(2)

2017年(9)

2016年(71)

我的朋友

分类: C/C++

2016-10-10 17:33:46

源码:
打印的头文件, debug.h
  1. #ifndef __DEBUG_H__
  2. #define __DEBUG_H__

  3. #include <stdio.h>

  4. extern int common_printf(FILE * fp, const int level, const char * file,
  5.                   const int line, const char * func, const char * fmt, ...);

  6. /* 打印等级 0-4 */
  7. #define PRINT_LEVEL 4

  8. /* 提供4个打印接口 DEBUG, INFO, WARN, ERROR */
  9. #define LEVEL_DEBUG 4
  10. #define LEVEL_INFO 3
  11. #define LEVEL_WARN 2
  12. #define LEVEL_ERROR 1

  13. #if (PRINT_LEVEL >= LEVEL_DEBUG)
  14. #define DEBUG(fmt, ...) \
  15.     common_printf(stdout, LEVEL_DEBUG, __FILE__, __LINE__, __func__, fmt, ##__VA_ARGS__);
  16. #else
  17. #define DEBUG(fmt, ...)
  18. #endif

  19. #if (PRINT_LEVEL >= LEVEL_INFO)
  20. #define INFO(fmt, ...) \
  21.     common_printf(stdout, LEVEL_INFO, __FILE__, __LINE__, __func__, fmt, ##__VA_ARGS__);
  22. #else
  23. #define INFO(fmt, ...)
  24. #endif

  25. #if (PRINT_LEVEL >= LEVEL_WARN)
  26. #define WARN(fmt, ...) \
  27.     common_printf(stdout, LEVEL_WARN, __FILE__, __LINE__, __func__, fmt, ##__VA_ARGS__);
  28. #else
  29. #define WARN(fmt, ...)
  30. #endif

  31. #if (PRINT_LEVEL >= LEVEL_ERROR)
  32. #define ERROR(fmt, ...) \
  33.     common_printf(stdout, LEVEL_ERROR, __FILE__, __LINE__, __func__, fmt, ##__VA_ARGS__);
  34. #else
  35. #define ERROR(fmt, ...)
  36. #endif

  37. #endif

打印的源文件,debug.c
  1. #include <stdio.h>
  2. #include <sys/time.h>
  3. #include <time.h>
  4. #include <stdarg.h>
  5. #include "debug.h"

  6. #define ANSI_COLOR_RED "\x1b[1;31m"
  7. #define ANSI_COLOR_GREEN "\x1b[1;32m"
  8. #define ANSI_COLOR_YELLOW "\x1b[1;33m"
  9. #define ANSI_COLOR_PURPLE      "\x1b[1;35m"
  10. #define ANSI_COLOR_CYAN      "\x1b[29m"
  11. #define ANSI_COLOR_CYAN_LESS "\x1b[29m"
  12. #define ANSI_COLOR_RESET      "\x1b[0m"

  13. #define PRINT_DEBUG          "DEBUG"
  14. #define PRINT_INFO               "INFO"
  15. #define PRINT_WARN               "WARN"
  16. #define PRINT_ERROR          "ERROR"

  17. #define PRINT_LEN 10240

  18. static void _common_printf(FILE * fp, const char * time_cl, const char * type_cl,
  19.                                   const char * func_cl, const char * content_cl,
  20.                                    const char * level, const char * file,
  21.                                    const int line, const char * func, const char * fmt)
  22. {
  23.     struct timeval tv;
  24.     gettimeofday(&tv, NULL);
  25.     char datestr[20];
  26.     struct tm tm;
  27.     time_t timesec = tv.tv_sec;
  28.     localtime_r(&timesec, &tm);
  29.     strftime(datestr, sizeof(datestr), "%Y-%m-%d %H:%M:%S", &tm);
  30.     fprintf(fp, "%s" "%s.%06ld " ANSI_COLOR_RESET
  31.                 "%s" "[%-05s]" ANSI_COLOR_RESET
  32.                 "%s" "[%s, %4d][%-10s] " ANSI_COLOR_RESET
  33.                 "%s" "%s" ANSI_COLOR_RESET,
  34.                 time_cl, datestr, tv.tv_usec, type_cl,
  35.                 level, func_cl, file, line, func, content_cl, fmt);
  36.     fflush(fp);
  37. }

  38. int common_printf(FILE * fp, const int level, const char * file,
  39.                          const int line, const char * func, const char * fmt, ...)
  40. {
  41.     int i;
  42.     char buf[PRINT_LEN];
  43.     
  44.     va_list args;    
  45.     va_start(args, fmt);
  46.     i = vsnprintf(buf, PRINT_LEN, fmt, args);
  47.     va_end(args);

  48.     switch (level) {
  49.         case LEVEL_DEBUG:
  50.                 _common_printf(fp, ANSI_COLOR_CYAN_LESS, ANSI_COLOR_GREEN,
  51.                            ANSI_COLOR_CYAN, ANSI_COLOR_GREEN,
  52.                            PRINT_DEBUG, file, line, func, buf);
  53.                 break;                          
  54.                           
  55.         case LEVEL_INFO:
  56.                 _common_printf(fp, ANSI_COLOR_CYAN_LESS, ANSI_COLOR_YELLOW,
  57.                            ANSI_COLOR_CYAN, ANSI_COLOR_YELLOW,
  58.                            PRINT_INFO, file, line, func, buf);
  59.                 break;
  60.                 
  61.         case LEVEL_WARN:
  62.                 _common_printf(fp, ANSI_COLOR_CYAN_LESS, ANSI_COLOR_PURPLE,
  63.                            ANSI_COLOR_CYAN, ANSI_COLOR_PURPLE,
  64.                            PRINT_WARN, file, line, func, buf);
  65.                 break;          
  66.         case LEVEL_ERROR:
  67.                 _common_printf(fp, ANSI_COLOR_CYAN_LESS, ANSI_COLOR_RED,
  68.                            ANSI_COLOR_CYAN, ANSI_COLOR_RED,
  69.                            PRINT_ERROR, file, line, func, buf);        
  70.                 break;
  71.         default:
  72.                 break;
  73.     }    

  74.     return i;
  75. }
测试程序,test.c
  1. #include "debug.h"

  2. int main(int argc, char *argv[]) 
  3. {
  4.     int i ;
  5.     i = DEBUG("hahaha\n");
  6.     printf("==> buf_len [%d]\n", i);
  7.     INFO("hahaha\n");
  8.     WARN("hahaha\n");
  9.     ERROR("hahaha\n");
  10.     return 0;
  11. }
结果:
  1. [root@localhost test]# gcc -o main test.c debug.c
  2. [root@localhost test]# ./main
  3. 2017-04-20 11:31:08.307846 [DEBUG][test.c,  6][main ] hahaha
  4. ==> buf_len [7]
  5. 2017-04-20 11:31:08.308034 [INFO ][test.c,  8][main ] hahaha
  6. 2017-04-20 11:31:08.308044 [WARN ][test.c,  9][main ] hahaha
  7. 2017-04-20 11:31:08.308061 [ERROR][test.c, 10][main ] hahaha
阅读(1876) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~