2016-04-20 16:57:49 +08:00
|
|
|
//
|
|
|
|
// Copyright(c) 2015 Gabi Melman.
|
|
|
|
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
|
|
|
|
//
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
//
|
2017-04-07 08:16:49 +08:00
|
|
|
// base sink templated over a mutex (either dummy or real)
|
2018-06-11 03:59:17 +08:00
|
|
|
// concrete implementation should only override the sink_it_ method.
|
2016-08-01 05:38:59 +08:00
|
|
|
// all locking is taken care of here so no locking needed by the implementers..
|
2016-04-20 16:57:49 +08:00
|
|
|
//
|
|
|
|
|
2018-04-29 06:31:09 +08:00
|
|
|
#include "spdlog/common.h"
|
|
|
|
#include "spdlog/details/log_msg.h"
|
|
|
|
#include "spdlog/formatter.h"
|
|
|
|
#include "spdlog/sinks/sink.h"
|
2016-04-20 16:57:49 +08:00
|
|
|
|
2018-03-17 18:47:46 +08:00
|
|
|
namespace spdlog {
|
|
|
|
namespace sinks {
|
2018-03-16 23:35:56 +08:00
|
|
|
template<class Mutex>
|
|
|
|
class base_sink : public sink
|
2016-04-20 16:57:49 +08:00
|
|
|
{
|
|
|
|
public:
|
2018-06-24 06:32:39 +08:00
|
|
|
base_sink()
|
|
|
|
: sink()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
base_sink(const std::string &formatter_pattern)
|
|
|
|
: sink(formatter_pattern)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
base_sink(std::unique_ptr<spdlog::formatter> sink_formatter)
|
|
|
|
: sink(std::move(sink_formatter))
|
|
|
|
{
|
|
|
|
}
|
2016-04-20 16:57:49 +08:00
|
|
|
|
2018-03-09 21:26:33 +08:00
|
|
|
base_sink(const base_sink &) = delete;
|
|
|
|
base_sink &operator=(const base_sink &) = delete;
|
2016-04-20 16:57:49 +08:00
|
|
|
|
2018-03-09 21:26:33 +08:00
|
|
|
void log(const details::log_msg &msg) SPDLOG_FINAL override
|
2016-04-20 16:57:49 +08:00
|
|
|
{
|
2018-06-11 03:59:17 +08:00
|
|
|
std::lock_guard<Mutex> lock(mutex_);
|
2018-06-24 06:32:39 +08:00
|
|
|
fmt::memory_buffer formatted;
|
|
|
|
formatter_->format(msg, formatted);
|
|
|
|
sink_it_(msg, formatted);
|
2016-04-20 16:57:49 +08:00
|
|
|
}
|
2018-02-25 06:56:56 +08:00
|
|
|
|
2017-06-29 15:51:44 +08:00
|
|
|
void flush() SPDLOG_FINAL override
|
|
|
|
{
|
2018-06-11 03:59:17 +08:00
|
|
|
std::lock_guard<Mutex> lock(mutex_);
|
|
|
|
flush_();
|
2017-06-29 15:51:44 +08:00
|
|
|
}
|
2016-04-20 16:57:49 +08:00
|
|
|
|
|
|
|
protected:
|
2018-06-24 06:32:39 +08:00
|
|
|
virtual void sink_it_(const details::log_msg &msg, const fmt::memory_buffer &formatted) = 0;
|
2018-06-11 03:59:17 +08:00
|
|
|
virtual void flush_() = 0;
|
|
|
|
Mutex mutex_;
|
2016-04-20 16:57:49 +08:00
|
|
|
};
|
2018-03-17 18:47:46 +08:00
|
|
|
} // namespace sinks
|
|
|
|
} // namespace spdlog
|