9 #include "quill/core/Attributes.h" 10 #include "quill/core/QuillError.h" 11 #include "quill/core/Spinlock.h" 17 #include <type_traits> 33 explicit SinkInfo() =
default;
34 SinkInfo(std::string sid, std::weak_ptr<Sink> sptr)
35 : sink_id(static_cast<std::string&&>(sid)), sink_ptr(
static_cast<std::weak_ptr<Sink>&&
>(sptr)) {};
38 std::weak_ptr<Sink> sink_ptr;
46 QUILL_EXPORT
static SinkManager& instance() noexcept
53 QUILL_NODISCARD std::shared_ptr<Sink> get_sink(std::string
const& sink_name)
const 58 std::shared_ptr<Sink> sink = _find_sink(sink_name);
60 if (QUILL_UNLIKELY(!sink))
62 QUILL_THROW(
QuillError{
"Sink with name \"" + sink_name +
"\" does not exist"});
69 template <
typename TSink,
typename... Args>
70 std::shared_ptr<Sink> create_or_get_sink(std::string
const& sink_name, Args&&... args)
72 static_assert(std::is_base_of_v<Sink, TSink>,
"TSink must derive from Sink");
77 std::shared_ptr<Sink> sink = _find_sink(sink_name);
81 if constexpr (std::disjunction_v<std::is_same<FileSink, TSink>, std::is_base_of<FileSink, TSink>>)
83 sink = std::make_shared<TSink>(sink_name,
static_cast<Args&&
>(args)...);
87 sink = std::make_shared<TSink>(
static_cast<Args&&
>(args)...);
90 _insert_sink(sink_name, sink);
97 uint32_t cleanup_unused_sinks()
104 for (
auto it = _sinks.begin(); it != _sinks.end();)
106 if (it->sink_ptr.expired())
108 it = _sinks.erase(it);
125 void _insert_sink(std::string
const& sink_name, std::shared_ptr<Sink>
const& sink)
128 std::lower_bound(_sinks.begin(), _sinks.end(), sink_name,
129 [](SinkInfo
const& elem, std::string
const& b) {
return elem.sink_id < b; });
131 _sinks.insert(search_it, SinkInfo{sink_name, sink});
135 QUILL_NODISCARD std::shared_ptr<Sink> _find_sink(std::string
const& target)
const noexcept
137 std::shared_ptr<Sink> sink;
140 std::lower_bound(_sinks.begin(), _sinks.end(), target,
141 [](SinkInfo
const& elem, std::string
const& b) {
return elem.sink_id < b; });
143 if (search_it != std::end(_sinks) && search_it->sink_id == target)
145 sink = search_it->sink_ptr.lock();
152 std::vector<SinkInfo> _sinks;
FileSink Writes the log messages to a file.
Definition: FileSink.h:214
Base class for sinks.
Definition: Sink.h:40
Setups a signal handler to handle fatal signals.
Definition: BackendManager.h:24
Definition: Spinlock.h:18
custom exception
Definition: QuillError.h:45
Definition: Spinlock.h:58
Definition: SinkManager.h:28