This commit is contained in:
gabime 2019-06-20 00:29:23 +03:00
parent b12c19162b
commit 1d86803e38
4 changed files with 152 additions and 57 deletions

View File

@ -18,7 +18,6 @@
#include "spdlog/details/registry.h" #include "spdlog/details/registry.h"
#include "spdlog/details/thread_pool.h" #include "spdlog/details/thread_pool.h"
#include <memory> #include <memory>
#include <mutex> #include <mutex>
#include <functional> #include <functional>
@ -81,7 +80,7 @@ inline void init_thread_pool(size_t q_size, size_t thread_count, std::function<v
// set global thread pool. // set global thread pool.
inline void init_thread_pool(size_t q_size, size_t thread_count) inline void init_thread_pool(size_t q_size, size_t thread_count)
{ {
init_thread_pool(q_size, thread_count, []{}); init_thread_pool(q_size, thread_count, [] {});
} }
// get the global thread pool. // get the global thread pool.

View File

@ -0,0 +1,89 @@
// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
#pragma once
#ifndef SPDLOG_HEADER_ONLY
#include "spdlog/sinks/stdout_sinks.h"
#endif
namespace spdlog {
namespace sinks {
template<typename ConsoleMutex>
SPDLOG_INLINE stdout_sink_base<ConsoleMutex>::stdout_sink_base(FILE *file)
: mutex_(ConsoleMutex::mutex())
, file_(file)
{}
template<typename ConsoleMutex>
SPDLOG_INLINE void stdout_sink_base<ConsoleMutex>::log(const details::log_msg &msg)
{
std::lock_guard<mutex_t> lock(mutex_);
fmt::memory_buffer formatted;
formatter_->format(msg, formatted);
fwrite(formatted.data(), sizeof(char), formatted.size(), file_);
fflush(file_); // flush every line to terminal
}
template<typename ConsoleMutex>
SPDLOG_INLINE void stdout_sink_base<ConsoleMutex>::flush()
{
std::lock_guard<mutex_t> lock(mutex_);
fflush(file_);
}
template<typename ConsoleMutex>
SPDLOG_INLINE void stdout_sink_base<ConsoleMutex>::set_pattern(const std::string &pattern)
{
std::lock_guard<mutex_t> lock(mutex_);
formatter_ = std::unique_ptr<spdlog::formatter>(new pattern_formatter(pattern));
}
template<typename ConsoleMutex>
SPDLOG_INLINE void stdout_sink_base<ConsoleMutex>::set_formatter(std::unique_ptr<spdlog::formatter> sink_formatter)
{
std::lock_guard<mutex_t> lock(mutex_);
formatter_ = std::move(sink_formatter);
}
// stdout sink
template<typename ConsoleMutex>
SPDLOG_INLINE stdout_sink<ConsoleMutex>::stdout_sink()
: stdout_sink_base<ConsoleMutex>(stdout)
{}
// stderr sink
template<typename ConsoleMutex>
SPDLOG_INLINE stderr_sink<ConsoleMutex>::stderr_sink()
: stdout_sink_base<ConsoleMutex>(stderr)
{}
} // namespace sinks
// factory methods
template<typename Factory>
SPDLOG_INLINE std::shared_ptr<logger> stdout_logger_mt(const std::string &logger_name)
{
return Factory::template create<sinks::stdout_sink_mt>(logger_name);
}
template<typename Factory>
SPDLOG_INLINE std::shared_ptr<logger> stdout_logger_st(const std::string &logger_name)
{
return Factory::template create<sinks::stdout_sink_st>(logger_name);
}
template<typename Factory>
SPDLOG_INLINE std::shared_ptr<logger> stderr_logger_mt(const std::string &logger_name)
{
return Factory::template create<sinks::stderr_sink_mt>(logger_name);
}
template<typename Factory>
SPDLOG_INLINE std::shared_ptr<logger> stderr_logger_st(const std::string &logger_name)
{
return Factory::template create<sinks::stderr_sink_st>(logger_name);
}
} // namespace spdlog

View File

@ -16,82 +16,64 @@ namespace spdlog {
namespace sinks { namespace sinks {
template<typename TargetStream, typename ConsoleMutex> template<typename ConsoleMutex>
class stdout_sink final : public sink class stdout_sink_base : public sink
{ {
public: public:
using mutex_t = typename ConsoleMutex::mutex_t; using mutex_t = typename ConsoleMutex::mutex_t;
stdout_sink() explicit stdout_sink_base(FILE *file);
: mutex_(ConsoleMutex::mutex()) ~stdout_sink_base() override = default;
, file_(TargetStream::stream()) stdout_sink_base(const stdout_sink_base &other) = delete;
{} stdout_sink_base &operator=(const stdout_sink_base &other) = delete;
~stdout_sink() override = default;
stdout_sink(const stdout_sink &other) = delete; void log(const details::log_msg &msg) override;
stdout_sink &operator=(const stdout_sink &other) = delete; void flush() override;
void set_pattern(const std::string &pattern) override;
void log(const details::log_msg &msg) override void set_formatter(std::unique_ptr<spdlog::formatter> sink_formatter) override;
{
std::lock_guard<mutex_t> lock(mutex_);
fmt::memory_buffer formatted;
formatter_->format(msg, formatted);
fwrite(formatted.data(), sizeof(char), formatted.size(), file_);
fflush(TargetStream::stream());
}
void flush() override
{
std::lock_guard<mutex_t> lock(mutex_);
fflush(file_);
}
void set_pattern(const std::string &pattern) override
{
std::lock_guard<mutex_t> lock(mutex_);
formatter_ = std::unique_ptr<spdlog::formatter>(new pattern_formatter(pattern));
}
void set_formatter(std::unique_ptr<spdlog::formatter> sink_formatter) override
{
std::lock_guard<mutex_t> lock(mutex_);
formatter_ = std::move(sink_formatter);
}
private: private:
mutex_t &mutex_; mutex_t &mutex_;
FILE *file_; FILE *file_;
}; };
using stdout_sink_mt = stdout_sink<details::console_stdout, details::console_mutex>; template<typename ConsoleMutex>
using stdout_sink_st = stdout_sink<details::console_stdout, details::console_nullmutex>; class stdout_sink : public stdout_sink_base<ConsoleMutex>
{
public:
stdout_sink();
};
using stderr_sink_mt = stdout_sink<details::console_stderr, details::console_mutex>; template<typename ConsoleMutex>
using stderr_sink_st = stdout_sink<details::console_stderr, details::console_nullmutex>; class stderr_sink : public stdout_sink_base<ConsoleMutex>
{
public:
stderr_sink();
};
using stdout_sink_mt = stdout_sink<details::console_mutex>;
using stdout_sink_st = stdout_sink<details::console_nullmutex>;
using stderr_sink_mt = stderr_sink<details::console_mutex>;
using stderr_sink_st = stderr_sink<details::console_nullmutex>;
} // namespace sinks } // namespace sinks
// factory methods // factory methods
template<typename Factory = spdlog::synchronous_factory> template<typename Factory = spdlog::synchronous_factory>
inline std::shared_ptr<logger> stdout_logger_mt(const std::string &logger_name) std::shared_ptr<logger> stdout_logger_mt(const std::string &logger_name);
{
return Factory::template create<sinks::stdout_sink_mt>(logger_name);
}
template<typename Factory = spdlog::synchronous_factory> template<typename Factory = spdlog::synchronous_factory>
inline std::shared_ptr<logger> stdout_logger_st(const std::string &logger_name) std::shared_ptr<logger> stdout_logger_st(const std::string &logger_name);
{
return Factory::template create<sinks::stdout_sink_st>(logger_name);
}
template<typename Factory = spdlog::synchronous_factory> template<typename Factory = spdlog::synchronous_factory>
inline std::shared_ptr<logger> stderr_logger_mt(const std::string &logger_name) std::shared_ptr<logger> stderr_logger_mt(const std::string &logger_name);
{
return Factory::template create<sinks::stderr_sink_mt>(logger_name);
}
template<typename Factory = spdlog::synchronous_factory> template<typename Factory = spdlog::synchronous_factory>
inline std::shared_ptr<logger> stderr_logger_st(const std::string &logger_name) std::shared_ptr<logger> stderr_logger_st(const std::string &logger_name);
{
return Factory::template create<sinks::stderr_sink_st>(logger_name);
}
} // namespace spdlog } // namespace spdlog
#ifdef SPDLOG_HEADER_ONLY
#include "stdout_sinks-inl.h"
#endif

View File

@ -61,6 +61,7 @@ template class spdlog::sinks::ansicolor_stderr_sink<spdlog::details::console_mut
template class spdlog::sinks::ansicolor_stderr_sink<spdlog::details::console_nullmutex>; template class spdlog::sinks::ansicolor_stderr_sink<spdlog::details::console_nullmutex>;
#endif #endif
// factory methods for color sinks
#include "spdlog/sinks/stdout_color_sinks-inl.h" #include "spdlog/sinks/stdout_color_sinks-inl.h"
template std::shared_ptr<spdlog::logger> spdlog::stdout_color_mt<spdlog::synchronous_factory>( template std::shared_ptr<spdlog::logger> spdlog::stdout_color_mt<spdlog::synchronous_factory>(
const std::string &logger_name, color_mode mode); const std::string &logger_name, color_mode mode);
@ -76,6 +77,30 @@ template std::shared_ptr<spdlog::logger> spdlog::stdout_color_st<spdlog::async_f
template std::shared_ptr<spdlog::logger> spdlog::stderr_color_mt<spdlog::async_factory>(const std::string &logger_name, color_mode mode); template std::shared_ptr<spdlog::logger> spdlog::stderr_color_mt<spdlog::async_factory>(const std::string &logger_name, color_mode mode);
template std::shared_ptr<spdlog::logger> spdlog::stderr_color_st<spdlog::async_factory>(const std::string &logger_name, color_mode mode); template std::shared_ptr<spdlog::logger> spdlog::stderr_color_st<spdlog::async_factory>(const std::string &logger_name, color_mode mode);
// stdout/stderr sinks
#include "spdlog/sinks/stdout_sinks-inl.h"
template class spdlog::sinks::stdout_sink_base<spdlog::details::console_mutex>;
template class spdlog::sinks::stdout_sink_base<spdlog::details::console_nullmutex>;
template class spdlog::sinks::stdout_sink<spdlog::details::console_mutex>;
template class spdlog::sinks::stdout_sink<spdlog::details::console_nullmutex>;
template class spdlog::sinks::stderr_sink<spdlog::details::console_mutex>;
template class spdlog::sinks::stderr_sink<spdlog::details::console_nullmutex>;
// factory methods
template std::shared_ptr<spdlog::logger> spdlog::stdout_logger_mt<spdlog::synchronous_factory>(const std::string &logger_name);
template std::shared_ptr<spdlog::logger> spdlog::stdout_logger_st<spdlog::synchronous_factory>(const std::string &logger_name);
template std::shared_ptr<spdlog::logger> spdlog::stdout_logger_mt<spdlog::async_factory>(const std::string &logger_name);
template std::shared_ptr<spdlog::logger> spdlog::stdout_logger_st<spdlog::async_factory>(const std::string &logger_name);
template std::shared_ptr<spdlog::logger> spdlog::stderr_logger_mt<spdlog::synchronous_factory>(const std::string &logger_name);
template std::shared_ptr<spdlog::logger> spdlog::stderr_logger_st<spdlog::synchronous_factory>(const std::string &logger_name);
template std::shared_ptr<spdlog::logger> spdlog::stderr_logger_mt<spdlog::async_factory>(const std::string &logger_name);
template std::shared_ptr<spdlog::logger> spdlog::stderr_logger_st<spdlog::async_factory>(const std::string &logger_name);
// Slightly modified version of fmt lib's format.cc source file. // Slightly modified version of fmt lib's format.cc source file.
// Copyright (c) 2012 - 2016, Victor Zverovich // Copyright (c) 2012 - 2016, Victor Zverovich
// All rights reserved. // All rights reserved.