Agora  1.2.0
Agora project
dist_sink.h
Go to the documentation of this file.
1 // Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
2 // Distributed under the MIT License (http://opensource.org/licenses/MIT)
3 
4 #pragma once
5 
6 #include "base_sink.h"
10 
11 #include <algorithm>
12 #include <memory>
13 #include <mutex>
14 #include <vector>
15 
16 // Distribution sink (mux). Stores a vector of sinks which get called when log
17 // is called
18 
19 namespace spdlog {
20 namespace sinks {
21 
22 template<typename Mutex>
23 class dist_sink : public base_sink<Mutex>
24 {
25 public:
26  dist_sink() = default;
27  explicit dist_sink(std::vector<std::shared_ptr<sink>> sinks)
28  : sinks_(sinks)
29  {}
30 
31  dist_sink(const dist_sink &) = delete;
32  dist_sink &operator=(const dist_sink &) = delete;
33 
34  void add_sink(std::shared_ptr<sink> sink)
35  {
36  std::lock_guard<Mutex> lock(base_sink<Mutex>::mutex_);
37  sinks_.push_back(sink);
38  }
39 
40  void remove_sink(std::shared_ptr<sink> sink)
41  {
42  std::lock_guard<Mutex> lock(base_sink<Mutex>::mutex_);
43  sinks_.erase(std::remove(sinks_.begin(), sinks_.end(), sink), sinks_.end());
44  }
45 
46  void set_sinks(std::vector<std::shared_ptr<sink>> sinks)
47  {
48  std::lock_guard<Mutex> lock(base_sink<Mutex>::mutex_);
49  sinks_ = std::move(sinks);
50  }
51 
52  std::vector<std::shared_ptr<sink>> &sinks()
53  {
54  return sinks_;
55  }
56 
57 protected:
58  void sink_it_(const details::log_msg &msg) override
59  {
60  for (auto &sink : sinks_)
61  {
62  if (sink->should_log(msg.level))
63  {
64  sink->log(msg);
65  }
66  }
67  }
68 
69  void flush_() override
70  {
71  for (auto &sink : sinks_)
72  {
73  sink->flush();
74  }
75  }
76 
77  void set_pattern_(const std::string &pattern) override
78  {
79  set_formatter_(details::make_unique<spdlog::pattern_formatter>(pattern));
80  }
81 
82  void set_formatter_(std::unique_ptr<spdlog::formatter> sink_formatter) override
83  {
84  base_sink<Mutex>::formatter_ = std::move(sink_formatter);
85  for (auto &sink : sinks_)
86  {
88  }
89  }
90  std::vector<std::shared_ptr<sink>> sinks_;
91 };
92 
95 
96 } // namespace sinks
97 } // namespace spdlog
spdlog::sinks::sink
Definition: sink.h:12
spdlog::sinks::dist_sink
Definition: dist_sink.h:23
spdlog::sinks::dist_sink::set_formatter_
void set_formatter_(std::unique_ptr< spdlog::formatter > sink_formatter) override
Definition: dist_sink.h:82
spdlog::sinks::dist_sink::operator=
dist_sink & operator=(const dist_sink &)=delete
base_sink.h
spdlog::sinks::dist_sink::sinks
std::vector< std::shared_ptr< sink > > & sinks()
Definition: dist_sink.h:52
spdlog::sinks::dist_sink::set_sinks
void set_sinks(std::vector< std::shared_ptr< sink >> sinks)
Definition: dist_sink.h:46
spdlog::sinks::dist_sink::set_pattern_
void set_pattern_(const std::string &pattern) override
Definition: dist_sink.h:77
null_mutex.h
spdlog::sinks::sink::flush
virtual void flush()=0
spdlog::sinks::dist_sink::sinks_
std::vector< std::shared_ptr< sink > > sinks_
Definition: dist_sink.h:90
spdlog::sinks::sink::should_log
bool should_log(level::level_enum msg_level) const
Definition: sink-inl.h:12
spdlog
Definition: async.h:25
spdlog::sinks::base_sink
Definition: base_sink.h:19
spdlog::sinks::dist_sink::dist_sink
dist_sink()=default
spdlog::sinks::sink::log
virtual void log(const details::log_msg &msg)=0
spdlog::sinks::dist_sink::flush_
void flush_() override
Definition: dist_sink.h:69
spdlog::sinks::dist_sink::remove_sink
void remove_sink(std::shared_ptr< sink > sink)
Definition: dist_sink.h:40
spdlog::details::log_msg::level
level::level_enum level
Definition: log_msg.h:21
spdlog::details::log_msg
Definition: log_msg.h:11
spdlog::sinks::dist_sink::add_sink
void add_sink(std::shared_ptr< sink > sink)
Definition: dist_sink.h:34
pattern_formatter.h
spdlog::sinks::dist_sink::sink_it_
void sink_it_(const details::log_msg &msg) override
Definition: dist_sink.h:58
spdlog::details::os::remove
SPDLOG_INLINE int remove(const filename_t &filename) SPDLOG_NOEXCEPT
Definition: os-inl.h:163
spdlog::sinks::sink::set_formatter
virtual void set_formatter(std::unique_ptr< spdlog::formatter > sink_formatter)=0
log_msg.h
spdlog::sinks::dist_sink::dist_sink
dist_sink(std::vector< std::shared_ptr< sink >> sinks)
Definition: dist_sink.h:27