zlog日誌庫的簡單封裝及給debug層級添加顏色顯示

來源:互聯網
上載者:User

現看看效果如何:

方法如下:

定義相關顏色的宏

#define ESC_START       "\033["#define ESC_END         "\033[0m"#define COLOR_FATAL     "31;40;5m"#define COLOR_ALERT     "31;40;1m"#define COLOR_CRIT      "31;40;1m"#define COLOR_ERROR     "35;40;1m"#define COLOR_WARN      "33;40;1m"#define COLOR_NOTICE    "34;40;1m"#define COLOR_INFO      "32;40;1m"#define COLOR_DEBUG     "36;40;1m"#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)

返回欄目頁:http://www.bianceng.cnhttp://www.bianceng.cn/Programming/extra/

封裝 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 }

作者:cnblogs superPerfect

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.