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