首页 > 编程语言 >easylogging++的那些事(四)源码分析(一)主流程

easylogging++的那些事(四)源码分析(一)主流程

时间:2022-11-24 18:23:11浏览次数:65  
标签:LOGGING ++ Storage 源码 base 主流程 日志 ELPP

目录

在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

相关文章

  • C++模拟游戏中鼠标点击和键盘按键
    游戏中模拟键盘输入,有时回被系统屏蔽,Java等语言都试过很多方法,好像都没用,所以下面给出一种C++实现方法#include<iostream>#include<windows.h>//forkeypushingBYTEsca......
  • C++Primer 第3章
    第3章字符串、向量和数组3.1命名空间的using声明目前为止,我们用到的库函数基本上都属于命名空间std,而程序也显式地将这一点标示了出来。例如,std::cin表示从标准输入中......
  • C++模拟鼠标键盘操作
    API或MFC视窗程序里有函数,例如API函数设位置:BOOLSetCursorPos(intx,inty);参数是屏幕坐标x,y头文件Winuser.h链接库#pragmacomment(lib,......
  • VC++模拟键盘输入(keybd_event() 、 PostMessage() /SendMessage()、SendInput())详解
                                           VC++模拟键盘输入找了一周,终于找到模拟键......
  • C++模拟按键和发送数据
    ZjlAj.h  #pragmaonce #include"stdafx.h"   #defineZJL_TEXT_T("RFSettingV3.47")   enum{NOS_COM=0X20000,N......
  • C++模拟键盘按下的代码
            最近想写自动登录软件的程序,但缺少模拟电脑键盘按下的代码,写了一个小封装,希望大家能看懂或指正一下。 #include<windows.h> #include......
  • C++中的Type Alias
    在C++中,我们通常使用typedef来实现typealias.比如:#include<cstdint>//Cstandardinttypedefuint32_tpoints_t;//points_tisaliasofuint32_ttypedefuin......
  • C++ 简易按键精灵制作
     简易按键精灵制作参考链接:https://docs.microsoft.com/zh-cn/windows/win32/api/winuser/nf-winuser-sendinput?redirectedfrom=MSDN、https://www.fluentcpp.com/20......
  • C++ 模拟鼠标键盘操作
     文章目录1、简介2、GetAsyncKeyState2.1函数声明2.2宏定义2.3代码示例3.GetKeyState3.1函数声明3.2宏定义4、mouse_event4.1函数声明4.2......
  • vscode debug c/c++
    task.json{//tasks用于调试前,执行自定义的任务,生成可调式的文件"tasks":[{"type":"cppbuild",//调......