mirror of
https://github.com/gabime/spdlog.git
synced 2025-01-26 15:39:03 +08:00
fixed dist_sink.h
This commit is contained in:
parent
72f4fae207
commit
a9a7309001
@ -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 @@
|
|||||||
|
|
||||||
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:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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>;
|
||||||
|
Loading…
Reference in New Issue
Block a user