diff --git a/include/spdlog/sinks/wincolor_sink-inl.h b/include/spdlog/sinks/wincolor_sink-inl.h index fc37db87..1979e44e 100644 --- a/include/spdlog/sinks/wincolor_sink-inl.h +++ b/include/spdlog/sinks/wincolor_sink-inl.h @@ -7,22 +7,30 @@ #include #endif +#include +#include + #include #include namespace spdlog { namespace sinks { - template -SPDLOG_INLINE wincolor_sink::wincolor_sink(HANDLE out_handle, color_mode mode) - : out_handle_(out_handle) +SPDLOG_INLINE wincolor_sink::wincolor_sink(void *out_handle, color_mode mode) + : BOLD(FOREGROUND_INTENSITY) + , RED(FOREGROUND_RED) + , GREEN(FOREGROUND_GREEN) + , CYAN(FOREGROUND_GREEN | FOREGROUND_BLUE) + , WHITE(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE) + , YELLOW(FOREGROUND_RED | FOREGROUND_GREEN) + , out_handle_(out_handle) , mutex_(ConsoleMutex::mutex()) , formatter_(details::make_unique()) { // check if out_handle is points to the actual console. // ::GetConsoleMode() should return 0 if it is redirected or not valid console handle. DWORD console_mode; - in_console_ = ::GetConsoleMode(out_handle, &console_mode) != 0; + in_console_ = ::GetConsoleMode(static_cast(out_handle_), &console_mode) != 0; set_color_mode(mode); colors_[level::trace] = WHITE; @@ -42,7 +50,7 @@ SPDLOG_INLINE wincolor_sink::~wincolor_sink() // change the color for the given level template -void SPDLOG_INLINE wincolor_sink::set_color(level::level_enum level, WORD color) +void SPDLOG_INLINE wincolor_sink::set_color(level::level_enum level, std::uint16_t color) { std::lock_guard lock(mutex_); colors_[level] = color; @@ -67,10 +75,10 @@ void SPDLOG_INLINE wincolor_sink::log(const details::log_msg &msg) print_range_(formatted, 0, msg.color_range_start); // in color range - auto orig_attribs = set_foreground_color_(colors_[msg.level]); + auto orig_attribs = static_cast(set_foreground_color_(colors_[msg.level])); print_range_(formatted, msg.color_range_start, msg.color_range_end); // reset to orig colors - ::SetConsoleTextAttribute(out_handle_, orig_attribs); + ::SetConsoleTextAttribute(static_cast(out_handle_), orig_attribs); print_range_(formatted, msg.color_range_end, formatted.size()); } else // print without colors if color range is invalid (or color is disabled) @@ -118,16 +126,16 @@ void SPDLOG_INLINE wincolor_sink::set_color_mode(color_mode mode) // set foreground color and return the orig console attributes (for resetting later) template -WORD SPDLOG_INLINE wincolor_sink::set_foreground_color_(WORD attribs) +std::uint16_t SPDLOG_INLINE wincolor_sink::set_foreground_color_(std::uint16_t attribs) { CONSOLE_SCREEN_BUFFER_INFO orig_buffer_info; - ::GetConsoleScreenBufferInfo(out_handle_, &orig_buffer_info); + ::GetConsoleScreenBufferInfo(static_cast(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 + ::SetConsoleTextAttribute(static_cast(out_handle_), static_cast(attribs) | back_color); + return static_cast(orig_buffer_info.wAttributes); // return orig attribs } // print a range of formatted message to console @@ -135,7 +143,7 @@ template void SPDLOG_INLINE wincolor_sink::print_range_(const memory_buf_t &formatted, size_t start, size_t end) { auto size = static_cast(end - start); - ::WriteConsoleA(out_handle_, formatted.data() + start, size, nullptr, nullptr); + ::WriteConsoleA(static_cast(out_handle_), formatted.data() + start, size, nullptr, nullptr); } template @@ -147,7 +155,7 @@ void SPDLOG_INLINE wincolor_sink::write_to_file_(const memory_buf_ } auto size = static_cast(formatted.size()); DWORD bytes_written = 0; - bool ok = ::WriteFile(out_handle_, formatted.data(), size, &bytes_written, nullptr) != 0; + bool ok = ::WriteFile(static_cast(out_handle_), formatted.data(), size, &bytes_written, nullptr) != 0; if (!ok) { throw_spdlog_ex("wincolor_sink: ::WriteFile() failed. GetLastError(): " + std::to_string(::GetLastError())); @@ -165,6 +173,5 @@ template SPDLOG_INLINE wincolor_stderr_sink::wincolor_stderr_sink(color_mode mode) : wincolor_sink(::GetStdHandle(STD_ERROR_HANDLE), mode) {} - } // namespace sinks } // namespace spdlog diff --git a/include/spdlog/sinks/wincolor_sink.h b/include/spdlog/sinks/wincolor_sink.h index 8d4d08fd..bf7ebf79 100644 --- a/include/spdlog/sinks/wincolor_sink.h +++ b/include/spdlog/sinks/wincolor_sink.h @@ -12,9 +12,7 @@ #include #include #include - -#include -#include +#include namespace spdlog { namespace sinks { @@ -26,21 +24,21 @@ template class wincolor_sink : public sink { public: - const WORD BOLD = FOREGROUND_INTENSITY; - const WORD RED = FOREGROUND_RED; - const WORD GREEN = FOREGROUND_GREEN; - const WORD CYAN = FOREGROUND_GREEN | FOREGROUND_BLUE; - const WORD WHITE = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE; - const WORD YELLOW = FOREGROUND_RED | FOREGROUND_GREEN; + const std::uint16_t BOLD; + const std::uint16_t RED; + const std::uint16_t GREEN; + const std::uint16_t CYAN; + const std::uint16_t WHITE; + const std::uint16_t YELLOW; - wincolor_sink(HANDLE out_handle, color_mode mode); + wincolor_sink(void *out_handle, color_mode mode); ~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); + void set_color(level::level_enum 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; @@ -49,15 +47,15 @@ public: protected: using mutex_t = typename ConsoleMutex::mutex_t; - HANDLE out_handle_; + void *out_handle_; mutex_t &mutex_; bool in_console_; bool should_do_colors_; std::unique_ptr formatter_; - std::array colors_; + std::array colors_; // set foreground color and return the orig console attributes (for resetting later) - WORD set_foreground_color_(WORD attribs); + std::uint16_t set_foreground_color_(std::uint16_t attribs); // print a range of formatted message to console void print_range_(const memory_buf_t &formatted, size_t start, size_t end); @@ -85,7 +83,6 @@ using wincolor_stdout_sink_st = wincolor_stdout_sink using wincolor_stderr_sink_mt = wincolor_stderr_sink; using wincolor_stderr_sink_st = wincolor_stderr_sink; - } // namespace sinks } // namespace spdlog diff --git a/tests/utils.cpp b/tests/utils.cpp index 6b5b9103..32b7c03f 100644 --- a/tests/utils.cpp +++ b/tests/utils.cpp @@ -1,6 +1,8 @@ #include "includes.h" -#ifndef _WIN32 +#ifdef _WIN32 +#include +#else #include #include #endif