2019-06-04 05:09:16 +08:00
|
|
|
// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
|
2016-04-20 16:57:49 +08:00
|
|
|
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2018-04-29 06:31:09 +08:00
|
|
|
#include "spdlog/details/null_mutex.h"
|
|
|
|
#include "spdlog/sinks/base_sink.h"
|
2016-04-20 16:57:49 +08:00
|
|
|
|
|
|
|
#include <mutex>
|
2018-03-09 21:26:33 +08:00
|
|
|
#include <ostream>
|
2016-04-20 16:57:49 +08:00
|
|
|
|
2018-03-17 18:47:46 +08:00
|
|
|
namespace spdlog {
|
|
|
|
namespace sinks {
|
2018-07-14 21:21:53 +08:00
|
|
|
template<typename Mutex>
|
2018-09-26 19:45:38 +08:00
|
|
|
class ostream_sink final : public base_sink<Mutex>
|
2016-04-20 16:57:49 +08:00
|
|
|
{
|
|
|
|
public:
|
2018-03-09 21:26:33 +08:00
|
|
|
explicit ostream_sink(std::ostream &os, bool force_flush = false)
|
2018-06-11 03:59:17 +08:00
|
|
|
: ostream_(os)
|
|
|
|
, force_flush_(force_flush)
|
2019-05-12 05:22:39 +08:00
|
|
|
{}
|
2018-03-09 21:26:33 +08:00
|
|
|
ostream_sink(const ostream_sink &) = delete;
|
|
|
|
ostream_sink &operator=(const ostream_sink &) = delete;
|
2016-04-20 16:57:49 +08:00
|
|
|
|
|
|
|
protected:
|
2018-07-14 21:21:53 +08:00
|
|
|
void sink_it_(const details::log_msg &msg) override
|
2016-04-20 16:57:49 +08:00
|
|
|
{
|
2019-08-28 23:46:09 +08:00
|
|
|
memory_buf_t formatted;
|
2019-06-28 04:56:37 +08:00
|
|
|
base_sink<Mutex>::formatter_->format(msg, formatted);
|
2018-08-17 05:32:13 +08:00
|
|
|
ostream_.write(formatted.data(), static_cast<std::streamsize>(formatted.size()));
|
2018-06-11 03:59:17 +08:00
|
|
|
if (force_flush_)
|
2018-10-05 20:19:03 +08:00
|
|
|
{
|
2018-06-11 03:59:17 +08:00
|
|
|
ostream_.flush();
|
2018-10-05 20:19:03 +08:00
|
|
|
}
|
2016-04-20 16:57:49 +08:00
|
|
|
}
|
|
|
|
|
2018-06-11 03:59:17 +08:00
|
|
|
void flush_() override
|
2016-04-20 16:57:49 +08:00
|
|
|
{
|
2018-06-11 03:59:17 +08:00
|
|
|
ostream_.flush();
|
2016-04-20 16:57:49 +08:00
|
|
|
}
|
|
|
|
|
2018-06-11 03:59:17 +08:00
|
|
|
std::ostream &ostream_;
|
|
|
|
bool force_flush_;
|
2016-04-20 16:57:49 +08:00
|
|
|
};
|
|
|
|
|
2018-02-25 05:35:09 +08:00
|
|
|
using ostream_sink_mt = ostream_sink<std::mutex>;
|
|
|
|
using ostream_sink_st = ostream_sink<details::null_mutex>;
|
|
|
|
|
2018-03-17 18:47:46 +08:00
|
|
|
} // namespace sinks
|
|
|
|
} // namespace spdlog
|