Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-12-17 10:29:02

0001 // Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
0002 // Distributed under the MIT License (http://opensource.org/licenses/MIT)
0003 
0004 #pragma once
0005 
0006 #include "base_sink.h"
0007 #include <spdlog/details/log_msg.h>
0008 #include <spdlog/details/null_mutex.h>
0009 #include <spdlog/pattern_formatter.h>
0010 
0011 #include <algorithm>
0012 #include <memory>
0013 #include <mutex>
0014 #include <vector>
0015 
0016 // Distribution sink (mux). Stores a vector of sinks which get called when log
0017 // is called
0018 
0019 namespace spdlog {
0020 namespace sinks {
0021 
0022 template <typename Mutex>
0023 class dist_sink : public base_sink<Mutex> {
0024 public:
0025     dist_sink() = default;
0026     explicit dist_sink(std::vector<std::shared_ptr<sink>> sinks)
0027         : sinks_(sinks) {}
0028 
0029     dist_sink(const dist_sink &) = delete;
0030     dist_sink &operator=(const dist_sink &) = delete;
0031 
0032     void add_sink(std::shared_ptr<sink> sub_sink) {
0033         std::lock_guard<Mutex> lock(base_sink<Mutex>::mutex_);
0034         sinks_.push_back(sub_sink);
0035     }
0036 
0037     void remove_sink(std::shared_ptr<sink> sub_sink) {
0038         std::lock_guard<Mutex> lock(base_sink<Mutex>::mutex_);
0039         sinks_.erase(std::remove(sinks_.begin(), sinks_.end(), sub_sink), sinks_.end());
0040     }
0041 
0042     void set_sinks(std::vector<std::shared_ptr<sink>> sinks) {
0043         std::lock_guard<Mutex> lock(base_sink<Mutex>::mutex_);
0044         sinks_ = std::move(sinks);
0045     }
0046 
0047     std::vector<std::shared_ptr<sink>> &sinks() { return sinks_; }
0048 
0049 protected:
0050     void sink_it_(const details::log_msg &msg) override {
0051         for (auto &sub_sink : sinks_) {
0052             if (sub_sink->should_log(msg.level)) {
0053                 sub_sink->log(msg);
0054             }
0055         }
0056     }
0057 
0058     void flush_() override {
0059         for (auto &sub_sink : sinks_) {
0060             sub_sink->flush();
0061         }
0062     }
0063 
0064     void set_pattern_(const std::string &pattern) override {
0065         set_formatter_(details::make_unique<spdlog::pattern_formatter>(pattern));
0066     }
0067 
0068     void set_formatter_(std::unique_ptr<spdlog::formatter> sink_formatter) override {
0069         base_sink<Mutex>::formatter_ = std::move(sink_formatter);
0070         for (auto &sub_sink : sinks_) {
0071             sub_sink->set_formatter(base_sink<Mutex>::formatter_->clone());
0072         }
0073     }
0074     std::vector<std::shared_ptr<sink>> sinks_;
0075 };
0076 
0077 using dist_sink_mt = dist_sink<std::mutex>;
0078 using dist_sink_st = dist_sink<details::null_mutex>;
0079 
0080 }  // namespace sinks
0081 }  // namespace spdlog