|
22 | 22 | #include "lldb/Utility/StreamString.h"
|
23 | 23 | #include "lldb/Utility/StructuredData.h"
|
24 | 24 |
|
| 25 | +#include "llvm/ADT/StringRef.h" |
25 | 26 | #include "llvm/Support/ConvertUTF.h"
|
| 27 | +#include "llvm/Support/ManagedStatic.h" |
26 | 28 |
|
27 | 29 | // Windows includes
|
28 | 30 | #include <tlhelp32.h>
|
@@ -302,3 +304,64 @@ Environment Host::GetEnvironment() {
|
302 | 304 | }
|
303 | 305 | return env;
|
304 | 306 | }
|
| 307 | + |
| 308 | +/// Manages the lifecycle of a Windows Event's Source. |
| 309 | +/// The destructor will call DeregisterEventSource. |
| 310 | +/// This class is meant to be used with \ref llvm::ManagedStatic. |
| 311 | +class WindowsEventLog { |
| 312 | +public: |
| 313 | + WindowsEventLog() : handle(RegisterEventSource(nullptr, L"lldb")) {} |
| 314 | + |
| 315 | + ~WindowsEventLog() { |
| 316 | + if (handle) |
| 317 | + DeregisterEventSource(handle); |
| 318 | + } |
| 319 | + |
| 320 | + HANDLE GetHandle() const { return handle; } |
| 321 | + |
| 322 | +private: |
| 323 | + HANDLE handle; |
| 324 | +}; |
| 325 | + |
| 326 | +static llvm::ManagedStatic<WindowsEventLog> event_log; |
| 327 | + |
| 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 | + |
| 342 | +void Host::SystemLog(Severity severity, llvm::StringRef message) { |
| 343 | + HANDLE h = event_log->GetHandle(); |
| 344 | + if (!h) |
| 345 | + return; |
| 346 | + |
| 347 | + std::wstring wide_message = AnsiToUtf16(message.str()); |
| 348 | + if (wide_message.empty()) |
| 349 | + return; |
| 350 | + |
| 351 | + LPCWSTR msg_ptr = wide_message.c_str(); |
| 352 | + |
| 353 | + WORD event_type; |
| 354 | + switch (severity) { |
| 355 | + case lldb::eSeverityWarning: |
| 356 | + event_type = EVENTLOG_WARNING_TYPE; |
| 357 | + break; |
| 358 | + case lldb::eSeverityError: |
| 359 | + event_type = EVENTLOG_ERROR_TYPE; |
| 360 | + break; |
| 361 | + case lldb::eSeverityInfo: |
| 362 | + default: |
| 363 | + event_type = EVENTLOG_INFORMATION_TYPE; |
| 364 | + } |
| 365 | + |
| 366 | + ReportEventW(h, event_type, 0, 0, nullptr, 1, 0, &msg_ptr, nullptr); |
| 367 | +} |
0 commit comments