From 0cc35ad04f9c2997014d7cf62a12f697e79fb534 Mon Sep 17 00:00:00 2001 From: Arnur Nigmetov Date: Sat, 20 Jan 2018 19:11:29 +0100 Subject: Major rewrite, templatized version --- .../wasserstein/include/spdlog/sinks/dist_sink.h | 73 ++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 geom_matching/wasserstein/include/spdlog/sinks/dist_sink.h (limited to 'geom_matching/wasserstein/include/spdlog/sinks/dist_sink.h') diff --git a/geom_matching/wasserstein/include/spdlog/sinks/dist_sink.h b/geom_matching/wasserstein/include/spdlog/sinks/dist_sink.h new file mode 100644 index 0000000..4d4b6b6 --- /dev/null +++ b/geom_matching/wasserstein/include/spdlog/sinks/dist_sink.h @@ -0,0 +1,73 @@ +// +// Copyright (c) 2015 David Schury, Gabi Melman +// Distributed under the MIT License (http://opensource.org/licenses/MIT) +// + +#pragma once + +#include "spdlog/details/log_msg.h" +#include "spdlog/details/null_mutex.h" +#include "spdlog/sinks/base_sink.h" +#include "spdlog/sinks/sink.h" + +#include +#include +#include +#include + +// Distribution sink (mux). Stores a vector of sinks which get called when log is called + +namespace spdlog +{ +namespace sinks +{ +template +class dist_sink: public base_sink +{ +public: + explicit dist_sink() :_sinks() {} + dist_sink(const dist_sink&) = delete; + dist_sink& operator=(const dist_sink&) = delete; + virtual ~dist_sink() = default; + +protected: + std::vector> _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 + { + std::lock_guard lock(base_sink::_mutex); + for (auto &sink : _sinks) + sink->flush(); + } + +public: + + + void add_sink(std::shared_ptr sink) + { + std::lock_guard lock(base_sink::_mutex); + _sinks.push_back(sink); + } + + void remove_sink(std::shared_ptr sink) + { + std::lock_guard lock(base_sink::_mutex); + _sinks.erase(std::remove(_sinks.begin(), _sinks.end(), sink), _sinks.end()); + } +}; + +typedef dist_sink dist_sink_mt; +typedef dist_sink dist_sink_st; +} +} -- cgit v1.2.3