spdlog/include/spdlog/sinks/ostream_sink.h

56 lines
1.3 KiB
C
Raw Normal View History

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