spdlog/include/spdlog/sinks/ostream_sink.h

50 lines
1.1 KiB
C
Raw Normal View History

2016-04-20 04:57:49 -04:00
//
// Copyright(c) 2015 Gabi Melman.
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
//
#pragma once
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 {
2018-03-16 11:35:56 -04:00
template<class Mutex>
class ostream_sink : 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)
{
}
ostream_sink(const ostream_sink &) = delete;
ostream_sink &operator=(const ostream_sink &) = delete;
2016-04-20 04:57:49 -04:00
protected:
2018-03-09 08:26:33 -05:00
void _sink_it(const details::log_msg &msg) override
2016-04-20 04:57:49 -04:00
{
_ostream.write(msg.formatted.data(), msg.formatted.size());
if (_force_flush)
_ostream.flush();
}
2017-05-20 20:48:54 -04:00
void _flush() override
2016-04-20 04:57:49 -04:00
{
_ostream.flush();
}
2018-03-09 08:26:33 -05:00
std::ostream &_ostream;
2016-04-20 04:57:49 -04:00
bool _force_flush;
};
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