Fixed issue where msvc_sink did not correctly output Unicode when defining SPDLOG_WCHAR_TO_UTF8_SUPPORT macro.

We should use OutputDebugStringW, instead of OutputDebugStringA.
pull/2653/head
cpp 3 years ago
parent be1aed9143
commit 7bb39ed051

@ -11,8 +11,15 @@
# include <mutex> # include <mutex>
# include <string> # include <string>
# include <memory>
// Avoid including windows.h (https://stackoverflow.com/a/30741042) // 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) void __stdcall OutputDebugStringA(const char *lpOutputString);
extern "C" __declspec(dllimport) int __stdcall IsDebuggerPresent(); extern "C" __declspec(dllimport) int __stdcall IsDebuggerPresent();
@ -39,7 +46,24 @@ protected:
memory_buf_t formatted; memory_buf_t formatted;
base_sink<Mutex>::formatter_->format(msg, formatted); base_sink<Mutex>::formatter_->format(msg, formatted);
formatted.push_back('\0'); // add a null terminator for OutputDebugStringA 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<int32_t>(std::size(formatted)), nullptr, 0)};
if (size == 0)
{
OutputDebugStringA(std::data(formatted));
return;
}
auto result{std::make_unique<wchar_t[]>(size)};
MultiByteToWideChar(CP_UTF8, 0, std::data(formatted), static_cast<int32_t>(std::size(formatted)), result.get(), size);
OutputDebugStringW(result.get());
# else
OutputDebugStringA(std::data(formatted));
# endif
} }
void flush_() override {} void flush_() override {}

Loading…
Cancel
Save