-
Notifications
You must be signed in to change notification settings - Fork 352
[windows][lldb] implement system logging on Windows #11017
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
17bafe3
598da83
5be89a1
f9b20a1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,10 +22,13 @@ | |
| #include "lldb/Utility/StreamString.h" | ||
| #include "lldb/Utility/StructuredData.h" | ||
|
|
||
| #include "llvm/ADT/StringRef.h" | ||
| #include "llvm/Support/ConvertUTF.h" | ||
| #include "llvm/Support/ManagedStatic.h" | ||
|
|
||
| // Windows includes | ||
| #include <tlhelp32.h> | ||
| #include <windows.h> | ||
|
|
||
| using namespace lldb; | ||
| using namespace lldb_private; | ||
|
|
@@ -302,3 +305,68 @@ Environment Host::GetEnvironment() { | |
| } | ||
| return env; | ||
| } | ||
|
|
||
| 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; | ||
|
|
||
| static LPCWSTR AnsiToUtf16(const char *ansi) { | ||
| if (!ansi) | ||
| return nullptr; | ||
| const int length = strlen(ansi); | ||
| const int unicode_length = | ||
| MultiByteToWideChar(CP_ACP, 0, ansi, length, nullptr, 0); | ||
|
||
| WCHAR *unicode = new WCHAR[unicode_length + 1]; | ||
| MultiByteToWideChar(CP_ACP, 0, ansi, length, unicode, unicode_length); | ||
| unicode[unicode_length] = 0; | ||
| return unicode; | ||
| } | ||
|
|
||
| void Host::SystemLog(Severity severity, llvm::StringRef message) { | ||
| HANDLE h = event_log->GetHandle(); | ||
| LPCWSTR wide_message = AnsiToUtf16(message.str().c_str()); | ||
|
|
||
| if (!h || !wide_message) { | ||
| switch (severity) { | ||
|
||
| case lldb::eSeverityInfo: | ||
| case lldb::eSeverityWarning: | ||
| llvm::outs() << message; | ||
| return; | ||
| case lldb::eSeverityError: | ||
| llvm::errs() << message; | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| WORD event_type; | ||
| WORD event_id; | ||
|
||
|
|
||
| switch (severity) { | ||
| case lldb::eSeverityWarning: | ||
| event_type = EVENTLOG_WARNING_TYPE; | ||
| break; | ||
| case lldb::eSeverityError: | ||
| event_type = EVENTLOG_ERROR_TYPE; | ||
| break; | ||
| case lldb::eSeverityInfo: | ||
| default: | ||
| event_type = EVENTLOG_INFORMATION_TYPE; | ||
| } | ||
|
|
||
| ReportEventW(h, event_type, 0, 0, nullptr, 1, 0, &wide_message, nullptr); | ||
|
|
||
| delete[] wide_message; | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed, thanks 👍