Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


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)
Expand Down
18 changes: 18 additions & 0 deletions include/spdlog/sinks/ansicolor_sink-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
#include <spdlog/details/os.h>
#include <spdlog/pattern_formatter.h>

#if defined(_WIN32)
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please use #ifdef _WIN32

#include <spdlog/details/windows_include.h>
#endif

namespace spdlog {
namespace sinks {

Expand Down Expand Up @@ -113,14 +117,28 @@ SPDLOG_INLINE void ansicolor_sink<ConsoleMutex>::set_color_mode_(color_mode mode
template <typename ConsoleMutex>
SPDLOG_INLINE void ansicolor_sink<ConsoleMutex>::print_ccode_(
const string_view_t &color_code) const {
#ifdef _WIN32
DWORD bytes_written = 0;
HANDLE h = reinterpret_cast<HANDLE>(_get_osfhandle(_fileno(target_file_)));
// 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_);
#endif
}

template <typename ConsoleMutex>
SPDLOG_INLINE void ansicolor_sink<ConsoleMutex>::print_range_(const memory_buf_t &formatted,
size_t start,
size_t end) const {
#ifdef _WIN32
DWORD bytes_written = 0;
HANDLE h = reinterpret_cast<HANDLE>(_get_osfhandle(_fileno(target_file_)));
WriteFile(h, formatted.data() + start, static_cast<DWORD>(end - start), &bytes_written,
Copy link
Copy Markdown
Owner

@gabime gabime Nov 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_get_osfhandle shoild be called only once at construction time and store the result in a class member

nullptr);
#else
details::os::fwrite_bytes(formatted.data() + start, end - start, target_file_);
#endif
}

template <typename ConsoleMutex>
Expand Down
Loading