easylogging++的那些事(四)源码分析(十一)Storage类的其他接口

在上一篇我们扩展了 easylogging++ 日志回滚 的默认实现。今天我们把 Storage 类的其他剩余的接口全部介绍完。

已经介绍过 Storage 类的接口

    在 easylogging++的 总体设计 中我们介绍了 Storage 类的主要功能(所有与日志输出相关的信息都保存在 Storage 类中,日志库的入口类或者总的管理类)。
    在 easylogging++的 主流程 中我们介绍了 Storage 类的初始化和析构。
    在 easylogging++的 日志格式配置方式 中我们介绍了 Storage 类的的解析命令行参数相关的接口。相关接口的声明如下:

void setApplicationArguments(int argc, char** argv);
void setApplicationArguments(int argc, const char** argv); 

    在 easylogging++的 偶尔日志宏 中我们介绍了偶尔写日志相关的一些接口。相关接口的声明如下:

base::RegisteredHitCounters* hitCounters(void) const;
bool validateEveryNCounter(const char* filename, base::type::LineNumber lineNumber, std::size_t occasion);
bool validateAfterNCounter(const char* filename, base::type::LineNumber lineNumber, std::size_t n);
bool validateNTimesCounter(const char* filename, base::type::LineNumber lineNumber, std::size_t n);

Storage 类的其他接口

获取注册的日志记录器的管理类

inline base::RegisteredLoggers *registeredLoggers(void) const
{
    return m_registeredLoggers;
}

获取VERBOSE日志信息管理类

inline base::VRegistry *vRegistry(void) const
{
    return m_vRegistry;
}

获取异步日志队列

#if ELPP_ASYNC_LOGGING
inline base::AsyncLogQueue *asyncLogQueue(void) const
{
    return m_asyncLogQueue;
}
#endif // ELPP_ASYNC_LOGGING

获取命令行参数解析器

inline const base::utils::CommandLineArgs *commandLineArgs(void) const
{
    return &m_commandLineArgs;
}

与全局 LoggingFlag 相关的接口

// 向全局LoggingFlag中追加加LoggingFlag
inline void addFlag(LoggingFlag flag)
{
    base::utils::addFlag(flag, &m_flags);
}

// 从全局LoggingFlag中删除LoggingFlag
inline void removeFlag(LoggingFlag flag)
{
    base::utils::removeFlag(flag, &m_flags);
}

// 从全局LoggingFlag中查询指定的LoggingFlag
inline bool hasFlag(LoggingFlag flag) const
{
    return base::utils::hasFlag(flag, m_flags);
}

// 返回全局LoggingFlag
inline base::type::EnumType flags(void) const
{
    return m_flags;
}

// 直接设置全局LoggingFlag
inline void setFlags(base::type::EnumType flags)
{
    m_flags = flags;
}

设置分层日志级别的基准级别

// 全局分层日志级别的基准级别,在决定是否真正处理一条日志时使用
inline void setLoggingLevel(Level level)
{
    m_loggingLevel = level;
}

日志回滚回调

    日志回滚回调时相关接口(日志回滚时触发),日志回滚回调在 扩展日志回滚 中已经介绍过。

// 设置日志回旋回调
inline void setPreRollOutCallback(const PreRollOutCallback &callback)
{
    m_preRollOutCallback = callback;
}

// 删除日志回旋回调
inline void unsetPreRollOutCallback(void)
{
    // base::defaultPreRollOutCallback默认的日志回旋回调不做任何事情
    m_preRollOutCallback = base::defaultPreRollOutCallback;
}

// 获取日志回旋回调
inline PreRollOutCallback &preRollOutCallback(void)
{
    return m_preRollOutCallback;
}

日志派发回调

    日志派发回调时相关(写日志时触发),日志派发回调在 CLOG日志输出 中介绍过了。

// 注册日志派发回调
template <typename T>
inline bool installLogDispatchCallback(const std::string &id)
{
    return base::utils::Utils::installCallback<T, base::type::LogDispatchCallbackPtr>(id, &m_logDispatchCallbacks);
}

// 注销日志派发回调
template <typename T>
inline void uninstallLogDispatchCallback(const std::string &id)
{
    base::utils::Utils::uninstallCallback<T, base::type::LogDispatchCallbackPtr>(id, &m_logDispatchCallbacks);
}

// 根据日志派发回调的ID获取日志派发回调对象指针
template <typename T>
inline T *logDispatchCallback(const std::string &id)
{
    return base::utils::Utils::callback<T, base::type::LogDispatchCallbackPtr>(id, &m_logDispatchCallbacks);
}

性能跟踪回调

    性能跟踪相关(性能跟踪时触发),性能跟踪在前面分析 性能跟踪 的实现中已经介绍过了。

// 注册性能跟踪回调
#if defined(ELPP_FEATURE_ALL) || defined(ELPP_FEATURE_PERFORMANCE_TRACKING)
template <typename T>
inline bool installPerformanceTrackingCallback(const std::string &id)
{
    return base::utils::Utils::installCallback<T, base::type::PerformanceTrackingCallbackPtr>(id, &m_performanceTrackingCallbacks);
}

// 注销性能跟踪回调
template <typename T>
inline void uninstallPerformanceTrackingCallback(const std::string &id)
{
    base::utils::Utils::uninstallCallback<T, base::type::PerformanceTrackingCallbackPtr>(id, &m_performanceTrackingCallbacks);
}

// 获取性能跟踪回调对象指针
template <typename T>
inline T *performanceTrackingCallback(const std::string &id)
{
    return base::utils::Utils::callback<T, base::type::PerformanceTrackingCallbackPtr>(id, &m_performanceTrackingCallbacks);
}
#endif // defined(ELPP_FEATURE_ALL) || defined(ELPP_FEATURE_PERFORMANCE_TRACKING)

线程名称相关

// 设置线程名称
/// @brief Sets thread name for current thread. Requires std::thread
inline void setThreadName(const std::string &name)
{
    if (name.empty())
        return;
    base::threading::ScopedLock scopedLock(m_threadNamesLock);
    m_threadNames[base::threading::getCurrentThreadId()] = name;
}

// 获取线程名称
inline std::string getThreadName(const std::string &threadId)
{
    base::threading::ScopedLock scopedLock(m_threadNamesLock);
    std::unordered_map<std::string, std::string>::const_iterator it = m_threadNames.find(threadId);
    if (it == m_threadNames.end())
    {
        return threadId;
    }
    return it->second;
}

自定义日志格式解析器相关接口

// 获取日志格式解析器的管理类
const std::vector<CustomFormatSpecifier> *customFormatSpecifiers(void) const
{
    return &m_customFormatSpecifiers;
}

// 获取日志格式解析器的管理类的锁
base::threading::Mutex &customFormatSpecifiersLock()
{
    return m_customFormatSpecifiersLock;
}

// 判断指定的自定义日志格式是否存在相应的日志格式解析器
bool Storage::hasCustomFormatSpecifier(const char *formatSpecifier)
{
    base::threading::ScopedLock scopedLock(customFormatSpecifiersLock());
    return std::find(m_customFormatSpecifiers.begin(), m_customFormatSpecifiers.end(), formatSpecifier) != m_customFormatSpecifiers.end();
}

// 安装日志格式解析器
void Storage::installCustomFormatSpecifier(const CustomFormatSpecifier &customFormatSpecifier)
{
    if (hasCustomFormatSpecifier(customFormatSpecifier.formatSpecifier()))
    {
        return;
    }
    base::threading::ScopedLock scopedLock(customFormatSpecifiersLock());
    m_customFormatSpecifiers.push_back(customFormatSpecifier);
}

// 卸载日志格式解析器
bool Storage::uninstallCustomFormatSpecifier(const char *formatSpecifier)
{
    base::threading::ScopedLock scopedLock(customFormatSpecifiersLock());
    std::vector<CustomFormatSpecifier>::iterator it = std::find(m_customFormatSpecifiers.begin(), m_customFormatSpecifiers.end(), formatSpecifier);
    if (it != m_customFormatSpecifiers.end() && strcmp(formatSpecifier, it->formatSpecifier()) == 0)
    {
        m_customFormatSpecifiers.erase(it);
        return true;
    }
    return false;
}

    CustomFormatSpecifier 类用于指定自定义日志格式以及对应解析接口。
    上面的 Storage 类的这些接口都很简单,这里就不多说了。

其他相关类

CustomFormatSpecifier 类

    CustomFormatSpecifier 类是自定义日志格式管理工具类,用于指定自定义日志格式以及对应解析接口。

typedef std::function<std::string(const LogMessage *)> FormatSpecifierValueResolver;

class CustomFormatSpecifier
{
public:
    CustomFormatSpecifier(const char *formatSpecifier, const FormatSpecifierValueResolver &resolver)
        : m_formatSpecifier(formatSpecifier), m_resolver(resolver) {}

    inline const char *formatSpecifier(void) const
    {
        return m_formatSpecifier;
    }
    inline const FormatSpecifierValueResolver &resolver(void) const
    {
        return m_resolver;
    }
    inline bool operator==(const char *formatSpecifier)
    {
        return strcmp(m_formatSpecifier, formatSpecifier) == 0;
    }

private:
    const char *m_formatSpecifier;           // 自定义日志格式指示器
    FormatSpecifierValueResolver m_resolver; // 自定义日志格式指示器的解析函数对象
};

    CustomFormatSpecifier 的实现很简单,没什么好多说的。

至此,Storage 类的所有接口就都介绍完了,下一篇我们开始介绍 LoggerRegisteredLoggers 的其他接口。

posted @ 2022-12-08 13:37  节奏自由  阅读(54)  评论(0编辑  收藏  举报