mirror of
https://github.com/gabime/spdlog.git
synced 2024-12-25 01:51:38 +08:00
Fix #1116
This commit is contained in:
parent
b12c19162b
commit
1d86803e38
@ -18,7 +18,6 @@
|
||||
#include "spdlog/details/registry.h"
|
||||
#include "spdlog/details/thread_pool.h"
|
||||
|
||||
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#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.
|
||||
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.
|
||||
|
89
include/spdlog/sinks/stdout_sinks-inl.h
Normal file
89
include/spdlog/sinks/stdout_sinks-inl.h
Normal 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
|
@ -16,82 +16,64 @@ namespace spdlog {
|
||||
|
||||
namespace sinks {
|
||||
|
||||
template<typename TargetStream, typename ConsoleMutex>
|
||||
class stdout_sink final : public sink
|
||||
template<typename ConsoleMutex>
|
||||
class stdout_sink_base : public sink
|
||||
{
|
||||
public:
|
||||
using mutex_t = typename ConsoleMutex::mutex_t;
|
||||
stdout_sink()
|
||||
: mutex_(ConsoleMutex::mutex())
|
||||
, file_(TargetStream::stream())
|
||||
{}
|
||||
~stdout_sink() override = default;
|
||||
explicit stdout_sink_base(FILE *file);
|
||||
~stdout_sink_base() override = default;
|
||||
stdout_sink_base(const stdout_sink_base &other) = delete;
|
||||
stdout_sink_base &operator=(const stdout_sink_base &other) = delete;
|
||||
|
||||
stdout_sink(const stdout_sink &other) = delete;
|
||||
stdout_sink &operator=(const stdout_sink &other) = delete;
|
||||
void log(const details::log_msg &msg) override;
|
||||
void flush() override;
|
||||
void set_pattern(const std::string &pattern) override;
|
||||
|
||||
void log(const details::log_msg &msg) 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);
|
||||
}
|
||||
void set_formatter(std::unique_ptr<spdlog::formatter> sink_formatter) override;
|
||||
|
||||
private:
|
||||
mutex_t &mutex_;
|
||||
FILE *file_;
|
||||
};
|
||||
|
||||
using stdout_sink_mt = stdout_sink<details::console_stdout, details::console_mutex>;
|
||||
using stdout_sink_st = stdout_sink<details::console_stdout, details::console_nullmutex>;
|
||||
template<typename ConsoleMutex>
|
||||
class stdout_sink : public stdout_sink_base<ConsoleMutex>
|
||||
{
|
||||
public:
|
||||
stdout_sink();
|
||||
};
|
||||
|
||||
using stderr_sink_mt = stdout_sink<details::console_stderr, details::console_mutex>;
|
||||
using stderr_sink_st = stdout_sink<details::console_stderr, details::console_nullmutex>;
|
||||
template<typename ConsoleMutex>
|
||||
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
|
||||
|
||||
// factory methods
|
||||
template<typename Factory = spdlog::synchronous_factory>
|
||||
inline std::shared_ptr<logger> stdout_logger_mt(const std::string &logger_name)
|
||||
{
|
||||
return Factory::template create<sinks::stdout_sink_mt>(logger_name);
|
||||
}
|
||||
std::shared_ptr<logger> stdout_logger_mt(const std::string &logger_name);
|
||||
|
||||
template<typename Factory = spdlog::synchronous_factory>
|
||||
inline std::shared_ptr<logger> stdout_logger_st(const std::string &logger_name)
|
||||
{
|
||||
return Factory::template create<sinks::stdout_sink_st>(logger_name);
|
||||
}
|
||||
std::shared_ptr<logger> stdout_logger_st(const std::string &logger_name);
|
||||
|
||||
template<typename Factory = spdlog::synchronous_factory>
|
||||
inline std::shared_ptr<logger> stderr_logger_mt(const std::string &logger_name)
|
||||
{
|
||||
return Factory::template create<sinks::stderr_sink_mt>(logger_name);
|
||||
}
|
||||
std::shared_ptr<logger> stderr_logger_mt(const std::string &logger_name);
|
||||
|
||||
template<typename Factory = spdlog::synchronous_factory>
|
||||
inline std::shared_ptr<logger> stderr_logger_st(const std::string &logger_name)
|
||||
{
|
||||
return Factory::template create<sinks::stderr_sink_st>(logger_name);
|
||||
}
|
||||
std::shared_ptr<logger> stderr_logger_st(const std::string &logger_name);
|
||||
|
||||
} // namespace spdlog
|
||||
|
||||
#ifdef SPDLOG_HEADER_ONLY
|
||||
#include "stdout_sinks-inl.h"
|
||||
#endif
|
||||
|
@ -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>;
|
||||
#endif
|
||||
|
||||
// factory methods for color sinks
|
||||
#include "spdlog/sinks/stdout_color_sinks-inl.h"
|
||||
template std::shared_ptr<spdlog::logger> spdlog::stdout_color_mt<spdlog::synchronous_factory>(
|
||||
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_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.
|
||||
// Copyright (c) 2012 - 2016, Victor Zverovich
|
||||
// All rights reserved.
|
||||
|
Loading…
Reference in New Issue
Block a user