Commit a9a73090 authored by gabime's avatar gabime

fixed dist_sink.h

parent 72f4fae2
...@@ -7,7 +7,6 @@ ...@@ -7,7 +7,6 @@
#include "spdlog/details/log_msg.h" #include "spdlog/details/log_msg.h"
#include "spdlog/details/null_mutex.h" #include "spdlog/details/null_mutex.h"
#include "spdlog/sink/base_sink.h"
#include <algorithm> #include <algorithm>
#include <memory> #include <memory>
...@@ -18,22 +17,18 @@ ...@@ -18,22 +17,18 @@
namespace spdlog { namespace spdlog {
namespace sinks { namespace sinks {
template<class Mutex>
class dist_sink : public base_sink<Mutex> template <typename Mutex>
class dist_sink : public sink
{ {
public: public:
explicit dist_sink() dist_sink() = default;
: sinks_()
{
}
dist_sink(const dist_sink &) = delete; dist_sink(const dist_sink &) = delete;
dist_sink &operator=(const dist_sink &) = delete; dist_sink &operator=(const dist_sink &) = delete;
protected: void log(const details::log_msg &msg) SPDLOG_FINAL override
std::vector<std::shared_ptr<sink>> sinks_;
void sink_it_(const details::log_msg &msg) override
{ {
std::lock_guard<Mutex> lock(mutex_);
for (auto &sink : sinks_) for (auto &sink : sinks_)
{ {
if (sink->should_log(msg.level)) if (sink->should_log(msg.level))
...@@ -43,24 +38,29 @@ protected: ...@@ -43,24 +38,29 @@ protected:
} }
} }
void flush_() override void flush() SPDLOG_FINAL override
{ {
std::lock_guard<Mutex> lock(mutex_);
for (auto &sink : sinks_) for (auto &sink : sinks_)
sink->flush(); sink->flush();
} }
public:
void add_sink(std::shared_ptr<sink> sink) void add_sink(std::shared_ptr<sink> sink)
{ {
std::lock_guard<Mutex> lock(base_sink<Mutex>::mutex_); std::lock_guard<Mutex> lock(mutex_);
sinks_.push_back(sink); sinks_.push_back(sink);
} }
void remove_sink(std::shared_ptr<sink> sink) void remove_sink(std::shared_ptr<sink> sink)
{ {
std::lock_guard<Mutex> lock(base_sink<Mutex>::mutex_); std::lock_guard<Mutex> lock(mutex_);
sinks_.erase(std::remove(sinks_.begin(), sinks_.end(), sink), sinks_.end()); sinks_.erase(std::remove(sinks_.begin(), sinks_.end(), sink), sinks_.end());
} }
private:
Mutex mutex_;
std::vector<std::shared_ptr<sink>> sinks_;
}; };
using dist_sink_mt = dist_sink<std::mutex>; using dist_sink_mt = dist_sink<std::mutex>;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment