spdlog/include/spdlog/sinks/stdout_sinks.h

76 lines
1.6 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
#include "../details/null_mutex.h"
#include "base_sink.h"
2016-04-20 11:57:49 +03:00
#include <cstdio>
#include <memory>
#include <mutex>
2018-03-09 15:26:33 +02:00
namespace spdlog { namespace sinks {
2016-04-20 11:57:49 +03:00
2018-03-09 15:26:33 +02:00
template <class Mutex> class stdout_sink SPDLOG_FINAL : public base_sink<Mutex>
2016-10-12 23:08:44 +03:00
{
using MyType = stdout_sink<Mutex>;
2018-02-25 01:25:15 +01:00
2016-10-12 23:08:44 +03:00
public:
2018-02-25 01:25:15 +01:00
explicit stdout_sink() = default;
2016-10-12 23:08:44 +03:00
static std::shared_ptr<MyType> instance()
{
static std::shared_ptr<MyType> instance = std::make_shared<MyType>();
return instance;
}
2018-02-25 01:25:15 +01:00
2017-05-21 03:43:41 +03:00
protected:
2018-03-09 15:26:33 +02:00
void _sink_it(const details::log_msg &msg) override
2016-10-12 23:08:44 +03:00
{
fwrite(msg.formatted.data(), sizeof(char), msg.formatted.size(), stdout);
2017-05-21 03:48:54 +03:00
_flush();
2016-10-12 23:08:44 +03:00
}
2016-04-20 11:57:49 +03:00
2017-05-21 03:43:41 +03:00
void _flush() override
2016-10-12 23:08:44 +03:00
{
fflush(stdout);
}
};
2016-04-20 11:57:49 +03:00
2018-02-24 22:35:09 +01:00
using stdout_sink_mt = stdout_sink<std::mutex>;
using stdout_sink_st = stdout_sink<details::null_mutex>;
2016-04-20 11:57:49 +03:00
2018-03-09 15:26:33 +02:00
template <class Mutex> class stderr_sink SPDLOG_FINAL : public base_sink<Mutex>
2016-10-12 23:08:44 +03:00
{
using MyType = stderr_sink<Mutex>;
2018-02-25 01:25:15 +01:00
2016-10-12 23:08:44 +03:00
public:
2018-02-25 01:25:15 +01:00
explicit stderr_sink() = default;
2018-02-24 23:56:56 +01:00
2016-10-12 23:08:44 +03:00
static std::shared_ptr<MyType> instance()
{
static std::shared_ptr<MyType> instance = std::make_shared<MyType>();
return instance;
}
2018-02-24 23:56:56 +01:00
2017-05-21 03:43:41 +03:00
protected:
2018-03-09 15:26:33 +02:00
void _sink_it(const details::log_msg &msg) override
2016-10-12 23:08:44 +03:00
{
fwrite(msg.formatted.data(), sizeof(char), msg.formatted.size(), stderr);
2017-05-21 03:48:54 +03:00
_flush();
2016-10-12 23:08:44 +03:00
}
2016-04-20 11:57:49 +03:00
2017-05-21 03:43:41 +03:00
void _flush() override
2016-10-12 23:08:44 +03:00
{
fflush(stderr);
}
};
2016-04-20 11:57:49 +03:00
2018-02-24 22:35:09 +01:00
using stderr_sink_mt = stderr_sink<std::mutex>;
using stderr_sink_st = stderr_sink<details::null_mutex>;
2018-03-09 15:26:33 +02:00
}} // namespace spdlog::sinks