From e12c50a8a5ab36c0b3a4f97a2635381ea40355c0 Mon Sep 17 00:00:00 2001 From: David Schury Date: Thu, 17 Sep 2015 17:01:04 +0200 Subject: [PATCH 1/2] Add distributional sink Setting up dist_sink to a logger will inject a layer between the logger and (some of) it's sinks. Sinks can be added and removed to dist_sink dynamically. --- include/spdlog/sinks/dist_sink.h | 91 ++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 include/spdlog/sinks/dist_sink.h diff --git a/include/spdlog/sinks/dist_sink.h b/include/spdlog/sinks/dist_sink.h new file mode 100644 index 00000000..06728db7 --- /dev/null +++ b/include/spdlog/sinks/dist_sink.h @@ -0,0 +1,91 @@ +/*************************************************************************/ +/* spdlog - an extremely fast and easy to use c++11 logging library. */ +/* Copyright (c) 2015 David Schury. */ +/* Copyright (c) 2014 Gabi Melman. */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ + +#pragma once + +#include +#include +#include +#include + +#include "../details/log_msg.h" +#include "../details/null_mutex.h" +#include "./base_sink.h" +#include "./sink.h" + +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: + void _sink_it(const details::log_msg& msg) override + { + for (auto iter = _sinks.begin(); iter != _sinks.end(); iter++) + (*iter)->log(msg); + } + + std::vector> _sinks; + +public: + void flush() override + { + for (auto iter = _sinks.begin(); iter != _sinks.end(); iter++) + (*iter)->flush(); + } + + void add_sink(std::shared_ptr sink) + { + std::lock_guard lock(base_sink::_mutex); + if (sink && + _sinks.end() == std::find(_sinks.begin(), _sinks.end(), sink)) + { + _sinks.push_back(sink); + } + } + + void remove_sink(std::shared_ptr sink) + { + std::lock_guard lock(base_sink::_mutex); + auto pos = std::find(_sinks.begin(), _sinks.end(), sink); + if (pos != _sinks.end()) + { + _sinks.erase(pos); + } + } +}; + +typedef dist_sink dist_sink_mt; +typedef dist_sink dist_sink_st; +} +} From 2175b008142fa939bfc9f6eeeb10ede576b2c0b3 Mon Sep 17 00:00:00 2001 From: David Schury Date: Thu, 17 Sep 2015 18:58:13 +0200 Subject: [PATCH 2/2] Fixed thread unsafe flush method. --- include/spdlog/sinks/dist_sink.h | 1 + 1 file changed, 1 insertion(+) diff --git a/include/spdlog/sinks/dist_sink.h b/include/spdlog/sinks/dist_sink.h index 06728db7..45cb8d7a 100644 --- a/include/spdlog/sinks/dist_sink.h +++ b/include/spdlog/sinks/dist_sink.h @@ -60,6 +60,7 @@ protected: public: void flush() override { + std::lock_guard lock(base_sink::_mutex); for (auto iter = _sinks.begin(); iter != _sinks.end(); iter++) (*iter)->flush(); }