Skip to content
Merged
Changes from 1 commit
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
44 changes: 7 additions & 37 deletions lldb/source/Host/windows/Host.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,8 @@
#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"

// Windows includes
#include <tlhelp32.h>
Expand Down Expand Up @@ -308,52 +306,24 @@ Environment Host::GetEnvironment() {
return env;
}

/// Manages the lifecycle of a Windows Event's Source.
/// The destructor will call DeregisterEventSource.
/// This class is meant to be used with \ref llvm::ManagedStatic.
class WindowsEventLog {
public:
WindowsEventLog() : handle(RegisterEventSource(nullptr, L"lldb")) {}

~WindowsEventLog() {
if (handle)
DeregisterEventSource(handle);
}

HANDLE GetHandle() const { return handle; }

private:
HANDLE handle;
};

static llvm::ManagedStatic<WindowsEventLog> event_log;

void Host::SystemLog(Severity severity, llvm::StringRef message) {
if (message.empty())
return;

HANDLE h = event_log->GetHandle();
if (!h)
return;

llvm::SmallVector<wchar_t, 1> argsUTF16;
if (UTF8ToUTF16(message.str(), argsUTF16))
return;

WORD event_type;
std::string log_prefix;
switch (severity) {
case lldb::eSeverityWarning:
event_type = EVENTLOG_WARNING_TYPE;
log_prefix = "[Warning] ";
break;
case lldb::eSeverityError:
event_type = EVENTLOG_ERROR_TYPE;
log_prefix = "[Error] ";
break;
case lldb::eSeverityInfo:
default:
event_type = EVENTLOG_INFORMATION_TYPE;
log_prefix = "[Info] ";
break;
}

LPCWSTR messages[1] = {argsUTF16.data()};
ReportEventW(h, event_type, 0, 0, nullptr, std::size(messages), 0, messages,
nullptr);
std::string final_message = log_prefix + message.str();
OutputDebugStringA(final_message.c_str());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm half tempted to say that this should be (Twine{log_prefix} + message).str().c_str() instead of the std::string and then calling OutputDebugStringA.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

llvm::raw_string_ostream would maybe look nicer.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Went with the llvm::raw_string_ostream approach

}
Loading