diff --git a/include/spdlog/sinks/ansicolor_sink.h b/include/spdlog/sinks/ansicolor_sink.h index 95313315..65858168 100644 --- a/include/spdlog/sinks/ansicolor_sink.h +++ b/include/spdlog/sinks/ansicolor_sink.h @@ -76,7 +76,7 @@ private: FILE *target_file_; bool should_do_colors_; std::array colors_; - void print_ccode_(const string_view_t &color_code); + void print_ccode_(const string_view_t color_code); void print_range_(const memory_buf_t &formatted, size_t start, size_t end); static std::string to_string_(const string_view_t sv); }; diff --git a/include/spdlog/sinks/wincolor_sink.h b/include/spdlog/sinks/wincolor_sink.h index 3091e47e..f04ef5c8 100644 --- a/include/spdlog/sinks/wincolor_sink.h +++ b/include/spdlog/sinks/wincolor_sink.h @@ -11,7 +11,7 @@ #include "../common.h" #include "../details/null_mutex.h" -#include "sink.h" +#include "base_sink.h" namespace spdlog { namespace sinks { @@ -19,8 +19,8 @@ namespace sinks { * Windows color console sink. Uses WriteConsoleA to write to the console with * colors */ -template -class wincolor_sink : public sink { +template +class wincolor_sink : public base_sink { public: wincolor_sink(void *out_handle, color_mode mode); ~wincolor_sink() override; @@ -29,24 +29,20 @@ public: wincolor_sink &operator=(const wincolor_sink &other) = delete; // change the color for the given level - void set_color(level level, std::uint16_t color); - void log(const details::log_msg &msg) final override; - void flush() final override; - void set_pattern(const std::string &pattern) override final; - void set_formatter(std::unique_ptr sink_formatter) override final; + void set_color(level level, std::uint16_t color); void set_color_mode(color_mode mode); -protected: - using mutex_t = typename ConsoleMutex::mutex_t; - void *out_handle_; - mutex_t &mutex_; - bool should_do_colors_; - std::unique_ptr formatter_; +private: + void *out_handle_; + bool should_do_colors_; std::array colors_; // set foreground color and return the orig console attributes (for resetting later) std::uint16_t set_foreground_color_(std::uint16_t attribs); + void sink_it_(const details::log_msg &msg) override; + void flush_() override; + // print a range of formatted message to console void print_range_(const memory_buf_t &formatted, size_t start, size_t end); @@ -56,22 +52,22 @@ protected: void set_color_mode_impl(color_mode mode); }; -template -class wincolor_stdout_sink : public wincolor_sink { +template +class wincolor_stdout_sink : public wincolor_sink { public: explicit wincolor_stdout_sink(color_mode mode = color_mode::automatic); }; -template -class wincolor_stderr_sink : public wincolor_sink { +template +class wincolor_stderr_sink : public wincolor_sink { public: explicit wincolor_stderr_sink(color_mode mode = color_mode::automatic); }; -using wincolor_stdout_sink_mt = wincolor_stdout_sink; -using wincolor_stdout_sink_st = wincolor_stdout_sink; +using wincolor_stdout_sink_mt = wincolor_stdout_sink; +using wincolor_stdout_sink_st = wincolor_stdout_sink; -using wincolor_stderr_sink_mt = wincolor_stderr_sink; -using wincolor_stderr_sink_st = wincolor_stderr_sink; +using wincolor_stderr_sink_mt = wincolor_stderr_sink; +using wincolor_stderr_sink_st = wincolor_stderr_sink; } // namespace sinks } // namespace spdlog diff --git a/src/sinks/stdout_sinks.cpp b/src/sinks/stdout_sinks.cpp index d740a3a8..445cccfa 100644 --- a/src/sinks/stdout_sinks.cpp +++ b/src/sinks/stdout_sinks.cpp @@ -51,7 +51,7 @@ void stdout_sink_base::sink_it_(const details::log_msg &msg) { } memory_buf_t formatted; - base_sink::formatter_->formatter_->format(msg, formatted); + base_sink::formatter_->format(msg, formatted); auto size = static_cast(formatted.size()); DWORD bytes_written = 0; bool ok = ::WriteFile(handle_, formatted.data(), size, &bytes_written, nullptr) != 0; diff --git a/src/sinks/wincolor_sink.cpp b/src/sinks/wincolor_sink.cpp index b48209b4..ad6ed78e 100644 --- a/src/sinks/wincolor_sink.cpp +++ b/src/sinks/wincolor_sink.cpp @@ -6,17 +6,16 @@ #include "spdlog/details/windows_include.h" #include "spdlog/sinks/wincolor_sink.h" #include "spdlog/common.h" -#include "spdlog/pattern_formatter.h" #include // clang-format on +#include +#include + namespace spdlog { namespace sinks { -template -wincolor_sink::wincolor_sink(void *out_handle, color_mode mode) - : out_handle_(out_handle), - mutex_(ConsoleMutex::mutex()), - formatter_(std::make_unique()) { +template +wincolor_sink::wincolor_sink(void *out_handle, color_mode mode) : out_handle_(out_handle) { set_color_mode_impl(mode); // set level colors colors_.at(level_to_number(level::trace)) = @@ -32,29 +31,28 @@ wincolor_sink::wincolor_sink(void *out_handle, color_mode mode) colors_.at(level_to_number(level::off)) = 0; } -template -wincolor_sink::~wincolor_sink() { +template +wincolor_sink::~wincolor_sink() { this->flush(); } // change the color for the given level -template -void wincolor_sink::set_color(level level, std::uint16_t color) { - std::lock_guard lock(mutex_); +template +void wincolor_sink::set_color(level level, std::uint16_t color) { + std::lock_guard lock(base_sink::mutex_); colors_[level_to_number(level)] = color; } -template -void wincolor_sink::log(const details::log_msg &msg) { +template +void wincolor_sink::sink_it_(const details::log_msg &msg) { if (out_handle_ == nullptr || out_handle_ == INVALID_HANDLE_VALUE) { return; } - - std::lock_guard lock(mutex_); + msg.color_range_start = 0; msg.color_range_end = 0; memory_buf_t formatted; - formatter_->format(msg, formatted); + base_sink::formatter_->format(msg, formatted); if (should_do_colors_ && msg.color_range_end > msg.color_range_start) { // before color range print_range_(formatted, 0, msg.color_range_start); @@ -71,31 +69,20 @@ void wincolor_sink::log(const details::log_msg &msg) { } } -template -void wincolor_sink::flush() { +template +void wincolor_sink::flush_() { // windows console always flushed? } -template -void wincolor_sink::set_pattern(const std::string &pattern) { - std::lock_guard lock(mutex_); - formatter_ = std::unique_ptr(new pattern_formatter(pattern)); -} -template -void wincolor_sink::set_formatter(std::unique_ptr sink_formatter) { - std::lock_guard lock(mutex_); - formatter_ = std::move(sink_formatter); -} - -template -void wincolor_sink::set_color_mode(color_mode mode) { - std::lock_guard lock(mutex_); +template +void wincolor_sink::set_color_mode(color_mode mode) { + std::lock_guard lock(base_sink::mutex_); set_color_mode_impl(mode); } -template -void wincolor_sink::set_color_mode_impl(color_mode mode) { +template +void wincolor_sink::set_color_mode_impl(color_mode mode) { if (mode == color_mode::automatic) { // should do colors only if out_handle_ points to actual console. DWORD console_mode; @@ -107,8 +94,8 @@ void wincolor_sink::set_color_mode_impl(color_mode mode) { } // set foreground color and return the orig console attributes (for resetting later) -template -std::uint16_t wincolor_sink::set_foreground_color_(std::uint16_t attribs) { +template +std::uint16_t wincolor_sink::set_foreground_color_(std::uint16_t attribs) { CONSOLE_SCREEN_BUFFER_INFO orig_buffer_info; if (!::GetConsoleScreenBufferInfo(static_cast(out_handle_), &orig_buffer_info)) { // just return white if failed getting console info @@ -124,8 +111,8 @@ std::uint16_t wincolor_sink::set_foreground_color_(std::uint16_t a } // print a range of formatted message to console -template -void wincolor_sink::print_range_(const memory_buf_t &formatted, +template +void wincolor_sink::print_range_(const memory_buf_t &formatted, size_t start, size_t end) { if (end > start) { @@ -136,8 +123,8 @@ void wincolor_sink::print_range_(const memory_buf_t &formatted, } } -template -void wincolor_sink::write_to_file_(const memory_buf_t &formatted) { +template +void wincolor_sink::write_to_file_(const memory_buf_t &formatted) { auto size = static_cast(formatted.size()); DWORD bytes_written = 0; auto ignored = ::WriteFile(static_cast(out_handle_), formatted.data(), size, @@ -146,23 +133,25 @@ void wincolor_sink::write_to_file_(const memory_buf_t &formatted) } // wincolor_stdout_sink -template -wincolor_stdout_sink::wincolor_stdout_sink(color_mode mode) - : wincolor_sink(::GetStdHandle(STD_OUTPUT_HANDLE), mode) {} +template +wincolor_stdout_sink::wincolor_stdout_sink(color_mode mode) + : wincolor_sink(::GetStdHandle(STD_OUTPUT_HANDLE), mode) {} // wincolor_stderr_sink -template -wincolor_stderr_sink::wincolor_stderr_sink(color_mode mode) - : wincolor_sink(::GetStdHandle(STD_ERROR_HANDLE), mode) {} +template +wincolor_stderr_sink::wincolor_stderr_sink(color_mode mode) + : wincolor_sink(::GetStdHandle(STD_ERROR_HANDLE), mode) {} } // namespace sinks } // namespace spdlog // template instantiations -template class SPDLOG_API spdlog::sinks::wincolor_sink; -template class SPDLOG_API spdlog::sinks::wincolor_sink; -template class SPDLOG_API spdlog::sinks::wincolor_stdout_sink; -template class SPDLOG_API spdlog::sinks::wincolor_stdout_sink; -template class SPDLOG_API spdlog::sinks::wincolor_stderr_sink; -template class SPDLOG_API spdlog::sinks::wincolor_stderr_sink; +template class SPDLOG_API spdlog::sinks::wincolor_sink; +template class SPDLOG_API spdlog::sinks::wincolor_sink; + +template class SPDLOG_API spdlog::sinks::wincolor_stdout_sink; +template class SPDLOG_API spdlog::sinks::wincolor_stdout_sink; + +template class SPDLOG_API spdlog::sinks::wincolor_stderr_sink; +template class SPDLOG_API spdlog::sinks::wincolor_stderr_sink; #endif // _WIN32