dist sink cleanup

This commit is contained in:
gabime 2016-08-20 14:52:26 +03:00
parent efc8de6d64
commit c4298a989e

View File

@ -12,9 +12,10 @@
#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
@ -29,40 +30,33 @@ public:
virtual ~dist_sink() = default;
protected:
std::vector<std::shared_ptr<sink>> _sinks;
void _sink_it(const details::log_msg& msg) override
{
for (auto iter = _sinks.begin(); iter != _sinks.end(); iter++)
(*iter)->log(msg);
for (auto &sink : _sinks)
sink->log(msg);
}
std::vector<std::shared_ptr<sink>> _sinks;
public:
void flush() override
{
std::lock_guard<Mutex> lock(base_sink<Mutex>::_mutex);
for (auto iter = _sinks.begin(); iter != _sinks.end(); iter++)
(*iter)->flush();
std::lock_guard<Mutex> lock(_mutex);
for (auto &sink : _sinks)
sink->flush();
}
void add_sink(std::shared_ptr<sink> sink)
{
std::lock_guard<Mutex> lock(base_sink<Mutex>::_mutex);
if (sink &&
_sinks.end() == std::find(_sinks.begin(), _sinks.end(), sink))
{
_sinks.push_back(sink);
}
std::lock_guard<Mutex> lock(_mutex);
_sinks.push_back(sink);
}
void remove_sink(std::shared_ptr<sink> sink)
{
std::lock_guard<Mutex> lock(base_sink<Mutex>::_mutex);
auto pos = std::find(_sinks.begin(), _sinks.end(), sink);
if (pos != _sinks.end())
{
_sinks.erase(pos);
}
std::lock_guard<Mutex> lock(_mutex);
_sinks.erase(std::remove(_sinks.begin(), _sinks.end(), sink), _sinks.end());
}
};