zlog 封装
#pragma once #include "zlog.h" class hm_zlog { public: ~hm_zlog(); protected: hm_zlog(); public: static hm_zlog *Instance(); void debug_zlog(const char *szlog, ...); void info_zlog(const char *szlog, ...); void notice_zlog(const char *szlog, ...); void error_zlog(const char *szlog, ...); void warn_zlog(const char *szlog, ...); void fatal_zlog(const char *szlog, ...); private: int init_zlog(); void destroy_zlog(); public: int m_rc; zlog_category_t *m_zlog_category; private: static hm_zlog *instance_; }; #define hm_log (* hm_zlog::Instance())
#include "hm_zlog.h" #include <iostream> #include <stdarg.h> hm_zlog *hm_zlog::instance_ = 0; hm_zlog::hm_zlog() { m_rc = 0; init_zlog(); } hm_zlog::~hm_zlog() { destroy_zlog(); } hm_zlog * hm_zlog::Instance() { if(instance_ == 0) { instance_ = new hm_zlog(); } return instance_; } void hm_zlog::debug_zlog(const char *szlog, ...) { char log[1024] = { 0 }; va_list args; va_start(args, szlog); vsprintf(log, szlog, args); va_end(args); zlog_debug(m_zlog_category, log); return ; } void hm_zlog::info_zlog(const char *szlog, ...) { char log[1024] = { 0 }; va_list args; va_start(args, szlog); vsprintf(log, szlog, args); va_end(args); zlog_info(m_zlog_category, log); return; } void hm_zlog::notice_zlog(const char *szlog, ...) { char log[1024] = { 0 }; va_list args; va_start(args, szlog); vsprintf(log, szlog, args); va_end(args); zlog_notice(m_zlog_category, log); return; } void hm_zlog::error_zlog(const char *szlog, ...) { char log[1024] = { 0 }; va_list args; va_start(args, szlog); vsprintf(log, szlog, args); va_end(args); zlog_error(m_zlog_category, log); return; } void hm_zlog::warn_zlog(const char *szlog, ...) { char log[1024] = { 0 }; va_list args; va_start(args, szlog); vsprintf(log, szlog, args); va_end(args); zlog_warn(m_zlog_category, log); return; } void hm_zlog::fatal_zlog(const char *szlog, ...) { char log[1024] = { 0 }; va_list args; va_start(args, szlog); vsprintf(log, szlog, args); va_end(args); zlog_fatal(m_zlog_category, log); return; } int hm_zlog::init_zlog() { m_rc = zlog_init("test.conf"); if(m_rc) { printf("init zlog failed.\n"); return -1; } m_zlog_category = zlog_get_category("my_cat"); if(!m_zlog_category) { printf("get zlog cat fialed.\n"); return -2; } return m_rc; } void hm_zlog::destroy_zlog() { zlog_fini(); m_rc = 0; return ; }
测试
#include <iostream> #include "hm_zlog.h" int main() { hm_log.info_zlog("hello %d。", 1);
delete hm_log;
hm_log = NULL;
std::cout << "Hello World!\n"; }
以上封装,将配置文件的名称和策略的名称写入了代码里。

浙公网安备 33010602011771号