zlog日志库的简单封装,以及给debug级别添加颜色显示

现看看效果如何:

 

方法如下:

 

 定义相关颜色的宏 

 1 #define ESC_START       "\033["
 2 #define ESC_END         "\033[0m"
 3 #define COLOR_FATAL     "31;40;5m"
 4 #define COLOR_ALERT     "31;40;1m"
 5 #define COLOR_CRIT      "31;40;1m"
 6 #define COLOR_ERROR     "35;40;1m"
 7 #define COLOR_WARN      "33;40;1m"
 8 #define COLOR_NOTICE    "34;40;1m"
 9 #define COLOR_INFO      "32;40;1m"
10 #define COLOR_DEBUG     "36;40;1m"
11 #define COLOR_TRACE     "37;40;1m"

 

 

封装 zlog函数

 

 1 extern zlog_category_t * log_category;
 2 extern int log_init();
 3 extern void log_fini();
 4 #define LOG_FATAL(fmt,args...)         \
 5     zlog(log_category, __FILE__, sizeof(__FILE__)-1, \
 6     __func__, sizeof(__func__)-1, __LINE__, \
 7     ZLOG_LEVEL_FATAL, ESC_START COLOR_FATAL fmt ESC_END, ##args)
 8 
 9     
10 #define LOG_ERROR(fmt , args...)    \
11     zlog(log_category, __FILE__, sizeof(__FILE__)-1, \
12     __func__, sizeof(__func__)-1, __LINE__, \
13     ZLOG_LEVEL_ERROR, ESC_START COLOR_ERROR fmt ESC_END, ##args)
14     
15 #define LOG_WARN(fmt, args...)        \
16     zlog(log_category, __FILE__, sizeof(__FILE__)-1, \
17     __func__, sizeof(__func__)-1, __LINE__, \
18     ZLOG_LEVEL_WARN, ESC_START COLOR_WARN fmt ESC_END, ##args)
19     
20 #define LOG_NOTICE(fmt , args...)    \
21     zlog(log_category, __FILE__, sizeof(__FILE__)-1, \
22     __func__, sizeof(__func__)-1, __LINE__, \
23     ZLOG_LEVEL_NOTICE, ESC_START COLOR_NOTICE fmt ESC_END, ##args)
24     
25 #define LOG_INFO(fmt,args...)         \
26     zlog(log_category, __FILE__, sizeof(__FILE__)-1, \
27     __func__, sizeof(__func__)-1, __LINE__, \
28     ZLOG_LEVEL_INFO, ESC_START COLOR_INFO fmt ESC_END, ##args)
29     
30 #define LOG_DEBUG(fmt , args...)    \
31     zlog(log_category, __FILE__, sizeof(__FILE__)-1, \
32     __func__, sizeof(__func__)-1, __LINE__, \
33     ZLOG_LEVEL_DEBUG, ESC_START COLOR_DEBUG fmt ESC_END, ##args)

 

 

 

 封装 zlog_init,zlog_get_category,zlog_fini

 

 1 zlog_category_t * log_category = NULL;
 2 
 3 int log_init() {
 4     //初始化.配置文件名是固定的log.conf
 5     if (zlog_init("log.conf"))  {
 6         printf("Error: zlog_init\n");
 7     zlog_fini();
 8         return -1;
 9     }
10     //找到分类,在配置文件中的category
11     log_category = zlog_get_category("my_cat");
12     if (!log_category) {
13     printf("Error: get cat fail\n");
14     zlog_fini();
15     return -2;
16     }
17     return 0 ;
18 }
19 
20 
21 void log_fini() {
22     zlog_fini();
23 }

 

 

 

 

 

 

 

 

posted @ 2014-03-24 19:31  superPerfect  阅读(3688)  评论(0编辑  收藏  举报