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" |
---|
14 | #include "spdlog/details/null_mutex.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 | { |
---|
84 | sink->set_formatter(base_sink<Mutex>::formatter_->clone()); |
---|
85 | } |
---|
86 | } |
---|
87 | std::vector<std::shared_ptr<sink>> sinks_; |
---|
88 | }; |
---|
89 | |
---|
90 | using dist_sink_mt = dist_sink<std::mutex>; |
---|
91 | using dist_sink_st = dist_sink<details::null_mutex>; |
---|
92 | |
---|
93 | } // namespace sinks |
---|
94 | } // namespace spdlog |
---|