ANSI color sink on Windows

pull/3442/head
Mihir Patel 1 month ago
parent 322b7af491
commit d09e13d29d

@ -156,6 +156,7 @@ message(STATUS "Build type: " ${CMAKE_BUILD_TYPE})
# Static/Shared library # 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) 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) 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) list(APPEND SPDLOG_SRCS src/bundled_fmtlib_format.cpp)

@ -10,6 +10,13 @@
#include <spdlog/details/os.h> #include <spdlog/details/os.h>
#include <spdlog/pattern_formatter.h> #include <spdlog/pattern_formatter.h>
#if defined(_WIN32)
#include <spdlog/details/windows_include.h>
#include <fileapi.h> // WriteFile
#include <io.h> // _get_osfhandle
#include <stdio.h> // _fileno
#endif
namespace spdlog { namespace spdlog {
namespace sinks { namespace sinks {
@ -113,14 +120,30 @@ SPDLOG_INLINE void ansicolor_sink<ConsoleMutex>::set_color_mode_(color_mode mode
template <typename ConsoleMutex> template <typename ConsoleMutex>
SPDLOG_INLINE void ansicolor_sink<ConsoleMutex>::print_ccode_( SPDLOG_INLINE void ansicolor_sink<ConsoleMutex>::print_ccode_(
const string_view_t &color_code) const { const string_view_t &color_code) const {
#ifdef _WIN32
DWORD bytes_written = 0;
HANDLE h =
(target_file_ == stdout) ? GetStdHandle(STD_OUTPUT_HANDLE) : GetStdHandle(STD_ERROR_HANDLE);
// WriteFile bypasses the extra \r injection
WriteFile(h, color_code.data(), static_cast<DWORD>(color_code.size()), &bytes_written, nullptr);
#else
details::os::fwrite_bytes(color_code.data(), color_code.size(), target_file_); details::os::fwrite_bytes(color_code.data(), color_code.size(), target_file_);
#endif
} }
template <typename ConsoleMutex> template <typename ConsoleMutex>
SPDLOG_INLINE void ansicolor_sink<ConsoleMutex>::print_range_(const memory_buf_t &formatted, SPDLOG_INLINE void ansicolor_sink<ConsoleMutex>::print_range_(const memory_buf_t &formatted,
size_t start, size_t start,
size_t end) const { size_t end) const {
#ifdef _WIN32
DWORD bytes_written = 0;
HANDLE h =
(target_file_ == stdout) ? GetStdHandle(STD_OUTPUT_HANDLE) : GetStdHandle(STD_ERROR_HANDLE);
WriteFile(h, formatted.data() + start, static_cast<DWORD>(end - start), &bytes_written,
nullptr);
#else
details::os::fwrite_bytes(formatted.data() + start, end - start, target_file_); details::os::fwrite_bytes(formatted.data() + start, end - start, target_file_);
#endif
} }
template <typename ConsoleMutex> template <typename ConsoleMutex>

Loading…
Cancel
Save