diff --git a/lldb/source/Host/windows/Host.cpp b/lldb/source/Host/windows/Host.cpp index 4e747f77afc0e..4277b8edb38e5 100644 --- a/lldb/source/Host/windows/Host.cpp +++ b/lldb/source/Host/windows/Host.cpp @@ -22,6 +22,7 @@ #include "lldb/Utility/StreamString.h" #include "lldb/Utility/StructuredData.h" +#include "llvm/ADT/SmallVector.h" #include "llvm/ADT/StringRef.h" #include "llvm/Support/ConvertUTF.h" #include "llvm/Support/ManagedStatic.h" @@ -32,6 +33,8 @@ using namespace lldb; using namespace lldb_private; +using llvm::sys::windows::UTF8ToUTF16; + static bool GetTripleForProcess(const FileSpec &executable, llvm::Triple &triple) { // Open the PE File as a binary file, and parse just enough information to @@ -325,31 +328,18 @@ class WindowsEventLog { static llvm::ManagedStatic event_log; -static std::wstring AnsiToUtf16(const std::string &ansi) { - if (ansi.empty()) - return {}; - - const int unicode_length = - MultiByteToWideChar(CP_ACP, 0, ansi.c_str(), -1, nullptr, 0); - if (unicode_length == 0) - return {}; - - std::wstring unicode(unicode_length, L'\0'); - MultiByteToWideChar(CP_ACP, 0, ansi.c_str(), -1, &unicode[0], unicode_length); - return unicode; -} - void Host::SystemLog(Severity severity, llvm::StringRef message) { + if (message.empty()) + return; + HANDLE h = event_log->GetHandle(); if (!h) return; - std::wstring wide_message = AnsiToUtf16(message.str()); - if (wide_message.empty()) + llvm::SmallVector argsUTF16; + if (UTF8ToUTF16(message.str(), argsUTF16)) return; - LPCWSTR msg_ptr = wide_message.c_str(); - WORD event_type; switch (severity) { case lldb::eSeverityWarning: @@ -363,5 +353,7 @@ void Host::SystemLog(Severity severity, llvm::StringRef message) { event_type = EVENTLOG_INFORMATION_TYPE; } - ReportEventW(h, event_type, 0, 0, nullptr, 1, 0, &msg_ptr, nullptr); + LPCWSTR messages[1] = {argsUTF16.data()}; + ReportEventW(h, event_type, 0, 0, nullptr, std::size(messages), 0, messages, + nullptr); }