所有的日志都是由日志记录器完成的,日志记录器使用唯一的 ID(大小写敏感)来标识。在 Easylogging++ 中默认了三个现有的日志记录器:
ELPP_SYSLOG
,否则不存在注册日志记录器
static inline Logger* getLogger(const std::string& identity, bool registerIfNotAvailable = true) { base::threading::ScopedLock scopedLock(ELPP->lock()); return ELPP->registeredLoggers()->get(identity, registerIfNotAvailable); }函数参数说明:
static inline bool unregisterLogger(const std::string& identity) { base::threading::ScopedLock scopedLock(ELPP->lock()); return ELPP->registeredLoggers()->remove(identity); }
inline void unregister(const T_Key& uniqKey) { T_Ptr* existing = get(uniqKey); if (existing != nullptr) { base::utils::safeDelete(existing); this->list().erase(uniqKey); } }第四行的 existing 和第五行的 uniqKey 其实是指向同一个对象的,先 delete 后 erase 导致出错。改正的方法也很简单,只需替换两行代码顺序即可。
#define ELPP_STL_LOGGING #include "easylogging++.h" INITIALIZE_EASYLOGGINGPP int main(int argc, char** argv) { /// 使用一个不存在的日记记录器会输出一条错误信息 CLOG(INFO, "testlog"); /// 注册一个新的日志记录器 el::Logger* newLogger = el::Loggers::getLogger("testlog"); CLOG(INFO, "testlog") << "this is a new logger"; /// 枚举日志记录器,没有定义宏 ELPP_SYSLOG,不会枚举出系统日志记录器 std::vector<std::string> allLoggers; el::Loggers::populateAllLoggerIds(&allLoggers); LOG(INFO) << allLoggers; /// 注销日志记录器 el::Loggers::unregisterLogger("testlog"); /// 使用一个不存在的日记记录器会输出一条错误信息 CLOG(INFO, "testlog"); /// 枚举日志记录器 el::Loggers::populateAllLoggerIds(&allLoggers); LOG(INFO) << allLoggers; system("pause"); return 0; }