diff --git a/include/spdlog/sinks/msvc_sink.h b/include/spdlog/sinks/msvc_sink.h index 708b35ac..629a1d80 100644 --- a/include/spdlog/sinks/msvc_sink.h +++ b/include/spdlog/sinks/msvc_sink.h @@ -11,8 +11,15 @@ # include # include +# include // Avoid including windows.h (https://stackoverflow.com/a/30741042) +# ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT +extern "C" __declspec(dllimport) void __stdcall OutputDebugStringW(const wchar_t *lpOutputString); +extern "C" __declspec(dllimport) int __stdcall MultiByteToWideChar(UINT CodePage, DWORD dwFlags, + LPCCH lpMultiByteStr, + int cbMultiByte, LPWSTR lpWideCharStr, int cchWideChar); +# endif extern "C" __declspec(dllimport) void __stdcall OutputDebugStringA(const char *lpOutputString); extern "C" __declspec(dllimport) int __stdcall IsDebuggerPresent(); @@ -39,7 +46,24 @@ protected: memory_buf_t formatted; base_sink::formatter_->format(msg, formatted); formatted.push_back('\0'); // add a null terminator for OutputDebugStringA - OutputDebugStringA(formatted.data()); +# ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT + auto const size{MultiByteToWideChar(CP_UTF8, 0, std::data(formatted), static_cast(std::size(formatted)), nullptr, 0)}; + + if (size == 0) + { + OutputDebugStringA(std::data(formatted)); + + return; + } + + auto result{std::make_unique(size)}; + + MultiByteToWideChar(CP_UTF8, 0, std::data(formatted), static_cast(std::size(formatted)), result.get(), size); + + OutputDebugStringW(result.get()); +# else + OutputDebugStringA(std::data(formatted)); +# endif } void flush_() override {}