日志库EasyLogging++学习系列(6)—— 日志记录器

转载自:

  http://blog.csdn.net/Fish_55_66/article/details/49468161

 

所有的日志都是由日志记录器完成的,日志记录器使用唯一的 ID(大小写敏感)来标识。在 Easylogging++ 中默认了三个现有的日志记录器:

 

  • 默认日志记录器,其 ID 为:default
  • 性能日志记录器,其 ID 为:performance
  • 系统日志记录器,其 ID 为:syslog,需要定义宏 ELPP_SYSLOG ,否则不存在
 

注册日志记录器

 

除了上述三个默认现有的日志记录器,我们还可以注册新的日志记录器,这个在前面的《日志库EasyLogging++学习系列(3)—— 配置功能》一文中就提到了一种方法,就是使用全局配置文件来注册。
 
现在介绍另外一种更通用的方法,就是使用 el::Loggers::getLogger()函数。下面把这个函数从源码中摘录出来:
[cpp] view plain copy
 
  1. static inline Logger* getLogger(const std::string& identity, bool registerIfNotAvailable = true) {  
  2.         base::threading::ScopedLock scopedLock(ELPP->lock());  
  3.         return ELPP->registeredLoggers()->get(identity, registerIfNotAvailable);  
  4.     }  
函数参数说明:
  •  identity,日志记录器 ID 标识,注意大小写敏感。
  • registerIfNotAvailable,当指定 ID 的日志记录器不存在时,值为true则新建一个指定 ID 的日志记录器,并返回它的一个对象指针,值为false则返回 NULL;当指定 ID 的日志记录器已经存在时,不论该参数为何值,都返回它的一个对象指针。
所有新建的日志记录器都是使用通过默认配置功能设置好的配置,同样是详见《日志库EasyLogging++学习系列(3)—— 配置功能》一文中的默认配置功能介绍。其实,每一个日志记录器的配置都可以是不同的,只需利用函数  el::Loggers::reconfigureLogger()重新设置即可。
 
注销日志记录器
 
你可以注销除了默认日志记录器(ID:default)以外所有的日志记录器。不过仅仅建议你选择注销由你自己注册的并且不再使用的日志记录器,否则可能会出现意料之外的错误。你可以使用函数el::Loggers::unregisterLogger()注销某一个日志记录器。下面是这个函数的源码:
[cpp] view plain copy
 
  1. static inline bool unregisterLogger(const std::string& identity) {  
  2.         base::threading::ScopedLock scopedLock(ELPP->lock());  
  3.         return ELPP->registeredLoggers()->remove(identity);  
  4.     }  
在 Easylogging++ V9.80版本中,这个函数有个错误,最终会导致程序崩溃。原因很简单,就是有个指针对象释放之后仍被使用,下面是出错的源码:
[cpp] view plain copy
 
  1. inline void unregister(const T_Key& uniqKey) {  
  2.         T_Ptr* existing = get(uniqKey);  
  3.         if (existing != nullptr) {  
  4.             base::utils::safeDelete(existing);  
  5.             this->list().erase(uniqKey);  
  6.         }  
  7.     }  
第四行的 existing 和第五行的 uniqKey 其实是指向同一个对象的,先 delete 后 erase 导致出错。改正的方法也很简单,只需替换两行代码顺序即可。
 
枚举日志记录器
 
你可以使用函数 el::Loggers::populateAllLoggerIds(std::vector<std::string>&)枚举日志库中所有的日志记录器。下面的代码演示了注册、注销、枚举日志记录器等功能:
[cpp] view plain copy
 
  1. #define ELPP_STL_LOGGING    
  2. #include "easylogging++.h"  
  3.   
  4. INITIALIZE_EASYLOGGINGPP  
  5.   
  6. int main(int argc, char** argv)  
  7. {  
  8.     /// 使用一个不存在的日记记录器会输出一条错误信息  
  9.     CLOG(INFO, "testlog");  
  10.   
  11.     /// 注册一个新的日志记录器  
  12.     el::Logger* newLogger = el::Loggers::getLogger("testlog");  
  13.     CLOG(INFO, "testlog") << "this is a new logger";  
  14.   
  15.     /// 枚举日志记录器,没有定义宏 ELPP_SYSLOG,不会枚举出系统日志记录器  
  16.     std::vector<std::string> allLoggers;  
  17.     el::Loggers::populateAllLoggerIds(&allLoggers);  
  18.     LOG(INFO) << allLoggers;  
  19.   
  20.     /// 注销日志记录器  
  21.     el::Loggers::unregisterLogger("testlog");  
  22.     /// 使用一个不存在的日记记录器会输出一条错误信息  
  23.     CLOG(INFO, "testlog");  
  24.   
  25.     /// 枚举日志记录器  
  26.     el::Loggers::populateAllLoggerIds(&allLoggers);  
  27.     LOG(INFO) << allLoggers;  
  28.   
  29.     system("pause");  
  30.     return 0;  
  31. }  
 

 

 
 
posted @ 2017-10-07 16:43  博客园—哆啦A梦  阅读(639)  评论(0)    收藏  举报