From 9dc44c39e7c6f5687feff44b9b2cddd8f8fb3e30 Mon Sep 17 00:00:00 2001 From: gabime Date: Sat, 7 Jul 2018 14:04:31 +0300 Subject: [PATCH] Fixed bugs in stdout_sinks and in msvc --- example/Makefile | 2 +- example/example.cpp | 6 ++-- include/spdlog/details/fmt_helper.h | 4 +++ .../spdlog/details/pattern_formatter_impl.h | 12 ++++---- include/spdlog/details/registry.h | 6 ++-- include/spdlog/details/thread_pool.h | 16 +++++------ include/spdlog/formatter.h | 2 -- include/spdlog/sinks/android_sink.h | 4 +-- include/spdlog/sinks/ansicolor_sink.h | 10 +++---- include/spdlog/sinks/msvc_sink.h | 7 +++-- include/spdlog/sinks/sink.h | 1 + include/spdlog/sinks/stdout_sinks.h | 28 +++++++++++-------- include/spdlog/sinks/wincolor_sink.h | 10 +++---- include/spdlog/sinks/windebug_sink.h | 27 ------------------ 14 files changed, 59 insertions(+), 76 deletions(-) delete mode 100644 include/spdlog/sinks/windebug_sink.h diff --git a/example/Makefile b/example/Makefile index 38c43f19..0f085d18 100644 --- a/example/Makefile +++ b/example/Makefile @@ -1,5 +1,5 @@ CXX = g++ -CXX_FLAGS = -Wall -Wextra -pedantic -std=c++11 -pthread -I../include +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 eb520a25..757f6756 100644 --- a/example/example.cpp +++ b/example/example.cpp @@ -55,16 +55,16 @@ int main(int, char *[]) } } -#include "spdlog/sinks/stdout_color_sinks.h" // or "/sinks/stdout_sinks.h" if no colors needed +#include "spdlog/sinks/stdout_sinks.h" // or "/sinks/stdout_sinks.h" if no colors needed void stdout_example() { // create color multi threaded logger - auto console = spdlog::stdout_color_mt("console"); + auto console = spdlog::stdout_logger_mt("console"); console->info("Welcome to spdlog!"); console->error("Some error message with arg: {}", 1); - auto err_logger = spdlog::stderr_color_mt("error_logger"); + auto err_logger = spdlog::stderr_logger_mt("error_logger"); err_logger->error("Some error message"); // Formatting examples console->warn("Easy padding in numbers like {:08d}", 12); diff --git a/include/spdlog/details/fmt_helper.h b/include/spdlog/details/fmt_helper.h index 4fb0f3f5..743d9a3c 100644 --- a/include/spdlog/details/fmt_helper.h +++ b/include/spdlog/details/fmt_helper.h @@ -88,6 +88,10 @@ inline void pad3(int n, fmt::memory_buffer &dest) inline void pad6(size_t n, fmt::memory_buffer &dest) { + // todo: maybe replace this implementation with + // pad3(n / 1000, dest); + // pad3(n % 1000, dest); + if (n > 99999) { append_int(n, dest); diff --git a/include/spdlog/details/pattern_formatter_impl.h b/include/spdlog/details/pattern_formatter_impl.h index 1b25fb79..e2cd5a95 100644 --- a/include/spdlog/details/pattern_formatter_impl.h +++ b/include/spdlog/details/pattern_formatter_impl.h @@ -533,7 +533,7 @@ public: , last_log_secs_(0) { std::memset(&cached_tm_, 0, sizeof(cached_tm_)); - compile_pattern(pattern); + compile_pattern_(pattern); } pattern_formatter(const pattern_formatter &) = default; @@ -544,7 +544,7 @@ public: auto secs = std::chrono::duration_cast(msg.time.time_since_epoch()); if (secs != last_log_secs_) { - cached_tm_ = get_time(msg); + cached_tm_ = get_time_(msg); last_log_secs_ = secs; } #endif @@ -563,7 +563,7 @@ private: std::chrono::seconds last_log_secs_; std::vector> formatters_; - std::tm get_time(const details::log_msg &msg) + std::tm get_time_(const details::log_msg &msg) { if (pattern_time_ == pattern_time_type::local) { @@ -572,7 +572,7 @@ private: return details::os::gmtime(log_clock::to_time_t(msg.time)); } - void handle_flag(char flag) + void handle_flag_(char flag) { switch (flag) { @@ -717,7 +717,7 @@ private: } } - void compile_pattern(const std::string &pattern) + void compile_pattern_(const std::string &pattern) { auto end = pattern.end(); std::unique_ptr user_chars; @@ -732,7 +732,7 @@ private: // if( if (++it != end) { - handle_flag(*it); + handle_flag_(*it); } else { diff --git a/include/spdlog/details/registry.h b/include/spdlog/details/registry.h index 85111d23..87495de1 100644 --- a/include/spdlog/details/registry.h +++ b/include/spdlog/details/registry.h @@ -36,7 +36,7 @@ public: { std::lock_guard lock(mutex_); auto logger_name = new_logger->name(); - throw_if_exists(logger_name); + throw_if_exists_(logger_name); loggers_[logger_name] = new_logger; } @@ -44,7 +44,7 @@ public: { std::lock_guard lock(mutex_); auto logger_name = new_logger->name(); - throw_if_exists(logger_name); + throw_if_exists_(logger_name); // create default formatter if not exists @@ -162,7 +162,7 @@ public: private: registry_t() = default; - void throw_if_exists(const std::string &logger_name) + void throw_if_exists_(const std::string &logger_name) { if (loggers_.find(logger_name) != loggers_.end()) { diff --git a/include/spdlog/details/thread_pool.h b/include/spdlog/details/thread_pool.h index 9019823a..35641e43 100644 --- a/include/spdlog/details/thread_pool.h +++ b/include/spdlog/details/thread_pool.h @@ -95,7 +95,7 @@ public: } for (size_t i = 0; i < threads_n; i++) { - threads_.emplace_back(std::bind(&thread_pool::worker_loop, this)); + threads_.emplace_back(std::bind(&thread_pool::worker_loop_, this)); } } @@ -106,7 +106,7 @@ public: { for (size_t i = 0; i < threads_.size(); i++) { - post_async_msg(async_msg(async_msg_type::terminate), async_overflow_policy::block_retry); + post_async_msg_(async_msg(async_msg_type::terminate), async_overflow_policy::block_retry); } for (auto &t : threads_) @@ -123,12 +123,12 @@ public: void post_log(async_logger_ptr &&worker_ptr, details::log_msg &&msg, async_overflow_policy overflow_policy) { async_msg async_m(std::forward(worker_ptr), async_msg_type::log, std::forward(msg)); - post_async_msg(std::move(async_m), overflow_policy); + post_async_msg_(std::move(async_m), overflow_policy); } void post_flush(async_logger_ptr &&worker_ptr, async_overflow_policy overflow_policy) { - post_async_msg(async_msg(std::move(worker_ptr), async_msg_type::flush), overflow_policy); + post_async_msg_(async_msg(std::move(worker_ptr), async_msg_type::flush), overflow_policy); } private: @@ -136,7 +136,7 @@ private: std::vector threads_; - void post_async_msg(async_msg &&new_msg, async_overflow_policy overflow_policy) + void post_async_msg_(async_msg &&new_msg, async_overflow_policy overflow_policy) { if (overflow_policy == async_overflow_policy::block_retry) { @@ -148,14 +148,14 @@ private: } } - void worker_loop() + void worker_loop_() { - while (process_next_msg()) {}; + while (process_next_msg_()) {}; } // process next message in the queue // return true if this thread should still be active (while no terminate msg was received) - bool process_next_msg() + bool process_next_msg_() { async_msg incoming_async_msg; bool dequeued = q_.dequeue_for(incoming_async_msg, std::chrono::seconds(10)); diff --git a/include/spdlog/formatter.h b/include/spdlog/formatter.h index ec343f83..39d86524 100644 --- a/include/spdlog/formatter.h +++ b/include/spdlog/formatter.h @@ -17,5 +17,3 @@ public: virtual void format(const details::log_msg &msg, fmt::memory_buffer &dest) = 0; }; } // namespace spdlog - -#include "details/pattern_formatter_impl.h" diff --git a/include/spdlog/sinks/android_sink.h b/include/spdlog/sinks/android_sink.h index 9b1259e1..97515db9 100644 --- a/include/spdlog/sinks/android_sink.h +++ b/include/spdlog/sinks/android_sink.h @@ -38,7 +38,7 @@ public: void log(const details::log_msg &msg) override { - const android_LogPriority priority = convert_to_android(msg.level); + const android_LogPriority priority = convert_to_android_(msg.level); fmt::memory_buffer formatted; if (use_raw_msg_) { @@ -70,7 +70,7 @@ public: void flush() override {} private: - static android_LogPriority convert_to_android(spdlog::level::level_enum level) + static android_LogPriority convert_to_android_(spdlog::level::level_enum level) { switch (level) { diff --git a/include/spdlog/sinks/ansicolor_sink.h b/include/spdlog/sinks/ansicolor_sink.h index f72f6196..961062ac 100644 --- a/include/spdlog/sinks/ansicolor_sink.h +++ b/include/spdlog/sinks/ansicolor_sink.h @@ -5,9 +5,9 @@ #pragma once +#include "spdlog/details/console_globals.h" #include "spdlog/details/null_mutex.h" #include "spdlog/details/os.h" -#include "spdlog/details/console_globals.h" #include #include @@ -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 ConsoleMutexTrait::mutex_t; + using mutex_t = typename ConsoleMutex::mutex_t; ansicolor_sink() - : target_file_(StreamTrait::stream()) - , mutex_(ConsoleMutexTrait::console_mutex()) + : target_file_(TargetStream::stream()) + , mutex_(ConsoleMutex::console_mutex()) { should_do_colors_ = details::os::in_terminal(target_file_) && details::os::is_color_terminal(); diff --git a/include/spdlog/sinks/msvc_sink.h b/include/spdlog/sinks/msvc_sink.h index b12a1b49..4c45635a 100644 --- a/include/spdlog/sinks/msvc_sink.h +++ b/include/spdlog/sinks/msvc_sink.h @@ -27,9 +27,9 @@ public: explicit msvc_sink() {} protected: - void sink_it_(const details::log_msg &msg) override + void sink_it_(const details::log_msg &, const fmt::memory_buffer &formatted) override { - OutputDebugStringA(msg.formatted.c_str()); + OutputDebugStringA(fmt::to_string(formatted).c_str()); } void flush_() override {} @@ -38,6 +38,9 @@ protected: using msvc_sink_mt = msvc_sink; using msvc_sink_st = msvc_sink; +using windebug_sink_mt = msvc_sink_mt; +using windebug_sink_st = msvc_sink_st; + } // namespace sinks } // namespace spdlog diff --git a/include/spdlog/sinks/sink.h b/include/spdlog/sinks/sink.h index c18e5781..2c2a764c 100644 --- a/include/spdlog/sinks/sink.h +++ b/include/spdlog/sinks/sink.h @@ -6,6 +6,7 @@ #pragma once #include "spdlog/details/log_msg.h" +#include "spdlog/details/pattern_formatter_impl.h" #include "spdlog/formatter.h" namespace spdlog { diff --git a/include/spdlog/sinks/stdout_sinks.h b/include/spdlog/sinks/stdout_sinks.h index 80854730..5d25a954 100644 --- a/include/spdlog/sinks/stdout_sinks.h +++ b/include/spdlog/sinks/stdout_sinks.h @@ -5,26 +5,27 @@ #pragma once +#include "spdlog/details/console_globals.h" #include "spdlog/details/null_mutex.h" -#include "spdlog/details/traits.h" #include "spdlog/spdlog.h" #include #include #include +#include namespace spdlog { namespace sinks { -template +template class stdout_sink : public sink { public: - using mutex_t = typename ConsoleMutexTrait::mutex_t; + using mutex_t = typename ConsoleMutex::mutex_t; stdout_sink() - : mutex_(ConsoleMutexTrait::console_mutex()) - , file_(StdoutTrait::stream()) + : mutex_(ConsoleMutex::console_mutex()) + , file_(TargetStream::stream()) { } ~stdout_sink() = default; @@ -35,14 +36,16 @@ public: void log(const details::log_msg &msg) override { std::lock_guard lock(mutex_); - fwrite(msg.formatted.data(), sizeof(char), msg.formatted.size(), file_); - fflush(StdoutTrait::stream()); + 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 lock(mutex_); - fflush(StdoutTrait::stream()); + fflush(TargetStream::stream()); } private: @@ -50,10 +53,11 @@ private: FILE *file_; }; -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 stdout_sink_mt = stdout_sink; +using stdout_sink_st = stdout_sink; + +using stderr_sink_mt = stdout_sink; +using stderr_sink_st = stdout_sink; } // namespace sinks diff --git a/include/spdlog/sinks/wincolor_sink.h b/include/spdlog/sinks/wincolor_sink.h index b4604e35..7199db11 100644 --- a/include/spdlog/sinks/wincolor_sink.h +++ b/include/spdlog/sinks/wincolor_sink.h @@ -7,8 +7,8 @@ #include "../fmt/fmt.h" #include "spdlog/common.h" -#include "spdlog/details/null_mutex.h" #include "spdlog/details/console_globals.h" +#include "spdlog/details/null_mutex.h" #include "spdlog/sinks/sink.h" #include @@ -22,7 +22,7 @@ namespace sinks { /* * Windows color console sink. Uses WriteConsoleA to write to the console with colors */ -template +template class wincolor_sink : public sink { public: @@ -34,8 +34,8 @@ public: const WORD YELLOW = FOREGROUND_RED | FOREGROUND_GREEN; wincolor_sink() - : out_handle_(HandleTrait::handle()) - , mutex_(ConsoleMutexTrait::console_mutex()) + : out_handle_(OutHandle::handle()) + , mutex_(ConsoleMutex::console_mutex()) { colors_[level::trace] = WHITE; colors_[level::debug] = CYAN; @@ -90,7 +90,7 @@ public: } private: - using mutex_t = typename ConsoleMutexTrait::mutex_t; + using mutex_t = typename ConsoleMutex::mutex_t; // set color and return the orig console attributes (for resetting later) WORD set_console_attribs(WORD attribs) { diff --git a/include/spdlog/sinks/windebug_sink.h b/include/spdlog/sinks/windebug_sink.h deleted file mode 100644 index 15ffdc2e..00000000 --- a/include/spdlog/sinks/windebug_sink.h +++ /dev/null @@ -1,27 +0,0 @@ -// -// Copyright(c) 2017 Alexander Dalshov. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) -// - -#pragma once - -#if defined(_WIN32) - -#include "spdlog/sinks/msvc_sink.h" - -namespace spdlog { -namespace sinks { - -/* - * Windows debug sink (logging using OutputDebugStringA, synonym for msvc_sink) - */ -template -using windebug_sink = msvc_sink; - -using windebug_sink_mt = msvc_sink_mt; -using windebug_sink_st = msvc_sink_st; - -} // namespace sinks -} // namespace spdlog - -#endif