msvc_sink: support utf8 (#2651)

* msvc_sink: support utf8
This commit is contained in:
璀境石 2023-02-25 22:21:24 +08:00 committed by GitHub
parent 51bcff820e
commit 4641347c3f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -7,13 +7,20 @@
#if defined(_WIN32)
# include <spdlog/details/null_mutex.h>
# if defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT)
# include <spdlog/details/os.h>
# endif
# include <spdlog/sinks/base_sink.h>
# include <mutex>
# include <string>
// 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<Mutex>::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 {}