From 4641347c3fc4831ea8a5c15671591f287b2ec49f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=92=80=E5=A2=83=E7=9F=B3?= Date: Sat, 25 Feb 2023 22:21:24 +0800 Subject: [PATCH] msvc_sink: support utf8 (#2651) * msvc_sink: support utf8 --- include/spdlog/sinks/msvc_sink.h | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/include/spdlog/sinks/msvc_sink.h b/include/spdlog/sinks/msvc_sink.h index 708b35ac..34d5f34e 100644 --- a/include/spdlog/sinks/msvc_sink.h +++ b/include/spdlog/sinks/msvc_sink.h @@ -7,13 +7,20 @@ #if defined(_WIN32) # include +# if defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT) +# include +# endif # include # include # include // Avoid including windows.h (https://stackoverflow.com/a/30741042) +#if defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT) +extern "C" __declspec(dllimport) void __stdcall OutputDebugStringW(const wchar_t *lpOutputString); +#else extern "C" __declspec(dllimport) void __stdcall OutputDebugStringA(const char *lpOutputString); +#endif extern "C" __declspec(dllimport) int __stdcall IsDebuggerPresent(); namespace spdlog { @@ -38,8 +45,14 @@ protected: } memory_buf_t formatted; base_sink::formatter_->format(msg, formatted); - formatted.push_back('\0'); // add a null terminator for OutputDebugStringA + formatted.push_back('\0'); // add a null terminator for OutputDebugString +#if defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT) + wmemory_buf_t wformatted; + details::os::utf8_to_wstrbuf(string_view_t(formatted.data(), formatted.size()), wformatted); + OutputDebugStringW(wformatted.data()); +#else OutputDebugStringA(formatted.data()); +#endif } void flush_() override {}