Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 11 additions & 19 deletions lldb/source/Host/windows/Host.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down Expand Up @@ -325,31 +328,18 @@ class WindowsEventLog {

static llvm::ManagedStatic<WindowsEventLog> 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<wchar_t, 1> argsUTF16;
if (UTF8ToUTF16(message.str(), argsUTF16))
return;

LPCWSTR msg_ptr = wide_message.c_str();

WORD event_type;
switch (severity) {
case lldb::eSeverityWarning:
Expand All @@ -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);
}
Loading