目录
在easylogging++的哪些事(三)总体设计我们介绍了easylogging++的总体设计,从今天开始正式进入源码分析部分,首先我们来看看easylogging++的主流程。
一、easylogging++的初始化
easylogging++的那些事(一):功能介绍快速上手部分提到了接入easylogging++的方式是通过
INITIALIZE_EASYLOGGINGPP
宏完成初始化开始的,现在我们来看看这个宏:
话不多说,直接上源码:#if ELPP_ASYNC_LOGGING # define INITIALIZE_EASYLOGGINGPP ELPP_INIT_EASYLOGGINGPP(new el::base::Storage(el::LogBuilderPtr(new > el::base::DefaultLogBuilder()),\ new el::base::AsyncDispatchWorker())) #else # define INITIALIZE_EASYLOGGINGPP ELPP_INIT_EASYLOGGINGPP(new el::base::Storage(el::LogBuilderPtr(new > el::base::DefaultLogBuilder()))) #endif // ELPP_ASYNC_LOGGING
其中ELPP_ASYNC_LOGGING宏是用于开启异步日志的,不管是否开启异步日志,INITIALIZE_EASYLOGGINGPP实际上就是创建了一个
el::base::Storage
对象。在easylogging++的哪些事(三)总体设计中,我们介绍过
el::base::Storage
类是日志库的入口类或者总的管理类,easylogging++的所有与日志输出相关的信息都保存在el::base::Storage
类中,现在我们来看看el::base::Storage
类的初始化做了哪些事情,下面是源码(源码的解释已经在注释中了):
el::base::Storage
类构造函数的声明:#if ELPP_ASYNC_LOGGING Storage(const LogBuilderPtr& defaultLogBuilder, base::IWorker* asyncDispatchWorker); #else explicit Storage(const LogBuilderPtr& defaultLogBuilder); #endif // ELPP_ASYNC_LOGGING
el::base::Storage
类构造函数的定义:#if ELPP_ASYNC_LOGGING Storage::Storage(const LogBuilderPtr& defaultLogBuilder, base::IWorker* asyncDispatchWorker) : #else Storage::Storage(const LogBuilderPtr& defaultLogBuilder) : #endif // ELPP_ASYNC_LOGGING //初始化指定文件指定行的用于是否记录日志的计数器 m_registeredHitCounters(new base::RegisteredHitCounters()), //初始化注册的日志记录器的管理类 m_registeredLoggers(new base::RegisteredLoggers(defaultLogBuilder)), //初始化写日志时会用到的一些标志 m_flags(ELPP_DEFAULT_LOGGING_FLAGS), //初始化Verbose日志相关信息的管理类(保存Verbose日志级别以及vModule相关信息) m_vRegistry(new base::VRegistry(0, &m_flags)), #if ELPP_ASYNC_LOGGING //启用异步日志时,异步日志队列(暂时保存日志) m_asyncLogQueue(new base::AsyncLogQueue()), //启用异步日志时,异步日志调度器(处理AsyncLogQueue中的日志) m_asyncDispatchWorker(asyncDispatchWorker), #endif // ELPP_ASYNC_LOGGING //使用默认的日志回旋回调函数来初始化 m_preRollOutCallback(base::defaultPreRollOutCallback) { // Register default logger 注册默认的日志记录器 m_registeredLoggers->get(std::string(base::consts::kDefaultLoggerId)); // We register default logger anyway (worse case it's not going to register) just in case //再获取一次,以防未注册默认的日志记录器 m_registeredLoggers->get("default"); //注册性能跟踪日志记录器,并配置其日志格式 #if defined(ELPP_FEATURE_ALL) || defined(ELPP_FEATURE_PERFORMANCE_TRACKING) // Register performance logger and reconfigure format Logger* performanceLogger = m_registeredLoggers->get(std::string(base::consts::kPerformanceLoggerId)); m_registeredLoggers->get("performance"); performanceLogger->configurations()->setGlobally(ConfigurationType::Format, std::string("%datetime %level %msg")); performanceLogger->reconfigure(); #endif // defined(ELPP_FEATURE_ALL) || defined(ELPP_FEATURE_PERFORMANCE_TRACKING) //注册syslog日志记录器,并配置其日志格式 #if defined(ELPP_SYSLOG) // Register syslog logger and reconfigure format Logger* sysLogLogger = m_registeredLoggers->get(std::string(base::consts::kSysLogLoggerId)); sysLogLogger->configurations()->setGlobally(ConfigurationType::Format, std::string("%level: %msg")); sysLogLogger->reconfigure(); #endif // defined(ELPP_SYSLOG) //未配置VMODULE,也允许记录Verbose日志 addFlag(LoggingFlag::AllowVerboseIfModuleNotSpecified); #if ELPP_ASYNC_LOGGING //启用异步日志时,默认的LogDispatchCallback注册为AsyncLogDispatchCallback installLogDispatchCallback<base::AsyncLogDispatchCallback>(std::string("AsyncLogDispatchCallback")); #else //未启用异步日志时,默认的LogDispatchCallback注册为DefaultLogDispatchCallback installLogDispatchCallback<base::DefaultLogDispatchCallback>(std::string("DefaultLogDispatchCallback")); #endif // ELPP_ASYNC_LOGGING //注册性能跟踪默认回调DefaultPerformanceTrackingCallback #if defined(ELPP_FEATURE_ALL) || defined(ELPP_FEATURE_PERFORMANCE_TRACKING) installPerformanceTrackingCallback<base::DefaultPerformanceTrackingCallback> (std::string("DefaultPerformanceTrackingCallback")); #endif // defined(ELPP_FEATURE_ALL) || defined(ELPP_FEATURE_PERFORMANCE_TRACKING) ELPP_INTERNAL_INFO(1, "Easylogging++ has been initialized"); #if ELPP_ASYNC_LOGGING //启用异步日志时,开启一个线程(线程处理函数run)处理异步日志队列m_asyncLogQueue中保存的日志 m_asyncDispatchWorker->start(); #endif // ELPP_ASYNC_LOGGING }
二、easylogging++的反初始化
el::base::Storage
析构函数的定义如下:Storage::~Storage(void) { ELPP_INTERNAL_INFO(4, "Destroying storage"); #if ELPP_ASYNC_LOGGING ELPP_INTERNAL_INFO(5, "Replacing log dispatch callback to synchronous"); uninstallLogDispatchCallback<base::AsyncLogDispatchCallback>(std::string("AsyncLogDispatchCallback")); installLogDispatchCallback<base::DefaultLogDispatchCallback>(std::string("DefaultLogDispatchCallback")); ELPP_INTERNAL_INFO(5, "Destroying asyncDispatchWorker"); base::utils::safeDelete(m_asyncDispatchWorker); ELPP_INTERNAL_INFO(5, "Destroying asyncLogQueue"); base::utils::safeDelete(m_asyncLogQueue); #endif // ELPP_ASYNC_LOGGING ELPP_INTERNAL_INFO(5, "Destroying registeredHitCounters"); base::utils::safeDelete(m_registeredHitCounters); ELPP_INTERNAL_INFO(5, "Destroying registeredLoggers"); base::utils::safeDelete(m_registeredLoggers); ELPP_INTERNAL_INFO(5, "Destroying vRegistry"); base::utils::safeDelete(m_vRegistry); }
这里的逻辑就很简单了,就是初始化时动态创建的一些对象的释放,这里对不多说了。
easylogging++的主流程就介绍到这里,下一篇开始源码分析日志记录宏。
标签:LOGGING,++,Storage,源码,base,主流程,日志,ELPP From: https://www.cnblogs.com/DesignLife/p/16922074.html