Skip to content

Commit f0c8198

Browse files
[lldb][windows] use OutputDebugStringA instead of c to log events (#156474)
In #150213 we made use of the Event Viewer on Windows (equivalent of system logging on Darwin) rather than piping to the standard output. This turned out to be too verbose in practice, as the Event Viewer is developer oriented and not user oriented. This patch swaps the use of `ReportEventW` for `OutputDebugStringA`, allowing to use tools such as `DebugView` to record logs when we are interested in receiving them, rather than continuously writing to the buffer. Please see an example below: <img width="1253" height="215" alt="Screenshot 2025-09-02 at 16 07 03" src="https://github.com/user-attachments/assets/4a326e46-d8a4-4c99-8c96-1bee62da8d55" />
1 parent f99b0f3 commit f0c8198

File tree

1 file changed

+10
-36
lines changed

1 file changed

+10
-36
lines changed

lldb/source/Host/windows/Host.cpp

Lines changed: 10 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,8 @@
2222
#include "lldb/Utility/StreamString.h"
2323
#include "lldb/Utility/StructuredData.h"
2424

25-
#include "llvm/ADT/SmallVector.h"
2625
#include "llvm/ADT/StringRef.h"
2726
#include "llvm/Support/ConvertUTF.h"
28-
#include "llvm/Support/ManagedStatic.h"
2927

3028
// Windows includes
3129
#include <tlhelp32.h>
@@ -308,52 +306,28 @@ Environment Host::GetEnvironment() {
308306
return env;
309307
}
310308

311-
/// Manages the lifecycle of a Windows Event's Source.
312-
/// The destructor will call DeregisterEventSource.
313-
/// This class is meant to be used with \ref llvm::ManagedStatic.
314-
class WindowsEventLog {
315-
public:
316-
WindowsEventLog() : handle(RegisterEventSource(nullptr, L"lldb")) {}
317-
318-
~WindowsEventLog() {
319-
if (handle)
320-
DeregisterEventSource(handle);
321-
}
322-
323-
HANDLE GetHandle() const { return handle; }
324-
325-
private:
326-
HANDLE handle;
327-
};
328-
329-
static llvm::ManagedStatic<WindowsEventLog> event_log;
330-
331309
void Host::SystemLog(Severity severity, llvm::StringRef message) {
332310
if (message.empty())
333311
return;
334312

335-
HANDLE h = event_log->GetHandle();
336-
if (!h)
337-
return;
338-
339-
llvm::SmallVector<wchar_t, 1> argsUTF16;
340-
if (UTF8ToUTF16(message.str(), argsUTF16))
341-
return;
313+
std::string log_msg;
314+
llvm::raw_string_ostream stream(log_msg);
342315

343-
WORD event_type;
344316
switch (severity) {
345317
case lldb::eSeverityWarning:
346-
event_type = EVENTLOG_WARNING_TYPE;
318+
stream << "[Warning] ";
347319
break;
348320
case lldb::eSeverityError:
349-
event_type = EVENTLOG_ERROR_TYPE;
321+
stream << "[Error] ";
350322
break;
351323
case lldb::eSeverityInfo:
352324
default:
353-
event_type = EVENTLOG_INFORMATION_TYPE;
325+
stream << "[Info] ";
326+
break;
354327
}
355328

356-
LPCWSTR messages[1] = {argsUTF16.data()};
357-
ReportEventW(h, event_type, 0, 0, nullptr, std::size(messages), 0, messages,
358-
nullptr);
329+
stream << message;
330+
stream.flush();
331+
332+
OutputDebugStringA(log_msg.c_str());
359333
}

0 commit comments

Comments
 (0)