首页 > 编程语言 >easylogging++的那些事(四)源码分析(十二)Logger和RegisteredLoggers相关接口

easylogging++的那些事(四)源码分析(十二)Logger和RegisteredLoggers相关接口

时间:2022-12-08 20:12:12浏览次数:68  
标签:const ++ utils easylogging 源码 void base 日志 id

目录

在上一篇我们介绍完了 Storage 类的其他接口。今天我们来看看 Logger 类和 RegisteredLoggers 类的接口。

Logger 类

已经介绍过的接口

    在 总体设计 框架设计部分我们介绍了 Logger 类保存的一些信息。
    在 日志格式配置方式 中我们介绍了 Logger 类与日志配置相关的一些接口。相关接口的声明如下:

void configure(const Configurations &configurations);
void resolveLoggerFormatSpec(void) const;
Logger(const std::string &id, const Configurations &configurations, base::LogStreamsReferenceMapPtr logStreamsReference);
void reconfigure(void);

    在 CLOG 宏 其他相关类 中我们介绍了 Logger 类与日志输出相关的一些接口。相关接口的声明如下:

bool isValidId(const std::string &id)
void flush(void);
void flush(Level level, base::type::fstream_t *fs);
void initUnflushedCount(void);

    在 类 printf 接口 中我们详细分析了 Logger 的类 printf 接口的实现。

构造函数

Logger::Logger(const std::string &id, base::LogStreamsReferenceMapPtr logStreamsReference)
    : m_id(id), m_typedConfigurations(nullptr), m_parentApplicationName(std::string()), m_isConfigured(false), m_logStreamsReference(logStreamsReference)
{
    initUnflushedCount();
}

    initUnflushedCount 接口在 CLOG其他相关类 中已经介绍过了,这里就不多说了。

Logger::Logger(const std::string &id, const Configurations &configurations, base::LogStreamsReferenceMapPtr logStreamsReference)
    : m_id(id), m_typedConfigurations(nullptr), m_parentApplicationName(std::string()), m_isConfigured(false), m_logStreamsReference(logStreamsReference)
{
    initUnflushedCount();
    configure(configurations);
}

    configure 接口在 日志格式配置方式 中已经介绍过了,这里就不多说了。

Logger::Logger(const Logger &logger)
{
    base::utils::safeDelete(m_typedConfigurations);
    m_id = logger.m_id;
    m_typedConfigurations = logger.m_typedConfigurations;
    m_parentApplicationName = logger.m_parentApplicationName;
    m_isConfigured = logger.m_isConfigured;
    m_configurations = logger.m_configurations;
    m_unflushedCount = logger.m_unflushedCount;
    m_logStreamsReference = logger.m_logStreamsReference;
}

赋值运算符

Logger &Logger::operator=(const Logger &logger)
{
    if (&logger != this)
    {
        base::utils::safeDelete(m_typedConfigurations);
        m_id = logger.m_id;
        m_typedConfigurations = logger.m_typedConfigurations;
        m_parentApplicationName = logger.m_parentApplicationName;
        m_isConfigured = logger.m_isConfigured;
        m_configurations = logger.m_configurations;
        m_unflushedCount = logger.m_unflushedCount;
        m_logStreamsReference = logger.m_logStreamsReference;
    }
    return *this;
}

析构函数

virtual ~Logger(void)
{
    base::utils::safeDelete(m_typedConfigurations);
}

日志输出支持

// 用于直接日志输出支持
virtual inline void log(el::base::type::ostream_t &os) const
{
    os << m_id.c_str();
}

获取日志记录器 ID

const std::string &id(void) const
{
    return m_id;
}

应用程序名称相关接口

// 获取应用程序名称
inline const std::string &parentApplicationName(void) const
{
    return m_parentApplicationName;
}

// 设置应用程序名称
inline void setParentApplicationName(const std::string &parentApplicationName)
{
    m_parentApplicationName = parentApplicationName;
}

获取日志记录器对应的 Configurations 对象

inline Configurations *configurations(void)
{
    return &m_configurations;
}

获取日志记录器对应的 TypedConfigurations 对象

inline base::TypedConfigurations *typedConfigurations(void)
{
    return m_typedConfigurations;
}

日志构建器

// 获取日志构建器
inline LogBuilder *logBuilder(void) const
{
    return m_logBuilder.get();
}

// 设置日志构建器
inline void setLogBuilder(const LogBuilderPtr &logBuilder)
{
    m_logBuilder = logBuilder;
}

    LogBuilder 类稍后会详细分析。

日志记录器的指定日志级别是否启用

inline bool enabled(Level level) const
{
    return m_typedConfigurations->enabled(level);
}

获取日志记录器对应的字符串流对象

inline base::type::stringstream_t &stream(void)
{
    return m_stream;
}

RegisteredLoggers 类

    RegisteredLoggers 类用于管理注册的日志记录器。
    先看下继承关系:
    公有继承自 base::utils::Registry <Logger, std::string>
    底层容器为 std::unordered_map <std::string, Logger*>

    基于 Registryiterator 别名定义了自身的迭代器类型别名 iteratorconst_iterator

// 实际iterator就是std::unordered_map<std::string, Logger*>::iterator
typedef typename Registry<Logger, std::string>::iterator iterator;
// 实际const_iterator就是std::unordered_map<std::string, Logger*>::iconst_iterator
typedef typename Registry<Logger, std::string>::const_iterator const_iterator;

    当前类中未直接使用 iteratorconst_iterator 类型。
    Registry 类模板在后面分析easylogging++的设计理念时会详细介绍。

    RegisteredLoggers 类的实现如下:

class RegisteredLoggers : public base::utils::Registry<Logger, std::string>
{
public:
    explicit RegisteredLoggers(const LogBuilderPtr &defaultLogBuilder);

    virtual ~RegisteredLoggers(void)
    {
        unsafeFlushAll();
    }

    inline void setDefaultConfigurations(const Configurations &configurations)
    {
        base::threading::ScopedLock scopedLock(lock());
        m_defaultConfigurations.setFromBase(const_cast<Configurations *>(&configurations));
    }

    inline Configurations *defaultConfigurations(void)
    {
        return &m_defaultConfigurations;
    }

    // 根据日志记录器id获取对应的日志记录器
    Logger *get(const std::string &id, bool forceCreation = true);

    // 根据注册回调标识ID获取注册对应的回调
    template <typename T>
    inline bool installLoggerRegistrationCallback(const std::string &id)
    {
        return base::utils::Utils::installCallback<T, base::type::LoggerRegistrationCallbackPtr>(id, &m_loggerRegistrationCallbacks);
    }

    // 根据注册回调标识ID获取注销对应的回调
    template <typename T>
    inline void uninstallLoggerRegistrationCallback(const std::string &id)
    {
        base::utils::Utils::uninstallCallback<T, base::type::LoggerRegistrationCallbackPtr>(id, &m_loggerRegistrationCallbacks);
    }

    // 根据注册回调标识ID获取对应的注册回调对象指针
    template <typename T>
    inline T *loggerRegistrationCallback(const std::string &id)
    {
        return base::utils::Utils::callback<T, base::type::LoggerRegistrationCallbackPtr>(id, &m_loggerRegistrationCallbacks);
    }

    bool remove(const std::string &id);
    // 根据日志记录器ID查找对应的日志记录器
    inline bool has(const std::string &id)
    {
        return get(id, false) != nullptr;
    }

    // 删除日志记录器
    inline void unregister(Logger *&logger)
    {
        base::threading::ScopedLock scopedLock(lock());
        base::utils::Registry<Logger, std::string>::unregister(logger->id());
    }

    inline LogStreamsReferenceMapPtr logStreamsReference(void)
    {
        return m_logStreamsReference;
    }

    // 刷新全部的日志文件(线程安全)
    inline void flushAll(void)
    {
        base::threading::ScopedLock scopedLock(lock());
        unsafeFlushAll();
    }

    inline void setDefaultLogBuilder(LogBuilderPtr &logBuilderPtr)
    {
        base::threading::ScopedLock scopedLock(lock());
        m_defaultLogBuilder = logBuilderPtr;
    }

private:
    LogBuilderPtr m_defaultLogBuilder;                                                                        // 默认的日志构建器
    Configurations m_defaultConfigurations;                                                                   // 默认的日志配置
    base::LogStreamsReferenceMapPtr m_logStreamsReference = nullptr;                                          // 日志文件流相关
    std::unordered_map<std::string, base::type::LoggerRegistrationCallbackPtr> m_loggerRegistrationCallbacks; // 日志注册回调对象容器,key->日志记录器注册回调ID,value->日志记录器注册回调
    friend class el::base::Storage;

    // 刷新全部的日志文件(非线程安全)
    void unsafeFlushAll(void);
};

RegisteredLoggers::RegisteredLoggers(const LogBuilderPtr &defaultLogBuilder) : m_defaultLogBuilder(defaultLogBuilder)
{
    m_defaultConfigurations.setToDefault();
    m_logStreamsReference = std::make_shared<base::LogStreamsReferenceMap>();
}

    Configurations 类已经在 日志格式配置管理类 详细介绍过了。

根据日志记录器 id 获取对应的日志记录器

Logger *RegisteredLoggers::get(const std::string &id, bool forceCreation)
{
    base::threading::ScopedLock scopedLock(lock());
    Logger *logger_ = base::utils::Registry<Logger, std::string>::get(id);
    // 当不存在forceCreation为true时,新建对应id的logger并注册,同时调用注册回调
    if (logger_ == nullptr && forceCreation)
    {
        bool validId = Logger::isValidId(id);
        if (!validId)
        {
            ELPP_ASSERT(validId, "Invalid logger ID [" << id << "]. Not registering this logger.");
            return nullptr;
        }
        logger_ = new Logger(id, m_defaultConfigurations, m_logStreamsReference);
        logger_->m_logBuilder = m_defaultLogBuilder;
        // 注册新的日志记录器
        registerNew(id, logger_);
        LoggerRegistrationCallback *callback = nullptr;
        // 遍历全部的日志记录器注册事件回调
        for (const std::pair<std::string, base::type::LoggerRegistrationCallbackPtr> &h : m_loggerRegistrationCallbacks)
        {
            callback = h.second.get();
            if (callback != nullptr && callback->enabled())
            {
                callback->handle(logger_);
            }
        }
    }
    return logger_;
}

    registerNew 接口后面分析 easylogging++的设计理念时会详细分析,这里就不多说了。

根据日志记录器 id 删除对应的日志记录器

bool RegisteredLoggers::remove(const std::string &id)
{
    if (id == base::consts::kDefaultLoggerId)
    {
        return false;
    }
    // get has internal lock
    Logger *logger = base::utils::Registry<Logger, std::string>::get(id);
    if (logger != nullptr)
    {
        // unregister has internal lock
        unregister(logger);
    }
    return true;
}

刷新全部的日志文件

void RegisteredLoggers::unsafeFlushAll(void)
{
    ELPP_INTERNAL_INFO(1, "Flushing all log files");
    // m_logStreamsReference保存了当前正在使用的所有的日志文件流。
    for (base::LogStreamsReferenceMap::iterator it = m_logStreamsReference->begin();
         it != m_logStreamsReference->end(); ++it)
    {
        if (it->second.get() == nullptr)
            continue;
        it->second->flush();
    }
}

其他相关类

LogBuilder 类

    LogBuilder 类用来将 LogMessage 调整(如是否需要加入换行,以及是否添加彩色显示时需要增加的相关标记用于彩色显示)。
    LogBuilder 类是抽象类,只能被继承,派生类需要实现 build 接口。

class LogBuilder : base::NoCopy
{
public:
    LogBuilder() : m_termSupportsColor(base::utils::OS::termSupportsColor()) {}
    virtual ~LogBuilder(void)
    {
        ELPP_INTERNAL_INFO(3, "Destroying log builder...")
    }

    // build接口主要用于调整LogMessage
    virtual base::type::string_t build(const LogMessage *logMessage, bool appendNewLine) const = 0;

    // 调整LogMessage用于彩色显示
    void convertToColoredOutput(base::type::string_t *logLine, Level level);

private:
    bool m_termSupportsColor;
    friend class el::base::DefaultLogDispatchCallback;
};

// 调整LogMessage用于彩色显示
void LogBuilder::convertToColoredOutput(base::type::string_t *logLine, Level level)
{
    if (!m_termSupportsColor)
        return;
    const base::type::char_t *resetColor = ELPP_LITERAL("\x1b[0m");
    if (level == Level::Error || level == Level::Fatal)
        *logLine = ELPP_LITERAL("\x1b[31m") + *logLine + resetColor;
    else if (level == Level::Warning)
        *logLine = ELPP_LITERAL("\x1b[33m") + *logLine + resetColor;
    else if (level == Level::Debug)
        *logLine = ELPP_LITERAL("\x1b[32m") + *logLine + resetColor;
    else if (level == Level::Info)
        *logLine = ELPP_LITERAL("\x1b[36m") + *logLine + resetColor;
    else if (level == Level::Trace)
        *logLine = ELPP_LITERAL("\x1b[35m") + *logLine + resetColor;
}

DefaultLogBuilder 类

    DefaultLogBuilder 类继承自 LogBuilder 类,实现了 build 接口。
    DefaultLogBuilder 类主要作为 easylogging++的默认日志构建器,用于初始化 Storage 全局管理类

class DefaultLogBuilder : public LogBuilder
{
public:
    base::type::string_t build(const LogMessage *logMessage, bool appendNewLine) const;
};

base::type::string_t DefaultLogBuilder::build(const LogMessage *logMessage, bool appendNewLine) const
{
    base::TypedConfigurations *tc = logMessage->logger()->typedConfigurations();
    // LogFormat:日志格式工具类用于管理日志格式(单个日志记录器的FORMAT配置项)
    const base::LogFormat *logFormat = &tc->logFormat(logMessage->level());
    // 配置文件当中单个日志记录器的FORMAT配置项的字符串形式的值
    base::type::string_t logLine = logFormat->format();
    char buff[base::consts::kSourceFilenameMaxLength + base::consts::kSourceLineMaxLength] = "";
    const char *bufLim = buff + sizeof(buff);
    // 下面依次检查有没有指定的日志格式指示器,有则替换为实际的内容
    if (logFormat->hasFlag(base::FormatFlags::AppName))
    {
        // App name
        base::utils::Str::replaceFirstWithEscape(logLine, base::consts::kAppNameFormatSpecifier, logMessage->logger()->parentApplicationName());
    }
    if (logFormat->hasFlag(base::FormatFlags::ThreadId))
    {
        // Thread ID
        base::utils::Str::replaceFirstWithEscape(logLine, base::consts::kThreadIdFormatSpecifier, ELPP->getThreadName(base::threading::getCurrentThreadId()));
    }
    if (logFormat->hasFlag(base::FormatFlags::DateTime))
    {
        // DateTime
        base::utils::Str::replaceFirstWithEscape(logLine, base::consts::kDateTimeFormatSpecifier,
                                                 base::utils::DateTime::getDateTime(logFormat->dateTimeFormat().c_str(),
                                                                                    &tc->subsecondPrecision(logMessage->level())));
    }
    if (logFormat->hasFlag(base::FormatFlags::Function))
    {
        // Function
        base::utils::Str::replaceFirstWithEscape(logLine, base::consts::kLogFunctionFormatSpecifier, logMessage->func());
    }
    if (logFormat->hasFlag(base::FormatFlags::File))
    {
        // File
        base::utils::Str::clearBuff(buff, base::consts::kSourceFilenameMaxLength);
        base::utils::File::buildStrippedFilename(logMessage->file().c_str(), buff);
        base::utils::Str::replaceFirstWithEscape(logLine, base::consts::kLogFileFormatSpecifier, std::string(buff));
    }
    if (logFormat->hasFlag(base::FormatFlags::FileBase))
    {
        // FileBase
        base::utils::Str::clearBuff(buff, base::consts::kSourceFilenameMaxLength);
        base::utils::File::buildBaseFilename(logMessage->file(), buff);
        base::utils::Str::replaceFirstWithEscape(logLine, base::consts::kLogFileBaseFormatSpecifier, std::string(buff));
    }
    if (logFormat->hasFlag(base::FormatFlags::Line))
    {
        // Line
        char *buf = base::utils::Str::clearBuff(buff, base::consts::kSourceLineMaxLength);
        buf = base::utils::Str::convertAndAddToBuff(logMessage->line(), base::consts::kSourceLineMaxLength, buf, bufLim, false);
        base::utils::Str::replaceFirstWithEscape(logLine, base::consts::kLogLineFormatSpecifier, std::string(buff));
    }
    if (logFormat->hasFlag(base::FormatFlags::Location))
    {
        // Location
        char *buf = base::utils::Str::clearBuff(buff, base::consts::kSourceFilenameMaxLength + base::consts::kSourceLineMaxLength);
        base::utils::File::buildStrippedFilename(logMessage->file().c_str(), buff);
        buf = base::utils::Str::addToBuff(buff, buf, bufLim);
        buf = base::utils::Str::addToBuff(":", buf, bufLim);
        buf = base::utils::Str::convertAndAddToBuff(logMessage->line(), base::consts::kSourceLineMaxLength, buf, bufLim, false);
        base::utils::Str::replaceFirstWithEscape(logLine, base::consts::kLogLocationFormatSpecifier, std::string(buff));
    }
    if (logMessage->level() == Level::Verbose && logFormat->hasFlag(base::FormatFlags::VerboseLevel))
    {
        // Verbose level
        char *buf = base::utils::Str::clearBuff(buff, 1);
        buf = base::utils::Str::convertAndAddToBuff(logMessage->verboseLevel(), 1, buf, bufLim, false);
        base::utils::Str::replaceFirstWithEscape(logLine, base::consts::kVerboseLevelFormatSpecifier, std::string(buff));
    }
    if (logFormat->hasFlag(base::FormatFlags::LogMessage))
    {
        // Log message
        base::utils::Str::replaceFirstWithEscape(logLine, base::consts::kMessageFormatSpecifier, logMessage->message());
    }
#if !defined(ELPP_DISABLE_CUSTOM_FORMAT_SPECIFIERS)
    // 自定义的日志格式指示器用对应注册的日志格式解析器解析的结果来替换
    el::base::threading::ScopedLock lock_(ELPP->customFormatSpecifiersLock());
    ELPP_UNUSED(lock_);
    // 依次遍历所有的日志格式解析器,对所有自定义的格式都替换一遍
    for (std::vector<CustomFormatSpecifier>::const_iterator it = ELPP->customFormatSpecifiers()->begin();
         it != ELPP->customFormatSpecifiers()->end(); ++it)
    {
        std::string fs(it->formatSpecifier());
        base::type::string_t wcsFormatSpecifier(fs.begin(), fs.end());
        base::utils::Str::replaceFirstWithEscape(logLine, wcsFormatSpecifier, it->resolver()(logMessage));
    }
#endif // !defined(ELPP_DISABLE_CUSTOM_FORMAT_SPECIFIERS)
    if (appendNewLine)
        logLine += ELPP_LITERAL("\n");
    return logLine;
}

    build 接口本身并不复杂,这里就不多说了。
    base::utils::Str 类是通用字符串操作的工具类,base::utils::File 是通用文件操作的工具类,后面文章会详细介绍。

至此,Logger 类和 RegisteredLoggers 类就介绍完了,下一篇我们开始介绍 LogFormat 类。

标签:const,++,utils,easylogging,源码,void,base,日志,id
From: https://www.cnblogs.com/DesignLife/p/16967138.html

相关文章

  • VC++ ScrollBar控件的使用方法
    1、在对话框中拖入CScrollBar控件,并修改ID,2、示例一:voidCTestScrollBarDlg::OnHScroll(UINTnSBCode,UINTnPos,CScrollBar*pScrollBar){ //TODO:在此添加消息处......
  • C++ pair
    1.头文件pair函数存在于C++标准库#include<utility>中。2.用处他可以创建一个有2个成员的结构体。3.使用方法它的使用方法是:pair<数据类型1,数据类型2>结构体变量......
  • MetaForce原力元宇宙佛萨奇2.0智能合约系统开发(源码部署)
    今天带你真正了解佛萨奇原力force,你用简单的话术让你真正明白佛丝!什么是佛萨奇原力FORGE?佛萨奇原力FORCE是在链上部署的一个智能合约BSC智能链是基于全球第一交易所网的数字......
  • C++图谱
    最新的培训机构培训图谱,可以用于自身知识体系图谱的构建。......
  • 继承与组合 C++(五)
     1.继承如果A是基类,B是A的派生类,那么B将继承A的数据和函数。 C++的“继承”特性可以提高程序的可复用性。正因为“继承”太有用、太容易用,才要防止乱用“继承”。我们应当......
  • C++面试题(四)
     ​待续#include<QCoreApplication>#include<QTextStream>#include<iostream>usingnamespacestd;classString{public:String(constchar*str=NULL);......
  • gin源码学习-路由注册(2)
    gin框架主要是在标准库net/http的基础上对路由进行改写,本文将从net/http与gin的路由注册分享路由方面的一些理解。1.net/http的路由注册1.1路由注册首先来个demo:pack......
  • [附源码]Python计算机毕业设计Django汽车租赁管理系统
    OverridetheentrypointofanimageIntroducedinGitLabandGitLabRunner9.4.Readmoreaboutthe extendedconfigurationoptions.Beforeexplainingtheav......
  • c++ stl总结
    前言本篇是c++总结系列的stl篇,重点讲解容器,及effectivestl的总结stl原理stl提供六大部件,这六大部件可以彼此搭配工作,这六大部件是:容器。各种数据结构迭代器。扮演容......
  • Notepad++ ,json 、xml 格式化插件安装不了 和 github 网站访问不了 最佳解决方案
    OverridetheentrypointofanimageIntroducedinGitLabandGitLabRunner9.4.Readmoreaboutthe extendedconfigurationoptions.Beforeexplainingtheav......