Skip to content

Commit

Permalink
msvc_sink: support utf8 (gabime#2651)
Browse files Browse the repository at this point in the history
* msvc_sink: support utf8
  • Loading branch information
璀境石 authored and xinjian committed Mar 3, 2023
1 parent 4cb13f0 commit 9ff2808
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion include/spdlog/sinks/msvc_sink.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -38,8 +45,14 @@ class msvc_sink : public base_sink<Mutex>
}
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 {}
Expand Down

0 comments on commit 9ff2808

Please sign in to comment.