2222#include " lldb/Utility/StreamString.h"
2323#include " lldb/Utility/StructuredData.h"
2424
25+ #include " llvm/ADT/SmallVector.h"
2526#include " llvm/ADT/StringRef.h"
2627#include " llvm/Support/ConvertUTF.h"
2728#include " llvm/Support/ManagedStatic.h"
3233using namespace lldb ;
3334using namespace lldb_private ;
3435
36+ using llvm::sys::windows::UTF8ToUTF16;
37+
3538static bool GetTripleForProcess (const FileSpec &executable,
3639 llvm::Triple &triple) {
3740 // Open the PE File as a binary file, and parse just enough information to
@@ -325,31 +328,18 @@ class WindowsEventLog {
325328
326329static llvm::ManagedStatic<WindowsEventLog> event_log;
327330
328- static std::wstring AnsiToUtf16 (const std::string &ansi) {
329- if (ansi.empty ())
330- return {};
331-
332- const int unicode_length =
333- MultiByteToWideChar (CP_ACP, 0 , ansi.c_str (), -1 , nullptr , 0 );
334- if (unicode_length == 0 )
335- return {};
336-
337- std::wstring unicode (unicode_length, L' \0 ' );
338- MultiByteToWideChar (CP_ACP, 0 , ansi.c_str (), -1 , &unicode[0 ], unicode_length);
339- return unicode;
340- }
341-
342331void Host::SystemLog (Severity severity, llvm::StringRef message) {
332+ if (message.empty ())
333+ return ;
334+
343335 HANDLE h = event_log->GetHandle ();
344336 if (!h)
345337 return ;
346338
347- std::wstring wide_message = AnsiToUtf16 (message. str ()) ;
348- if (wide_message. empty ( ))
339+ llvm::SmallVector< wchar_t , 1 > argsUTF16 ;
340+ if (UTF8ToUTF16 (message. str (), argsUTF16 ))
349341 return ;
350342
351- LPCWSTR msg_ptr = wide_message.c_str ();
352-
353343 WORD event_type;
354344 switch (severity) {
355345 case lldb::eSeverityWarning:
@@ -363,5 +353,7 @@ void Host::SystemLog(Severity severity, llvm::StringRef message) {
363353 event_type = EVENTLOG_INFORMATION_TYPE;
364354 }
365355
366- ReportEventW (h, event_type, 0 , 0 , nullptr , 1 , 0 , &msg_ptr, nullptr );
356+ LPCWSTR messages[1 ] = {argsUTF16.data ()};
357+ ReportEventW (h, event_type, 0 , 0 , nullptr , std::size (messages), 0 , messages,
358+ nullptr );
367359}
0 commit comments