spdlog源码解析

作者 : admin 本文共8552个字,预计阅读时间需要22分钟 发布时间: 2024-06-10 共1人阅读

基础抽象

#mermaid-svg-8Wlnt0sI42bDkciS {font-family:”trebuchet ms”,verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-8Wlnt0sI42bDkciS .error-icon{fill:#552222;}#mermaid-svg-8Wlnt0sI42bDkciS .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-8Wlnt0sI42bDkciS .edge-thickness-normal{stroke-width:2px;}#mermaid-svg-8Wlnt0sI42bDkciS .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-8Wlnt0sI42bDkciS .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-8Wlnt0sI42bDkciS .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-8Wlnt0sI42bDkciS .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-8Wlnt0sI42bDkciS .marker{fill:#333333;stroke:#333333;}#mermaid-svg-8Wlnt0sI42bDkciS .marker.cross{stroke:#333333;}#mermaid-svg-8Wlnt0sI42bDkciS svg{font-family:”trebuchet ms”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-8Wlnt0sI42bDkciS g.classGroup text{fill:#9370DB;fill:#131300;stroke:none;font-family:”trebuchet ms”,verdana,arial,sans-serif;font-size:10px;}#mermaid-svg-8Wlnt0sI42bDkciS g.classGroup text .title{font-weight:bolder;}#mermaid-svg-8Wlnt0sI42bDkciS .nodeLabel,#mermaid-svg-8Wlnt0sI42bDkciS .edgeLabel{color:#131300;}#mermaid-svg-8Wlnt0sI42bDkciS .edgeLabel .label rect{fill:#ECECFF;}#mermaid-svg-8Wlnt0sI42bDkciS .label text{fill:#131300;}#mermaid-svg-8Wlnt0sI42bDkciS .edgeLabel .label span{background:#ECECFF;}#mermaid-svg-8Wlnt0sI42bDkciS .classTitle{font-weight:bolder;}#mermaid-svg-8Wlnt0sI42bDkciS .node rect,#mermaid-svg-8Wlnt0sI42bDkciS .node circle,#mermaid-svg-8Wlnt0sI42bDkciS .node ellipse,#mermaid-svg-8Wlnt0sI42bDkciS .node polygon,#mermaid-svg-8Wlnt0sI42bDkciS .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-8Wlnt0sI42bDkciS .divider{stroke:#9370DB;stroke:1;}#mermaid-svg-8Wlnt0sI42bDkciS g.clickable{cursor:pointer;}#mermaid-svg-8Wlnt0sI42bDkciS g.classGroup rect{fill:#ECECFF;stroke:#9370DB;}#mermaid-svg-8Wlnt0sI42bDkciS g.classGroup line{stroke:#9370DB;stroke-width:1;}#mermaid-svg-8Wlnt0sI42bDkciS .classLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5;}#mermaid-svg-8Wlnt0sI42bDkciS .classLabel .label{fill:#9370DB;font-size:10px;}#mermaid-svg-8Wlnt0sI42bDkciS .relation{stroke:#333333;stroke-width:1;fill:none;}#mermaid-svg-8Wlnt0sI42bDkciS .dashed-line{stroke-dasharray:3;}#mermaid-svg-8Wlnt0sI42bDkciS #compositionStart,#mermaid-svg-8Wlnt0sI42bDkciS .composition{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-8Wlnt0sI42bDkciS #compositionEnd,#mermaid-svg-8Wlnt0sI42bDkciS .composition{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-8Wlnt0sI42bDkciS #dependencyStart,#mermaid-svg-8Wlnt0sI42bDkciS .dependency{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-8Wlnt0sI42bDkciS #dependencyStart,#mermaid-svg-8Wlnt0sI42bDkciS .dependency{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-8Wlnt0sI42bDkciS #extensionStart,#mermaid-svg-8Wlnt0sI42bDkciS .extension{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-8Wlnt0sI42bDkciS #extensionEnd,#mermaid-svg-8Wlnt0sI42bDkciS .extension{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-8Wlnt0sI42bDkciS #aggregationStart,#mermaid-svg-8Wlnt0sI42bDkciS .aggregation{fill:#ECECFF!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-8Wlnt0sI42bDkciS #aggregationEnd,#mermaid-svg-8Wlnt0sI42bDkciS .aggregation{fill:#ECECFF!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-8Wlnt0sI42bDkciS .edgeTerminals{font-size:11px;}#mermaid-svg-8Wlnt0sI42bDkciS :root{–mermaid-font-family:”trebuchet ms”,verdana,arial,sans-serif;}

sink

#level_t level_

+void log(const details::log_msg &msg)

+void flush()

+void set_pattern(const std::string &pattern)

+void set_formatter(std::unique_ptr sink_formatter)

+void set_level(level::level_enum log_level)

+level::level_enum level()

+bool should_log(level::level_enum msg_level)

formatter

+void format(const details::log_msg &msg, memory_buf_t &dest)

+std::unique_ptr clone()

其中sink中的log,flush,set_pattern,set_formatter为纯虚函数
formatter为虚类

base_sink

#mermaid-svg-gnD2euY2nTn3wFB0 {font-family:”trebuchet ms”,verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-gnD2euY2nTn3wFB0 .error-icon{fill:#552222;}#mermaid-svg-gnD2euY2nTn3wFB0 .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-gnD2euY2nTn3wFB0 .edge-thickness-normal{stroke-width:2px;}#mermaid-svg-gnD2euY2nTn3wFB0 .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-gnD2euY2nTn3wFB0 .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-gnD2euY2nTn3wFB0 .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-gnD2euY2nTn3wFB0 .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-gnD2euY2nTn3wFB0 .marker{fill:#333333;stroke:#333333;}#mermaid-svg-gnD2euY2nTn3wFB0 .marker.cross{stroke:#333333;}#mermaid-svg-gnD2euY2nTn3wFB0 svg{font-family:”trebuchet ms”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-gnD2euY2nTn3wFB0 g.classGroup text{fill:#9370DB;fill:#131300;stroke:none;font-family:”trebuchet ms”,verdana,arial,sans-serif;font-size:10px;}#mermaid-svg-gnD2euY2nTn3wFB0 g.classGroup text .title{font-weight:bolder;}#mermaid-svg-gnD2euY2nTn3wFB0 .nodeLabel,#mermaid-svg-gnD2euY2nTn3wFB0 .edgeLabel{color:#131300;}#mermaid-svg-gnD2euY2nTn3wFB0 .edgeLabel .label rect{fill:#ECECFF;}#mermaid-svg-gnD2euY2nTn3wFB0 .label text{fill:#131300;}#mermaid-svg-gnD2euY2nTn3wFB0 .edgeLabel .label span{background:#ECECFF;}#mermaid-svg-gnD2euY2nTn3wFB0 .classTitle{font-weight:bolder;}#mermaid-svg-gnD2euY2nTn3wFB0 .node rect,#mermaid-svg-gnD2euY2nTn3wFB0 .node circle,#mermaid-svg-gnD2euY2nTn3wFB0 .node ellipse,#mermaid-svg-gnD2euY2nTn3wFB0 .node polygon,#mermaid-svg-gnD2euY2nTn3wFB0 .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-gnD2euY2nTn3wFB0 .divider{stroke:#9370DB;stroke:1;}#mermaid-svg-gnD2euY2nTn3wFB0 g.clickable{cursor:pointer;}#mermaid-svg-gnD2euY2nTn3wFB0 g.classGroup rect{fill:#ECECFF;stroke:#9370DB;}#mermaid-svg-gnD2euY2nTn3wFB0 g.classGroup line{stroke:#9370DB;stroke-width:1;}#mermaid-svg-gnD2euY2nTn3wFB0 .classLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5;}#mermaid-svg-gnD2euY2nTn3wFB0 .classLabel .label{fill:#9370DB;font-size:10px;}#mermaid-svg-gnD2euY2nTn3wFB0 .relation{stroke:#333333;stroke-width:1;fill:none;}#mermaid-svg-gnD2euY2nTn3wFB0 .dashed-line{stroke-dasharray:3;}#mermaid-svg-gnD2euY2nTn3wFB0 #compositionStart,#mermaid-svg-gnD2euY2nTn3wFB0 .composition{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-gnD2euY2nTn3wFB0 #compositionEnd,#mermaid-svg-gnD2euY2nTn3wFB0 .composition{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-gnD2euY2nTn3wFB0 #dependencyStart,#mermaid-svg-gnD2euY2nTn3wFB0 .dependency{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-gnD2euY2nTn3wFB0 #dependencyStart,#mermaid-svg-gnD2euY2nTn3wFB0 .dependency{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-gnD2euY2nTn3wFB0 #extensionStart,#mermaid-svg-gnD2euY2nTn3wFB0 .extension{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-gnD2euY2nTn3wFB0 #extensionEnd,#mermaid-svg-gnD2euY2nTn3wFB0 .extension{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-gnD2euY2nTn3wFB0 #aggregationStart,#mermaid-svg-gnD2euY2nTn3wFB0 .aggregation{fill:#ECECFF!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-gnD2euY2nTn3wFB0 #aggregationEnd,#mermaid-svg-gnD2euY2nTn3wFB0 .aggregation{fill:#ECECFF!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-gnD2euY2nTn3wFB0 .edgeTerminals{font-size:11px;}#mermaid-svg-gnD2euY2nTn3wFB0 :root{–mermaid-font-family:”trebuchet ms”,verdana,arial,sans-serif;}

base_sink

#unique_ptr formatter_

#Mutex mutex_

#void sink_it_(const details::log_msg &msg)

#void flush_()

#void set_pattern_(const std::string &pattern)

#void set_formatter_(std::unique_ptr sink_formatter)

sink

base_sink模板类继承了sink,模板参数提供了同步机制。其实现了基类中的log,flush,set_pattern和set_formatter抽象方法,log方法中会调用抽象方法sink_it_,flush方法调用抽象方法flush_

template <typename Mutex>
void SPDLOG_INLINE spdlog::sinks::base_sink<Mutex>::log(const details::log_msg &msg) {
    std::lock_guard<Mutex> lock(mutex_);
    sink_it_(msg);
}

template <typename Mutex>
void SPDLOG_INLINE spdlog::sinks::base_sink<Mutex>::flush() {
    std::lock_guard<Mutex> lock(mutex_);
    flush_();
}

template <typename Mutex>
void SPDLOG_INLINE spdlog::sinks::base_sink<Mutex>::set_pattern(const std::string &pattern) {
    std::lock_guard<Mutex> lock(mutex_);
    set_pattern_(pattern);
}

template <typename Mutex>
void SPDLOG_INLINE
spdlog::sinks::base_sink<Mutex>::set_formatter(std::unique_ptr<spdlog::formatter> sink_formatter) {
    std::lock_guard<Mutex> lock(mutex_);
    set_formatter_(std::move(sink_formatter));
}
本站无任何商业行为
个人在线分享 » spdlog源码解析
E-->