Files
spdlog/include/spdlog/sinks/dist_sink.h

98 lines
2.4 KiB
C
Raw Normal View History

2019-06-04 00:09:16 +03:00
// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
2016-04-20 11:57:49 +03:00
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
#pragma once
#include "base_sink.h"
#include <spdlog/details/log_msg.h>
#include <spdlog/details/null_mutex.h>
#include <spdlog/pattern_formatter.h>
2016-04-20 11:57:49 +03:00
#include <algorithm>
#include <memory>
2018-03-09 15:26:33 +02:00
#include <mutex>
2016-04-20 11:57:49 +03:00
#include <vector>
2018-07-21 23:48:07 +03:00
// Distribution sink (mux). Stores a vector of sinks which get called when log
// is called
2016-08-20 14:52:26 +03:00
2018-03-17 12:47:46 +02:00
namespace spdlog {
namespace sinks {
2018-07-07 13:22:09 +03:00
2018-07-07 13:22:43 +03:00
template<typename Mutex>
class dist_sink : public base_sink<Mutex>
2016-04-20 11:57:49 +03:00
{
2016-08-22 20:54:18 +03:00
public:
2018-07-07 13:22:09 +03:00
dist_sink() = default;
2019-12-01 01:28:28 +02:00
explicit dist_sink(std::vector<std::shared_ptr<sink>> sinks)
: sinks_(sinks)
{}
2018-03-09 15:26:33 +02:00
dist_sink(const dist_sink &) = delete;
dist_sink &operator=(const dist_sink &) = delete;
2016-04-20 11:57:49 +03:00
void add_sink(std::shared_ptr<sink> sink)
{
std::lock_guard<Mutex> lock(base_sink<Mutex>::mutex_);
sinks_.push_back(sink);
}
void remove_sink(std::shared_ptr<sink> sink)
2016-04-20 11:57:49 +03:00
{
std::lock_guard<Mutex> lock(base_sink<Mutex>::mutex_);
sinks_.erase(std::remove(sinks_.begin(), sinks_.end(), sink), sinks_.end());
}
void set_sinks(std::vector<std::shared_ptr<sink>> sinks)
{
std::lock_guard<Mutex> lock(base_sink<Mutex>::mutex_);
sinks_ = std::move(sinks);
}
2019-08-25 18:05:47 +03:00
std::vector<std::shared_ptr<sink>> &sinks()
2019-08-25 17:24:17 +03:00
{
return sinks_;
}
protected:
void sink_it_(const details::log_msg &msg) override
{
for (auto &sink : sinks_)
2016-09-15 00:38:21 +03:00
{
2018-03-09 15:26:33 +02:00
if (sink->should_log(msg.level))
2016-09-15 00:38:21 +03:00
{
sink->log(msg);
}
}
2016-04-20 11:57:49 +03:00
}
2016-08-20 14:55:50 +03:00
void flush_() override
{
for (auto &sink : sinks_)
2018-09-26 15:43:23 +03:00
{
sink->flush();
2018-09-26 15:43:23 +03:00
}
}
void set_pattern_(const std::string &pattern) override
{
2018-10-10 11:23:25 +02:00
set_formatter_(details::make_unique<spdlog::pattern_formatter>(pattern));
2018-09-26 15:43:23 +03:00
}
void set_formatter_(std::unique_ptr<spdlog::formatter> sink_formatter) override
{
base_sink<Mutex>::formatter_ = std::move(sink_formatter);
for (auto &sink : sinks_)
{
2018-09-26 23:48:59 +03:00
sink->set_formatter(base_sink<Mutex>::formatter_->clone());
2018-09-26 15:43:23 +03:00
}
}
2018-07-07 13:22:09 +03:00
std::vector<std::shared_ptr<sink>> sinks_;
2016-04-20 11:57:49 +03:00
};
2018-02-24 22:35:09 +01:00
using dist_sink_mt = dist_sink<std::mutex>;
using dist_sink_st = dist_sink<details::null_mutex>;
2018-03-17 12:47:46 +02:00
} // namespace sinks
} // namespace spdlog