diff --git a/CMakeLists.txt b/CMakeLists.txt index 882aa90e..67cf3a20 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -156,6 +156,7 @@ message(STATUS "Build type: " ${CMAKE_BUILD_TYPE}) # Static/Shared library # --------------------------------------------------------------------------------------- set(SPDLOG_SRCS src/spdlog.cpp src/stdout_sinks.cpp src/color_sinks.cpp src/file_sinks.cpp src/async.cpp src/cfg.cpp) +list(APPEND SPDLOG_SRCS include/spdlog/sinks/ansicolor_sink-inl.h) if(NOT SPDLOG_USE_STD_FORMAT AND NOT SPDLOG_FMT_EXTERNAL AND NOT SPDLOG_FMT_EXTERNAL_HO) list(APPEND SPDLOG_SRCS src/bundled_fmtlib_format.cpp) diff --git a/include/spdlog/sinks/ansicolor_sink-inl.h b/include/spdlog/sinks/ansicolor_sink-inl.h index 6a23f6c7..84831961 100644 --- a/include/spdlog/sinks/ansicolor_sink-inl.h +++ b/include/spdlog/sinks/ansicolor_sink-inl.h @@ -10,6 +10,10 @@ #include #include +#if defined(_WIN32) + #include +#endif + namespace spdlog { namespace sinks { @@ -113,14 +117,28 @@ SPDLOG_INLINE void ansicolor_sink::set_color_mode_(color_mode mode template SPDLOG_INLINE void ansicolor_sink::print_ccode_( const string_view_t &color_code) const { +#ifdef _WIN32 + DWORD bytes_written = 0; + HANDLE h = reinterpret_cast(_get_osfhandle(_fileno(target_file_))); + // WriteFile bypasses the extra \r injection + WriteFile(h, color_code.data(), static_cast(color_code.size()), &bytes_written, nullptr); +#else details::os::fwrite_bytes(color_code.data(), color_code.size(), target_file_); +#endif } template SPDLOG_INLINE void ansicolor_sink::print_range_(const memory_buf_t &formatted, size_t start, size_t end) const { +#ifdef _WIN32 + DWORD bytes_written = 0; + HANDLE h = reinterpret_cast(_get_osfhandle(_fileno(target_file_))); + WriteFile(h, formatted.data() + start, static_cast(end - start), &bytes_written, + nullptr); +#else details::os::fwrite_bytes(formatted.data() + start, end - start, target_file_); +#endif } template