2019-05-11 20:06:17 +03:00
|
|
|
// Copyright(c) 2015-present Gabi Melman & spdlog contributors.
|
2016-04-20 11:57:49 +03:00
|
|
|
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2018-10-12 03:04:55 +03:00
|
|
|
#ifndef SPDLOG_H
|
2019-01-17 17:09:35 +02:00
|
|
|
#include "spdlog/spdlog.h"
|
2018-10-12 03:04:55 +03:00
|
|
|
#endif
|
|
|
|
|
2018-07-07 14:04:31 +03:00
|
|
|
#include "spdlog/details/console_globals.h"
|
2018-04-29 01:31:09 +03:00
|
|
|
#include "spdlog/details/null_mutex.h"
|
2016-04-20 11:57:49 +03:00
|
|
|
|
|
|
|
#include <cstdio>
|
|
|
|
#include <memory>
|
|
|
|
#include <mutex>
|
|
|
|
|
2018-03-17 12:47:46 +02:00
|
|
|
namespace spdlog {
|
2018-04-20 13:20:19 +03:00
|
|
|
|
|
|
|
namespace sinks {
|
|
|
|
|
2018-07-14 16:21:53 +03:00
|
|
|
template<typename TargetStream, typename ConsoleMutex>
|
2018-09-26 15:24:22 +03:00
|
|
|
class stdout_sink final : public sink
|
2018-04-20 13:20:19 +03:00
|
|
|
{
|
|
|
|
public:
|
2018-07-07 14:04:31 +03:00
|
|
|
using mutex_t = typename ConsoleMutex::mutex_t;
|
2018-04-20 13:20:19 +03:00
|
|
|
stdout_sink()
|
2018-07-14 16:21:53 +03:00
|
|
|
: mutex_(ConsoleMutex::mutex())
|
2018-07-07 14:04:31 +03:00
|
|
|
, file_(TargetStream::stream())
|
2019-05-12 00:22:39 +03:00
|
|
|
{}
|
2018-08-13 10:30:02 +03:00
|
|
|
~stdout_sink() override = default;
|
2018-04-20 13:20:19 +03:00
|
|
|
|
|
|
|
stdout_sink(const stdout_sink &other) = delete;
|
|
|
|
stdout_sink &operator=(const stdout_sink &other) = delete;
|
|
|
|
|
|
|
|
void log(const details::log_msg &msg) override
|
|
|
|
{
|
2018-06-10 22:59:17 +03:00
|
|
|
std::lock_guard<mutex_t> lock(mutex_);
|
2018-07-07 14:04:31 +03:00
|
|
|
fmt::memory_buffer formatted;
|
|
|
|
formatter_->format(msg, formatted);
|
|
|
|
fwrite(formatted.data(), sizeof(char), formatted.size(), file_);
|
|
|
|
fflush(TargetStream::stream());
|
2018-04-20 13:20:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void flush() override
|
|
|
|
{
|
2018-06-10 22:59:17 +03:00
|
|
|
std::lock_guard<mutex_t> lock(mutex_);
|
2018-07-14 16:21:53 +03:00
|
|
|
fflush(file_);
|
|
|
|
}
|
|
|
|
|
2018-08-13 10:30:02 +03:00
|
|
|
void set_pattern(const std::string &pattern) override
|
2018-07-14 16:21:53 +03:00
|
|
|
{
|
|
|
|
std::lock_guard<mutex_t> lock(mutex_);
|
|
|
|
formatter_ = std::unique_ptr<spdlog::formatter>(new pattern_formatter(pattern));
|
|
|
|
}
|
|
|
|
|
2018-08-13 10:30:02 +03:00
|
|
|
void set_formatter(std::unique_ptr<spdlog::formatter> sink_formatter) override
|
2018-07-14 16:21:53 +03:00
|
|
|
{
|
|
|
|
std::lock_guard<mutex_t> lock(mutex_);
|
|
|
|
formatter_ = std::move(sink_formatter);
|
2018-04-20 13:20:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2018-06-10 22:59:17 +03:00
|
|
|
mutex_t &mutex_;
|
|
|
|
FILE *file_;
|
2018-04-20 13:20:19 +03:00
|
|
|
};
|
|
|
|
|
2018-07-14 16:21:53 +03:00
|
|
|
using stdout_sink_mt = stdout_sink<details::console_stdout, details::console_mutex>;
|
|
|
|
using stdout_sink_st = stdout_sink<details::console_stdout, details::console_nullmutex>;
|
2018-07-07 14:04:31 +03:00
|
|
|
|
2018-07-14 16:21:53 +03:00
|
|
|
using stderr_sink_mt = stdout_sink<details::console_stderr, details::console_mutex>;
|
|
|
|
using stderr_sink_st = stdout_sink<details::console_stderr, details::console_nullmutex>;
|
2018-04-20 13:20:19 +03:00
|
|
|
|
|
|
|
} // namespace sinks
|
|
|
|
|
|
|
|
// factory methods
|
|
|
|
template<typename Factory = default_factory>
|
|
|
|
inline std::shared_ptr<logger> stdout_logger_mt(const std::string &logger_name)
|
|
|
|
{
|
2018-04-20 14:03:15 +03:00
|
|
|
return Factory::template create<sinks::stdout_sink_mt>(logger_name);
|
2018-04-20 13:20:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Factory = default_factory>
|
|
|
|
inline std::shared_ptr<logger> stdout_logger_st(const std::string &logger_name)
|
|
|
|
{
|
2018-04-20 14:03:15 +03:00
|
|
|
return Factory::template create<sinks::stdout_sink_st>(logger_name);
|
2018-04-20 13:20:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Factory = default_factory>
|
|
|
|
inline std::shared_ptr<logger> stderr_logger_mt(const std::string &logger_name)
|
|
|
|
{
|
2018-04-20 14:03:15 +03:00
|
|
|
return Factory::template create<sinks::stderr_sink_mt>(logger_name);
|
2018-04-20 13:20:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Factory = default_factory>
|
|
|
|
inline std::shared_ptr<logger> stderr_logger_st(const std::string &logger_name)
|
|
|
|
{
|
2018-04-20 14:03:15 +03:00
|
|
|
return Factory::template create<sinks::stderr_sink_st>(logger_name);
|
2018-04-20 13:20:19 +03:00
|
|
|
}
|
2018-03-17 12:47:46 +02:00
|
|
|
} // namespace spdlog
|