diff --git a/include/spdlog/sinks/wincolor_sink-inl.h b/include/spdlog/sinks/wincolor_sink-inl.h new file mode 100644 index 00000000..e778496f --- /dev/null +++ b/include/spdlog/sinks/wincolor_sink-inl.h @@ -0,0 +1,102 @@ +#pragma once + + +namespace spdlog { +namespace sinks { +template +SPDLOG_INLINE wincolor_sink::wincolor_sink() + : out_handle_(TargetStream::handle()) + , mutex_(ConsoleMutex::mutex()) +{ + colors_[level::trace] = WHITE; + colors_[level::debug] = CYAN; + colors_[level::info] = GREEN; + colors_[level::warn] = YELLOW | BOLD; + colors_[level::err] = RED | BOLD; // red bold + colors_[level::critical] = BACKGROUND_RED | WHITE | BOLD; // white bold on red background + colors_[level::off] = 0; +} + +template +SPDLOG_INLINE SPDLOG_INLINE wincolor_sink::~wincolor_sink() +{ + this->flush(); +} + +// change the color for the given level +template +void SPDLOG_INLINE wincolor_sink::set_color(level::level_enum level, WORD color) +{ + std::lock_guard lock(mutex_); + colors_[level] = color; +} + +template +void SPDLOG_INLINE wincolor_sink::log(const details::log_msg &msg) +{ + std::lock_guard lock(mutex_); + fmt::memory_buffer formatted; + formatter_->format(msg, formatted); + if (msg.color_range_end > msg.color_range_start) + { + // before color range + print_range_(formatted, 0, msg.color_range_start); + + // in color range + auto orig_attribs = set_console_attribs(colors_[msg.level]); + print_range_(formatted, msg.color_range_start, msg.color_range_end); + ::SetConsoleTextAttribute(out_handle_, + orig_attribs); // reset to orig colors + // after color range + print_range_(formatted, msg.color_range_end, formatted.size()); + } + else // print without colors if color range is invalid + { + print_range_(formatted, 0, formatted.size()); + } +} + +template +void SPDLOG_INLINE wincolor_sink::flush() +{ + // windows console always flushed? +} + +template +void SPDLOG_INLINE wincolor_sink::set_pattern(const std::string &pattern) +{ + std::lock_guard lock(mutex_); + formatter_ = std::unique_ptr(new pattern_formatter(pattern)); +} + +template +void SPDLOG_INLINE wincolor_sink::set_formatter(std::unique_ptr sink_formatter) +{ + std::lock_guard lock(mutex_); + formatter_ = std::move(sink_formatter); +} + +// set color and return the orig console attributes (for resetting later) +template +WORD SPDLOG_INLINE wincolor_sink::set_console_attribs(WORD attribs) +{ + CONSOLE_SCREEN_BUFFER_INFO orig_buffer_info; + ::GetConsoleScreenBufferInfo(out_handle_, &orig_buffer_info); + WORD back_color = orig_buffer_info.wAttributes; + // retrieve the current background color + back_color &= static_cast(~(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY)); + // keep the background color unchanged + ::SetConsoleTextAttribute(out_handle_, attribs | back_color); + return orig_buffer_info.wAttributes; // return orig attribs +} + +// print a range of formatted message to console +template +void SPDLOG_INLINE wincolor_sink::print_range_(const fmt::memory_buffer &formatted, size_t start, size_t end) +{ + auto size = static_cast(end - start); + ::WriteConsoleA(out_handle_, formatted.data() + start, size, nullptr, nullptr); +} + +} +} // namespace spdlog \ No newline at end of file diff --git a/include/spdlog/sinks/wincolor_sink.h b/include/spdlog/sinks/wincolor_sink.h index 70ee4a60..fd17b3d6 100644 --- a/include/spdlog/sinks/wincolor_sink.h +++ b/include/spdlog/sinks/wincolor_sink.h @@ -24,7 +24,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,100 +35,30 @@ public: const WORD WHITE = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE; const WORD YELLOW = FOREGROUND_RED | FOREGROUND_GREEN; - wincolor_sink() - : out_handle_(OutHandle::handle()) - , mutex_(ConsoleMutex::mutex()) - { - colors_[level::trace] = WHITE; - colors_[level::debug] = CYAN; - colors_[level::info] = GREEN; - colors_[level::warn] = YELLOW | BOLD; - colors_[level::err] = RED | BOLD; // red bold - colors_[level::critical] = BACKGROUND_RED | WHITE | BOLD; // white bold on red background - colors_[level::off] = 0; - } - - ~wincolor_sink() override - { - this->flush(); - } + wincolor_sink(); + ~wincolor_sink() override; wincolor_sink(const wincolor_sink &other) = delete; wincolor_sink &operator=(const wincolor_sink &other) = delete; // change the color for the given level - void set_color(level::level_enum level, WORD color) - { - std::lock_guard lock(mutex_); - colors_[level] = color; - } - - void log(const details::log_msg &msg) final override - { - std::lock_guard lock(mutex_); - fmt::memory_buffer formatted; - formatter_->format(msg, formatted); - if (msg.color_range_end > msg.color_range_start) - { - // before color range - print_range_(formatted, 0, msg.color_range_start); - - // in color range - auto orig_attribs = set_console_attribs(colors_[msg.level]); - print_range_(formatted, msg.color_range_start, msg.color_range_end); - ::SetConsoleTextAttribute(out_handle_, - orig_attribs); // reset to orig colors - // after color range - print_range_(formatted, msg.color_range_end, formatted.size()); - } - else // print without colors if color range is invalid - { - print_range_(formatted, 0, formatted.size()); - } - } - - void flush() final override - { - // windows console always flushed? - } - - void set_pattern(const std::string &pattern) override final - { - std::lock_guard lock(mutex_); - formatter_ = std::unique_ptr(new pattern_formatter(pattern)); - } - - void set_formatter(std::unique_ptr sink_formatter) override final - { - std::lock_guard lock(mutex_); - formatter_ = std::move(sink_formatter); - } + void set_color(level::level_enum level, WORD 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; + private: - using mutex_t = typename ConsoleMutex::mutex_t; - // set color and return the orig console attributes (for resetting later) - WORD set_console_attribs(WORD attribs) - { - CONSOLE_SCREEN_BUFFER_INFO orig_buffer_info; - ::GetConsoleScreenBufferInfo(out_handle_, &orig_buffer_info); - WORD back_color = orig_buffer_info.wAttributes; - // retrieve the current background color - back_color &= static_cast(~(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY)); - // keep the background color unchanged - ::SetConsoleTextAttribute(out_handle_, attribs | back_color); - return orig_buffer_info.wAttributes; // return orig attribs - } - - // print a range of formatted message to console - void print_range_(const fmt::memory_buffer &formatted, size_t start, size_t end) - { - auto size = static_cast(end - start); - ::WriteConsoleA(out_handle_, formatted.data() + start, size, nullptr, nullptr); - } - + using mutex_t = typename ConsoleMutex::mutex_t; HANDLE out_handle_; mutex_t &mutex_; std::unordered_map colors_; + + // set color and return the orig console attributes (for resetting later) + WORD set_console_attribs(WORD attribs); + // print a range of formatted message to console + void print_range_(const fmt::memory_buffer &formatted, size_t start, size_t end); }; using wincolor_stdout_sink_mt = wincolor_sink; diff --git a/src/spdlog.cpp b/src/spdlog.cpp index 83299175..319956a4 100644 --- a/src/spdlog.cpp +++ b/src/spdlog.cpp @@ -56,12 +56,22 @@ template class spdlog::sinks::rotating_file_sink; #include "spdlog/details/thread_pool-inl.h" template class spdlog::details::mpmc_blocking_queue; +#ifndef _WIN32 #include "spdlog/sinks/ansicolor_sink.h" #include "spdlog/sinks/ansicolor_sink-inl.h" template class spdlog::sinks::ansicolor_sink; template class spdlog::sinks::ansicolor_sink; template class spdlog::sinks::ansicolor_sink; template class spdlog::sinks::ansicolor_sink; +#else +#include "spdlog/sinks/wincolor_sink.h" +#include "spdlog/sinks/wincolor_sink-inl.h" +template class spdlog::sinks::wincolor_sink; +template class spdlog::sinks::wincolor_sink; +template class spdlog::sinks::wincolor_sink; +template class spdlog::sinks::wincolor_sink; +#endif + // fmt_helper templates #include "spdlog/details/fmt_helper.h"