spdlog/include/spdlog/sinks/ostream_sink.h

50 lines
1.1 KiB
C
Raw Normal View History

2016-04-20 11:57:49 +03:00
//
// Copyright(c) 2015 Gabi Melman.
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
//
#pragma once
2018-04-29 01:31:09 +03:00
#include "spdlog/details/null_mutex.h"
#include "spdlog/sinks/base_sink.h"
2016-04-20 11:57:49 +03:00
#include <mutex>
2018-03-09 15:26:33 +02:00
#include <ostream>
2016-04-20 11:57:49 +03:00
2018-03-17 12:47:46 +02:00
namespace spdlog {
namespace sinks {
2018-03-16 17:35:56 +02:00
template<class Mutex>
class ostream_sink : public base_sink<Mutex>
2016-04-20 11:57:49 +03:00
{
public:
2018-03-09 15:26:33 +02:00
explicit ostream_sink(std::ostream &os, bool force_flush = false)
: ostream_(os)
, force_flush_(force_flush)
2018-03-09 15:26:33 +02:00
{
}
ostream_sink(const ostream_sink &) = delete;
ostream_sink &operator=(const ostream_sink &) = delete;
2016-04-20 11:57:49 +03:00
protected:
2018-06-24 01:32:39 +03:00
void sink_it_(const details::log_msg &msg, const fmt::memory_buffer &formatted) override
2016-04-20 11:57:49 +03:00
{
2018-06-24 01:32:39 +03:00
ostream_.write(formatted.data(), formatted.size());
if (force_flush_)
ostream_.flush();
2016-04-20 11:57:49 +03:00
}
void flush_() override
2016-04-20 11:57:49 +03:00
{
ostream_.flush();
2016-04-20 11:57:49 +03:00
}
std::ostream &ostream_;
bool force_flush_;
2016-04-20 11:57:49 +03:00
};
2018-02-24 22:35:09 +01:00
using ostream_sink_mt = ostream_sink<std::mutex>;
using ostream_sink_st = ostream_sink<details::null_mutex>;
2018-03-17 12:47:46 +02:00
} // namespace sinks
} // namespace spdlog