From 7e83620ed1ded2344979e900fc2a2c430b51c0e3 Mon Sep 17 00:00:00 2001 From: dhmemi Date: Thu, 7 Apr 2022 20:48:52 +0800 Subject: [PATCH] feat(wincolor_sink): support print utf-8 string to non-unicode windows console. --- include/spdlog/sinks/wincolor_sink-inl.h | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/include/spdlog/sinks/wincolor_sink-inl.h b/include/spdlog/sinks/wincolor_sink-inl.h index be3d80a2..6d95ed33 100644 --- a/include/spdlog/sinks/wincolor_sink-inl.h +++ b/include/spdlog/sinks/wincolor_sink-inl.h @@ -145,8 +145,14 @@ void SPDLOG_INLINE wincolor_sink::print_range_(const memory_buf_t { if (end > start) { - auto size = static_cast(end - start); - auto ignored = ::WriteConsoleA(static_cast(out_handle_), formatted.data() + start, size, nullptr, nullptr); + auto size = end - start; +#ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT + wmemory_buf_t buf; + details::os::utf8_to_wstrbuf(string_view_t(formatted.data() + start, size), buf); + auto ignored = ::WriteConsoleW(static_cast(out_handle_), buf.data(), static_cast(buf.size()), nullptr, nullptr); +#else + auto ignored = ::WriteConsoleA(static_cast(out_handle_), formatted.data() + start, static_cast(size), nullptr, nullptr); +#endif (void)(ignored); } }