1 | // |
---|
2 | // Copyright(c) 2015 Gabi Melman. |
---|
3 | // Distributed under the MIT License (http://opensource.org/licenses/MIT) |
---|
4 | // |
---|
5 | |
---|
6 | #pragma once |
---|
7 | // |
---|
8 | // base sink templated over a mutex (either dummy or real) |
---|
9 | // concrete implementation should override the sink_it_() and flush_() methods. |
---|
10 | // locking is taken care of in this class - no locking needed by the |
---|
11 | // implementers.. |
---|
12 | // |
---|
13 | |
---|
14 | #include "spdlog/common.h" |
---|
15 | #include "spdlog/details/log_msg.h" |
---|
16 | #include "spdlog/formatter.h" |
---|
17 | #include "spdlog/sinks/sink.h" |
---|
18 | |
---|
19 | namespace spdlog { |
---|
20 | namespace sinks { |
---|
21 | template<typename Mutex> |
---|
22 | class base_sink : public sink |
---|
23 | { |
---|
24 | public: |
---|
25 | base_sink() = default; |
---|
26 | base_sink(const base_sink &) = delete; |
---|
27 | base_sink &operator=(const base_sink &) = delete; |
---|
28 | |
---|
29 | void log(const details::log_msg &msg) final |
---|
30 | { |
---|
31 | std::lock_guard<Mutex> lock(mutex_); |
---|
32 | sink_it_(msg); |
---|
33 | } |
---|
34 | |
---|
35 | void flush() final |
---|
36 | { |
---|
37 | std::lock_guard<Mutex> lock(mutex_); |
---|
38 | flush_(); |
---|
39 | } |
---|
40 | |
---|
41 | void set_pattern(const std::string &pattern) final |
---|
42 | { |
---|
43 | std::lock_guard<Mutex> lock(mutex_); |
---|
44 | set_pattern_(pattern); |
---|
45 | } |
---|
46 | |
---|
47 | void set_formatter(std::unique_ptr<spdlog::formatter> sink_formatter) final |
---|
48 | { |
---|
49 | std::lock_guard<Mutex> lock(mutex_); |
---|
50 | set_formatter_(std::move(sink_formatter)); |
---|
51 | } |
---|
52 | |
---|
53 | protected: |
---|
54 | virtual void sink_it_(const details::log_msg &msg) = 0; |
---|
55 | virtual void flush_() = 0; |
---|
56 | |
---|
57 | virtual void set_pattern_(const std::string &pattern) |
---|
58 | { |
---|
59 | set_formatter_(details::make_unique<spdlog::pattern_formatter>(pattern)); |
---|
60 | } |
---|
61 | |
---|
62 | virtual void set_formatter_(std::unique_ptr<spdlog::formatter> sink_formatter) |
---|
63 | { |
---|
64 | formatter_ = std::move(sink_formatter); |
---|
65 | } |
---|
66 | Mutex mutex_; |
---|
67 | }; |
---|
68 | } // namespace sinks |
---|
69 | } // namespace spdlog |
---|