From c2a49080aa8e28a059deca838022a512fb088b88 Mon Sep 17 00:00:00 2001 From: gabime Date: Sat, 14 Jul 2018 16:21:53 +0300 Subject: [PATCH] Refactored sink interface and base_sink --- bench/bench.cpp | 1 + example/Makefile | 2 +- example/example.cpp | 9 ++--- include/spdlog/async_logger.h | 2 +- include/spdlog/details/async_logger_impl.h | 2 +- include/spdlog/details/console_globals.h | 24 ++++++++------ include/spdlog/details/logger_impl.h | 4 +-- include/spdlog/details/registry.h | 5 ++- include/spdlog/logger.h | 4 +-- include/spdlog/sinks/android_sink.h | 33 ++++++++++++------- include/spdlog/sinks/ansicolor_sink.h | 24 ++++++++++---- include/spdlog/sinks/base_sink.h | 20 +++++++++--- include/spdlog/sinks/basic_file_sink.h | 6 ++-- include/spdlog/sinks/daily_file_sink.h | 7 ++-- include/spdlog/sinks/dist_sink.h | 38 ++++++++++------------ include/spdlog/sinks/msvc_sink.h | 2 +- include/spdlog/sinks/null_sink.h | 5 ++- include/spdlog/sinks/ostream_sink.h | 8 +++-- include/spdlog/sinks/rotating_file_sink.h | 6 ++-- include/spdlog/sinks/sink.h | 26 +++++++-------- include/spdlog/sinks/stdout_sinks.h | 26 +++++++++++---- include/spdlog/sinks/syslog_sink.h | 28 +++++++++++----- include/spdlog/sinks/wincolor_sink.h | 25 ++++++++++---- tests/errors.cpp | 11 +++++-- tests/test_sink.h | 2 +- 25 files changed, 200 insertions(+), 120 deletions(-) diff --git a/bench/bench.cpp b/bench/bench.cpp index c4f39823..a77ee83e 100644 --- a/bench/bench.cpp +++ b/bench/bench.cpp @@ -110,6 +110,7 @@ void bench(int howmany, std::shared_ptr log) auto delta = high_resolution_clock::now() - start; auto delta_d = duration_cast>(delta).count(); + cout << "Elapsed: " << delta_d << "\t" << format(int(howmany / delta_d)) << "/sec" << endl; spdlog::drop(log->name()); } diff --git a/example/Makefile b/example/Makefile index 0f085d18..01bdf507 100644 --- a/example/Makefile +++ b/example/Makefile @@ -1,4 +1,4 @@ -CXX = g++ +CXX ?= g++ CXX_FLAGS = -Wall -Wextra -pedantic -std=c++11 -pthread -I../include -fmax-errors=1 CXX_RELEASE_FLAGS = -O3 -march=native CXX_DEBUG_FLAGS= -g diff --git a/example/example.cpp b/example/example.cpp index 04e5aec3..095c1a1a 100644 --- a/example/example.cpp +++ b/example/example.cpp @@ -59,7 +59,8 @@ int main(int, char *[]) } } -#include "spdlog/sinks/stdout_color_sinks.h" // or "/sinks/stdout_sinks.h" if no colors needed +#include "spdlog/sinks/stdout_color_sinks.h" +// or #include "spdlog/sinks/stdout_sinks.h" if no colors needed void stdout_example() { // create color multi threaded logger @@ -181,18 +182,18 @@ void err_handler_example() void syslog_example() { std::string ident = "spdlog-example"; - auto syslog_logger = spdlog::syslog_logger("syslog", ident, LOG_PID); + auto syslog_logger = spdlog::syslog_logger_mt("syslog", ident, LOG_PID); syslog_logger->warn("This is warning that will end up in syslog."); } #endif // Android example #if defined(__ANDROID__) -#incude "spdlog/sinks/android_sink.h" +#include "spdlog/sinks/android_sink.h" void android_example() { std::string tag = "spdlog-android"; - auto android_logger = spdlog::android_logger("android", tag); + auto android_logger = spdlog::android_logger_mt("android", tag); android_logger->critical("Use \"adb shell logcat\" to view this message."); } diff --git a/include/spdlog/async_logger.h b/include/spdlog/async_logger.h index 4faaabc8..4f0bf3f8 100644 --- a/include/spdlog/async_logger.h +++ b/include/spdlog/async_logger.h @@ -32,7 +32,7 @@ class async_logger SPDLOG_FINAL : public std::enable_shared_from_this + template async_logger(const std::string &logger_name, const It &begin, const It &end, std::weak_ptr tp, async_overflow_policy overflow_policy = async_overflow_policy::block); diff --git a/include/spdlog/details/async_logger_impl.h b/include/spdlog/details/async_logger_impl.h index a3e6fb98..77247ab7 100644 --- a/include/spdlog/details/async_logger_impl.h +++ b/include/spdlog/details/async_logger_impl.h @@ -14,7 +14,7 @@ #include #include -template +template inline spdlog::async_logger::async_logger(const std::string &logger_name, const It &begin, const It &end, std::weak_ptr tp, async_overflow_policy overflow_policy) : logger(logger_name, begin, end) diff --git a/include/spdlog/details/console_globals.h b/include/spdlog/details/console_globals.h index 58f86a95..df17eb55 100644 --- a/include/spdlog/details/console_globals.h +++ b/include/spdlog/details/console_globals.h @@ -4,10 +4,14 @@ // Distributed under the MIT License (http://opensource.org/licenses/MIT) // +#include "spdlog/details/null_mutex.h" #include "stdio.h" +#include + + namespace spdlog { namespace details { -struct console_stdout_stream +struct console_stdout { static FILE *stream() { @@ -21,7 +25,7 @@ struct console_stdout_stream #endif }; -struct console_stderr_stream +struct console_stderr { static FILE *stream() { @@ -35,23 +39,23 @@ struct console_stderr_stream #endif }; -struct console_global_mutex +struct console_mutex { using mutex_t = std::mutex; - static mutex_t &console_mutex() + static mutex_t &mutex() { - static mutex_t mutex; - return mutex; + static mutex_t s_mutex; + return s_mutex; } }; -struct console_global_nullmutex +struct console_nullmutex { using mutex_t = null_mutex; - static mutex_t &console_mutex() + static mutex_t &mutex() { - static mutex_t mutex; - return mutex; + static mutex_t s_mutex; + return s_mutex; } }; } // namespace details diff --git a/include/spdlog/details/logger_impl.h b/include/spdlog/details/logger_impl.h index 5566b049..cc80ec76 100644 --- a/include/spdlog/details/logger_impl.h +++ b/include/spdlog/details/logger_impl.h @@ -10,7 +10,7 @@ // create logger with given name, sinks and the default pattern formatter // all other ctors will call this one -template +template inline spdlog::logger::logger(std::string logger_name, const It &begin, const It &end) : name_(std::move(logger_name)) , sinks_(begin, end) @@ -36,7 +36,7 @@ inline spdlog::logger::logger(const std::string &logger_name, spdlog::sink_ptr s inline spdlog::logger::~logger() = default; -template +template inline void spdlog::logger::set_formatter(const Args &... args) { for (auto &sink : sinks_) diff --git a/include/spdlog/details/registry.h b/include/spdlog/details/registry.h index 6de2f57a..e1961840 100644 --- a/include/spdlog/details/registry.h +++ b/include/spdlog/details/registry.h @@ -23,7 +23,7 @@ namespace spdlog { namespace details { class thread_pool; -template +template class registry_t { public: @@ -46,8 +46,7 @@ public: auto logger_name = new_logger->name(); throw_if_exists_(logger_name); - // create default formatter if not exists - + // set the global formatter pattern new_logger->set_formatter(formatter_pattern_, pattern_time_type_); if (err_handler_) diff --git a/include/spdlog/logger.h b/include/spdlog/logger.h index 30439f75..577528ab 100644 --- a/include/spdlog/logger.h +++ b/include/spdlog/logger.h @@ -31,7 +31,7 @@ public: logger(const std::string &name, sink_ptr single_sink); logger(const std::string &name, sinks_init_list sinks); - template + template logger(std::string name, const It &begin, const It &end); virtual ~logger(); @@ -118,7 +118,7 @@ public: // create a FormatterT formatter for each sink in this logger. // each sink gets its own private copy of a formatter object. - template + template void set_formatter(const Args &... args); void flush(); diff --git a/include/spdlog/sinks/android_sink.h b/include/spdlog/sinks/android_sink.h index ce3e9cd8..08b3121f 100644 --- a/include/spdlog/sinks/android_sink.h +++ b/include/spdlog/sinks/android_sink.h @@ -5,10 +5,10 @@ #pragma once -#if defined(__ANDROID__) - #include "spdlog/details/os.h" -#include "spdlog/sinks/sink.h" +#include "spdlog/sinks/base_sink.h" +#include "spdlog/details/null_mutex.h" +#include "spdlog/details/fmt_helper.h" #include #include @@ -25,9 +25,9 @@ namespace sinks { /* * Android sink (logging using __android_log_write) - * __android_log_write is thread-safe. No lock is needed. */ -class android_sink : public sink +template +class android_sink SPDLOG_FINAL: public base_sink { public: explicit android_sink(const std::string &tag = "spdlog", bool use_raw_msg = false) @@ -36,13 +36,14 @@ public: { } - void log(const details::log_msg &msg) override +protected: + void sink_it_(const details::log_msg &msg) override { const android_LogPriority priority = convert_to_android_(msg.level); fmt::memory_buffer formatted; if (use_raw_msg_) { - formatted.append(msg.raw.data(), msg.raw.data() + msg.raw.size()); + fmt_helper::append_buf(msg.raw, formatted); } else { @@ -67,7 +68,9 @@ public: } } - void flush() override {} + void flush_() override + { + } private: static android_LogPriority convert_to_android_(spdlog::level::level_enum level) @@ -95,16 +98,24 @@ private: bool use_raw_msg_; }; +using android_sink_mt = android_sink; +using android_sink_st = android_sink; } // namespace sinks // Create and register android syslog logger template -inline std::shared_ptr android_logger(const std::string &logger_name, const std::string &tag = "spdlog") +inline std::shared_ptr android_logger_mt(const std::string &logger_name, const std::string &tag = "spdlog") { - return Factory::template create(logger_name, tag); + return Factory::template create(logger_name, tag); +} + +template +inline std::shared_ptr android_logger_st(const std::string &logger_name, const std::string &tag = "spdlog") +{ + return Factory::template create(logger_name, tag); } } // namespace spdlog -#endif + diff --git a/include/spdlog/sinks/ansicolor_sink.h b/include/spdlog/sinks/ansicolor_sink.h index 961062ac..516369fa 100644 --- a/include/spdlog/sinks/ansicolor_sink.h +++ b/include/spdlog/sinks/ansicolor_sink.h @@ -22,14 +22,14 @@ namespace sinks { * of the message. * If no color terminal detected, omit the escape codes. */ -template +template class ansicolor_sink : public sink { public: using mutex_t = typename ConsoleMutex::mutex_t; ansicolor_sink() : target_file_(TargetStream::stream()) - , mutex_(ConsoleMutex::console_mutex()) + , mutex_(ConsoleMutex::mutex()) { should_do_colors_ = details::os::in_terminal(target_file_) && details::os::is_color_terminal(); @@ -115,6 +115,18 @@ public: fflush(target_file_); } + void set_pattern(const std::string &pattern) override SPDLOG_FINAL + { + std::lock_guard lock(mutex_); + formatter_ = std::unique_ptr(new pattern_formatter(pattern)); + } + + void set_formatter(std::unique_ptr sink_formatter) override SPDLOG_FINAL + { + std::lock_guard lock(mutex_); + formatter_ = std::move(sink_formatter); + } + private: void print_ccode_(const std::string &color_code) { @@ -132,11 +144,11 @@ private: std::unordered_map colors_; }; -using ansicolor_stdout_sink_mt = ansicolor_sink; -using ansicolor_stdout_sink_st = ansicolor_sink; +using ansicolor_stdout_sink_mt = ansicolor_sink; +using ansicolor_stdout_sink_st = ansicolor_sink; -using ansicolor_stderr_sink_mt = ansicolor_sink; -using ansicolor_stderr_sink_st = ansicolor_sink; +using ansicolor_stderr_sink_mt = ansicolor_sink; +using ansicolor_stderr_sink_st = ansicolor_sink; } // namespace sinks diff --git a/include/spdlog/sinks/base_sink.h b/include/spdlog/sinks/base_sink.h index 292a3261..72e300ee 100644 --- a/include/spdlog/sinks/base_sink.h +++ b/include/spdlog/sinks/base_sink.h @@ -17,7 +17,7 @@ namespace spdlog { namespace sinks { -template +template class base_sink : public sink { public: @@ -32,9 +32,7 @@ public: void log(const details::log_msg &msg) SPDLOG_FINAL override { std::lock_guard lock(mutex_); - fmt::memory_buffer formatted; - formatter_->format(msg, formatted); - sink_it_(msg, formatted); + sink_it_(msg); } void flush() SPDLOG_FINAL override @@ -43,8 +41,20 @@ public: flush_(); } + void set_pattern(const std::string &pattern) SPDLOG_FINAL override + { + std::lock_guard lock(mutex_); + formatter_ = std::unique_ptr(new pattern_formatter(pattern)); + } + + void set_formatter(std::unique_ptr sink_formatter) SPDLOG_FINAL override + { + std::lock_guard lock(mutex_); + formatter_ = std::move(sink_formatter); + } + protected: - virtual void sink_it_(const details::log_msg &msg, const fmt::memory_buffer &formatted) = 0; + virtual void sink_it_(const details::log_msg &msg) = 0; virtual void flush_() = 0; Mutex mutex_; }; diff --git a/include/spdlog/sinks/basic_file_sink.h b/include/spdlog/sinks/basic_file_sink.h index ffa92be0..5db38e8a 100644 --- a/include/spdlog/sinks/basic_file_sink.h +++ b/include/spdlog/sinks/basic_file_sink.h @@ -17,7 +17,7 @@ namespace sinks { /* * Trivial file sink with single file as target */ -template +template class basic_file_sink SPDLOG_FINAL : public base_sink { public: @@ -27,8 +27,10 @@ public: } protected: - void sink_it_(const details::log_msg &, const fmt::memory_buffer &formatted) override + void sink_it_(const details::log_msg &msg) override { + fmt::memory_buffer formatted; + sink::formatter_->format(msg, formatted); file_helper_.write(formatted); } diff --git a/include/spdlog/sinks/daily_file_sink.h b/include/spdlog/sinks/daily_file_sink.h index 1d49505e..80f55fd2 100644 --- a/include/spdlog/sinks/daily_file_sink.h +++ b/include/spdlog/sinks/daily_file_sink.h @@ -39,7 +39,7 @@ struct daily_filename_calculator /* * Rotating file sink based on date. rotates at midnight */ -template +template class daily_file_sink SPDLOG_FINAL : public base_sink { public: @@ -59,13 +59,16 @@ public: } protected: - void sink_it_(const details::log_msg &msg, const fmt::memory_buffer &formatted) override + void sink_it_(const details::log_msg &msg) override { + if (msg.time >= rotation_tp_) { file_helper_.open(FileNameCalc::calc_filename(base_filename_, now_tm(msg.time))); rotation_tp_ = next_rotation_tp_(); } + fmt::memory_buffer formatted; + sink::formatter_->format(msg, formatted); file_helper_.write(formatted); } diff --git a/include/spdlog/sinks/dist_sink.h b/include/spdlog/sinks/dist_sink.h index 80861247..6f51a5e3 100644 --- a/include/spdlog/sinks/dist_sink.h +++ b/include/spdlog/sinks/dist_sink.h @@ -5,6 +5,7 @@ #pragma once +#include "base_sink.h" #include "spdlog/details/log_msg.h" #include "spdlog/details/null_mutex.h" @@ -19,16 +20,29 @@ namespace spdlog { namespace sinks { template -class dist_sink : public sink +class dist_sink : public base_sink { public: dist_sink() = default; dist_sink(const dist_sink &) = delete; dist_sink &operator=(const dist_sink &) = delete; - void log(const details::log_msg &msg) SPDLOG_FINAL override + void add_sink(std::shared_ptr sink) { - std::lock_guard lock(mutex_); + std::lock_guard lock(base_sink::mutex_); + sinks_.push_back(sink); + } + + void remove_sink(std::shared_ptr sink) + { + std::lock_guard lock(base_sink::mutex_); + sinks_.erase(std::remove(sinks_.begin(), sinks_.end(), sink), sinks_.end()); + } + +protected: + void sink_it_(const details::log_msg &msg) override + { + for (auto &sink : sinks_) { if (sink->should_log(msg.level)) @@ -38,27 +52,11 @@ public: } } - void flush() SPDLOG_FINAL override + void flush_() override { - std::lock_guard lock(mutex_); for (auto &sink : sinks_) sink->flush(); } - - void add_sink(std::shared_ptr sink) - { - std::lock_guard lock(mutex_); - sinks_.push_back(sink); - } - - void remove_sink(std::shared_ptr sink) - { - std::lock_guard lock(mutex_); - sinks_.erase(std::remove(sinks_.begin(), sinks_.end(), sink), sinks_.end()); - } - -private: - Mutex mutex_; std::vector> sinks_; }; diff --git a/include/spdlog/sinks/msvc_sink.h b/include/spdlog/sinks/msvc_sink.h index 4c45635a..068d67e9 100644 --- a/include/spdlog/sinks/msvc_sink.h +++ b/include/spdlog/sinks/msvc_sink.h @@ -20,7 +20,7 @@ namespace sinks { /* * MSVC sink (logging using OutputDebugStringA) */ -template +template class msvc_sink : public base_sink { public: diff --git a/include/spdlog/sinks/null_sink.h b/include/spdlog/sinks/null_sink.h index 24a3b3ea..73b8c2b7 100644 --- a/include/spdlog/sinks/null_sink.h +++ b/include/spdlog/sinks/null_sink.h @@ -13,12 +13,11 @@ namespace spdlog { namespace sinks { -template +template class null_sink : public base_sink { protected: - void sink_it_(const details::log_msg &, const fmt::memory_buffer &) override {} - + void sink_it_(const details::log_msg &) override {} void flush_() override {} }; diff --git a/include/spdlog/sinks/ostream_sink.h b/include/spdlog/sinks/ostream_sink.h index c18bb8f6..5f31993c 100644 --- a/include/spdlog/sinks/ostream_sink.h +++ b/include/spdlog/sinks/ostream_sink.h @@ -13,8 +13,8 @@ namespace spdlog { namespace sinks { -template -class ostream_sink : public base_sink +template +class ostream_sink SPDLOG_FINAL : public base_sink { public: explicit ostream_sink(std::ostream &os, bool force_flush = false) @@ -26,8 +26,10 @@ public: ostream_sink &operator=(const ostream_sink &) = delete; protected: - void sink_it_(const details::log_msg &msg, const fmt::memory_buffer &formatted) override + void sink_it_(const details::log_msg &msg) override { + fmt::memory_buffer formatted; + sink::formatter_->format(msg, formatted); ostream_.write(formatted.data(), formatted.size()); if (force_flush_) ostream_.flush(); diff --git a/include/spdlog/sinks/rotating_file_sink.h b/include/spdlog/sinks/rotating_file_sink.h index aeeb5140..c88d0d0a 100644 --- a/include/spdlog/sinks/rotating_file_sink.h +++ b/include/spdlog/sinks/rotating_file_sink.h @@ -23,7 +23,7 @@ namespace sinks { // // Rotating file sink based on size // -template +template class rotating_file_sink SPDLOG_FINAL : public base_sink { public: @@ -55,8 +55,10 @@ public: } protected: - void sink_it_(const details::log_msg &, const fmt::memory_buffer &formatted) override + void sink_it_(const details::log_msg &msg) override { + fmt::memory_buffer formatted; + sink::formatter_->format(msg, formatted); current_size_ += formatted.size(); if (current_size_ > max_size_) { diff --git a/include/spdlog/sinks/sink.h b/include/spdlog/sinks/sink.h index 14557e8d..288d5438 100644 --- a/include/spdlog/sinks/sink.h +++ b/include/spdlog/sinks/sink.h @@ -14,16 +14,21 @@ namespace sinks { class sink { public: - // default sink ctor with default pattern formatter sink() - : formatter_(std::unique_ptr(new pattern_formatter("%+"))) + : level_(level::trace) + , formatter_(new pattern_formatter("%+")) { } - virtual ~sink() = default; + sink(std::unique_ptr formatter) + : level_(level::trace) + , formatter_(std::move(formatter)){}; + virtual ~sink() = default; virtual void log(const details::log_msg &msg) = 0; virtual void flush() = 0; + virtual void set_pattern(const std::string &pattern) = 0; + virtual void set_formatter(std::unique_ptr sink_formatter) = 0; bool should_log(level::level_enum msg_level) const { @@ -40,18 +45,11 @@ public: return static_cast(level_.load(std::memory_order_relaxed)); } - void set_pattern(const std::string &pattern) - { - formatter_ = std::unique_ptr(new pattern_formatter(pattern)); - } - - void set_formatter(std::unique_ptr sink_formatter) - { - formatter_ = std::move(sink_formatter); - } - protected: - level_t level_{level::trace}; + // sink log level - default is all + level_t level_; + + // sink formatter - default is full format std::unique_ptr formatter_; }; diff --git a/include/spdlog/sinks/stdout_sinks.h b/include/spdlog/sinks/stdout_sinks.h index 5d25a954..64c0ab70 100644 --- a/include/spdlog/sinks/stdout_sinks.h +++ b/include/spdlog/sinks/stdout_sinks.h @@ -18,13 +18,13 @@ namespace spdlog { namespace sinks { -template +template class stdout_sink : public sink { public: using mutex_t = typename ConsoleMutex::mutex_t; stdout_sink() - : mutex_(ConsoleMutex::console_mutex()) + : mutex_(ConsoleMutex::mutex()) , file_(TargetStream::stream()) { } @@ -45,7 +45,19 @@ public: void flush() override { std::lock_guard lock(mutex_); - fflush(TargetStream::stream()); + fflush(file_); + } + + void set_pattern(const std::string &pattern) override SPDLOG_FINAL + { + std::lock_guard lock(mutex_); + formatter_ = std::unique_ptr(new pattern_formatter(pattern)); + } + + void set_formatter(std::unique_ptr sink_formatter) override SPDLOG_FINAL + { + std::lock_guard lock(mutex_); + formatter_ = std::move(sink_formatter); } private: @@ -53,11 +65,11 @@ private: FILE *file_; }; -using stdout_sink_mt = stdout_sink; -using stdout_sink_st = stdout_sink; +using stdout_sink_mt = stdout_sink; +using stdout_sink_st = stdout_sink; -using stderr_sink_mt = stdout_sink; -using stderr_sink_st = stdout_sink; +using stderr_sink_mt = stdout_sink; +using stderr_sink_st = stdout_sink; } // namespace sinks diff --git a/include/spdlog/sinks/syslog_sink.h b/include/spdlog/sinks/syslog_sink.h index ca8487f0..2b12baf4 100644 --- a/include/spdlog/sinks/syslog_sink.h +++ b/include/spdlog/sinks/syslog_sink.h @@ -5,10 +5,8 @@ #pragma once -#include "spdlog/common.h" - -#include "spdlog/details/log_msg.h" -#include "spdlog/sinks/sink.h" +#include "spdlog/sinks/base_sink.h" +#include "spdlog/spdlog.h" #include #include @@ -21,7 +19,8 @@ namespace sinks { * * Locking is not needed, as `syslog()` itself is thread-safe. */ -class syslog_sink : public sink +template +class syslog_sink : public base_sink { public: // @@ -48,12 +47,13 @@ public: syslog_sink(const syslog_sink &) = delete; syslog_sink &operator=(const syslog_sink &) = delete; - void log(const details::log_msg &msg) override +protected: + void sink_it_(const details::log_msg &msg) override { ::syslog(syslog_prio_from_level(msg), "%s", fmt::to_string(msg.raw).c_str()); } - void flush() override {} + void flush_() override {} private: std::array priorities_; @@ -68,13 +68,23 @@ private: return priorities_[static_cast(msg.level)]; } }; + +using syslog_sink_mt = syslog_sink; +using syslog_sink_st = syslog_sink; } // namespace sinks // Create and register a syslog logger template -inline std::shared_ptr syslog_logger( +inline std::shared_ptr syslog_logger_mt( const std::string &logger_name, const std::string &syslog_ident = "", int syslog_option = 0, int syslog_facility = (1 << 3)) { - return Factory::template create(logger_name, syslog_ident, syslog_option, syslog_facility); + return Factory::template create(logger_name, syslog_ident, syslog_option, syslog_facility); +} + +template +inline std::shared_ptr syslog_logger_st( + const std::string &logger_name, const std::string &syslog_ident = "", int syslog_option = 0, int syslog_facility = (1 << 3)) +{ + return Factory::template create(logger_name, syslog_ident, syslog_option, syslog_facility); } } // namespace spdlog diff --git a/include/spdlog/sinks/wincolor_sink.h b/include/spdlog/sinks/wincolor_sink.h index 7199db11..913c774e 100644 --- a/include/spdlog/sinks/wincolor_sink.h +++ b/include/spdlog/sinks/wincolor_sink.h @@ -5,7 +5,6 @@ #pragma once -#include "../fmt/fmt.h" #include "spdlog/common.h" #include "spdlog/details/console_globals.h" #include "spdlog/details/null_mutex.h" @@ -22,7 +21,7 @@ namespace sinks { /* * Windows color console sink. Uses WriteConsoleA to write to the console with colors */ -template +template class wincolor_sink : public sink { public: @@ -35,7 +34,7 @@ public: wincolor_sink() : out_handle_(OutHandle::handle()) - , mutex_(ConsoleMutex::console_mutex()) + , mutex_(ConsoleMutex::mutex()) { colors_[level::trace] = WHITE; colors_[level::debug] = CYAN; @@ -89,6 +88,18 @@ public: // windows console always flushed? } + void set_pattern(const std::string &pattern) override SPDLOG_FINAL + { + std::lock_guard lock(mutex_); + formatter_ = std::unique_ptr(new pattern_formatter(pattern)); + } + + void set_formatter(std::unique_ptr sink_formatter) override SPDLOG_FINAL + { + std::lock_guard lock(mutex_); + formatter_ = std::move(sink_formatter); + } + private: using mutex_t = typename ConsoleMutex::mutex_t; // set color and return the orig console attributes (for resetting later) @@ -116,11 +127,11 @@ private: std::unordered_map colors_; }; -using wincolor_stdout_sink_mt = wincolor_sink; -using wincolor_stdout_sink_st = wincolor_sink; +using wincolor_stdout_sink_mt = wincolor_sink; +using wincolor_stdout_sink_st = wincolor_sink; -using wincolor_stderr_sink_mt = wincolor_sink; -using wincolor_stderr_sink_st = wincolor_sink; +using wincolor_stderr_sink_mt = wincolor_sink; +using wincolor_stderr_sink_st = wincolor_sink; } // namespace sinks } // namespace spdlog diff --git a/tests/errors.cpp b/tests/errors.cpp index 1320b748..32209b43 100644 --- a/tests/errors.cpp +++ b/tests/errors.cpp @@ -5,14 +5,19 @@ #include -class failing_sink : public spdlog::sinks::sink +class failing_sink : public spdlog::sinks::base_sink { - void log(const spdlog::details::log_msg &) override +public: + failing_sink() = default; + ~failing_sink() = default; + +protected: + void sink_it_(const spdlog::details::log_msg &) override { throw std::runtime_error("some error happened during log"); } - void flush() override + void flush_() override { throw std::runtime_error("some error happened during flush"); } diff --git a/tests/test_sink.h b/tests/test_sink.h index 3d8ee205..c4a09f0d 100644 --- a/tests/test_sink.h +++ b/tests/test_sink.h @@ -35,7 +35,7 @@ public: } protected: - void sink_it_(const details::log_msg &, const fmt::memory_buffer &) override + void sink_it_(const details::log_msg &) override { msg_counter_++; std::this_thread::sleep_for(delay_);