diff options
author | Jörg Frings-Fürst <debian@jff-webhosting.net> | 2018-06-27 16:59:37 +0200 |
---|---|---|
committer | Jörg Frings-Fürst <debian@jff-webhosting.net> | 2018-06-27 16:59:37 +0200 |
commit | 8111b77e95b083137faf888aeb5892073adf7ab4 (patch) | |
tree | ed3896dfd2c2bbd9cc41f0bfcab1ee2a41c1cc77 /lib/spdlog/sinks/dist_sink.h | |
parent | e767091d649df311dd221e243ab2ce54776ae779 (diff) |
New upstream version 2.0.0upstream/2.0.0
Diffstat (limited to 'lib/spdlog/sinks/dist_sink.h')
-rw-r--r-- | lib/spdlog/sinks/dist_sink.h | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/lib/spdlog/sinks/dist_sink.h b/lib/spdlog/sinks/dist_sink.h new file mode 100644 index 0000000..b4a7b6a --- /dev/null +++ b/lib/spdlog/sinks/dist_sink.h @@ -0,0 +1,77 @@ +// +// Copyright (c) 2015 David Schury, Gabi Melman +// Distributed under the MIT License (http://opensource.org/licenses/MIT) +// + +#pragma once + +#include "../details/log_msg.h" +#include "../details/null_mutex.h" +#include "base_sink.h" +#include "sink.h" + +#include <algorithm> +#include <memory> +#include <mutex> +#include <vector> + +// Distribution sink (mux). Stores a vector of sinks which get called when log is called + +namespace spdlog { +namespace sinks { +template<class Mutex> +class dist_sink : public base_sink<Mutex> +{ +public: + explicit dist_sink() + : _sinks() + { + } + dist_sink(const dist_sink &) = delete; + dist_sink &operator=(const dist_sink &) = delete; + +protected: + std::vector<std::shared_ptr<sink>> _sinks; + + void _sink_it(const details::log_msg &msg) override + { + for (auto &sink : _sinks) + { + if (sink->should_log(msg.level)) + { + sink->log(msg); + } + } + } + + void _flush() override + { + for (auto &sink : _sinks) + sink->flush(); + } + +public: + void add_sink(std::shared_ptr<sink> sink) + { + std::lock_guard<Mutex> lock(base_sink<Mutex>::_mutex); + _sinks.push_back(sink); + } + + void remove_sink(std::shared_ptr<sink> sink) + { + std::lock_guard<Mutex> lock(base_sink<Mutex>::_mutex); + _sinks.erase(std::remove(_sinks.begin(), _sinks.end(), sink), _sinks.end()); + } + + void remove_all_sinks() + { + std::lock_guard<Mutex> lock(base_sink<Mutex>::_mutex); + _sinks.clear(); + } +}; + +using dist_sink_mt = dist_sink<std::mutex>; +using dist_sink_st = dist_sink<details::null_mutex>; + +} // namespace sinks +} // namespace spdlog |