From 904e56702e9350a1734fa69840ffe0b3874506e6 Mon Sep 17 00:00:00 2001 From: Dominik Grabiec Date: Mon, 12 Feb 2024 17:17:05 +0100 Subject: [PATCH] Add wide character formatting and output support to wincolor_sink. Fixes printing of unicode characters to the windows console such as microsecond suffix for std::chrono types. --- include/spdlog/sinks/wincolor_sink-inl.h | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/include/spdlog/sinks/wincolor_sink-inl.h b/include/spdlog/sinks/wincolor_sink-inl.h index 10b8a74a..fcca7c96 100644 --- a/include/spdlog/sinks/wincolor_sink-inl.h +++ b/include/spdlog/sinks/wincolor_sink-inl.h @@ -134,19 +134,36 @@ void SPDLOG_INLINE wincolor_sink::print_range_(const memory_buf_t size_t start, size_t end) { if (end > start) { + #if defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT) + wmemory_buf_t wformatted; + details::os::utf8_to_wstrbuf(string_view_t(formatted.data() + start, end - start), + wformatted); + auto size = static_cast(wformatted.size()); + auto ignored = ::WriteConsoleW(static_cast(out_handle_), wformatted.data(), size, + nullptr, nullptr); + #else auto size = static_cast(end - start); auto ignored = ::WriteConsoleA(static_cast(out_handle_), formatted.data() + start, size, nullptr, nullptr); + #endif (void)(ignored); } } template void SPDLOG_INLINE wincolor_sink::write_to_file_(const memory_buf_t &formatted) { - auto size = static_cast(formatted.size()); DWORD bytes_written = 0; +#if defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT) + wmemory_buf_t wformatted; + details::os::utf8_to_wstrbuf(string_view_t(formatted.data(), formatted.size()), wformatted); + auto size = static_cast(wformatted.size() * sizeof(WCHAR)); + auto ignored = ::WriteFile(static_cast(out_handle_), wformatted.data(), size, + &bytes_written, nullptr); +#else + auto size = static_cast(formatted.size()); auto ignored = ::WriteFile(static_cast(out_handle_), formatted.data(), size, &bytes_written, nullptr); +#endif (void)(ignored); }