Skip to content

Conversation

charles-zablit
Copy link
Contributor

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:
Screenshot 2025-09-02 at 16 07 03

Comment on lines 327 to 328
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

Copy link
Collaborator

@adrian-prantl adrian-prantl left a comment

Choose a reason for hiding this comment

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

Otherwise lgtm

@charles-zablit charles-zablit merged commit f0c8198 into llvm:main Sep 2, 2025
9 checks passed
charles-zablit added a commit to charles-zablit/llvm-project that referenced this pull request Sep 2, 2025
…vm#156474)

In llvm#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"
/>
@llvmbot
Copy link
Member

llvmbot commented Sep 3, 2025

@llvm/pr-subscribers-lldb

Author: Charles Zablit (charles-zablit)

Changes

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" />


Full diff: https://github.com/llvm/llvm-project/pull/156474.diff

1 Files Affected:

  • (modified) lldb/source/Host/windows/Host.cpp (+10-36)
diff --git a/lldb/source/Host/windows/Host.cpp b/lldb/source/Host/windows/Host.cpp
index 4277b8edb38e5..e8973a3fb937a 100644
--- a/lldb/source/Host/windows/Host.cpp
+++ b/lldb/source/Host/windows/Host.cpp
@@ -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>
@@ -308,52 +306,28 @@ 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;
+  std::string log_msg;
+  llvm::raw_string_ostream stream(log_msg);
 
-  WORD event_type;
   switch (severity) {
   case lldb::eSeverityWarning:
-    event_type = EVENTLOG_WARNING_TYPE;
+    stream << "[Warning] ";
     break;
   case lldb::eSeverityError:
-    event_type = EVENTLOG_ERROR_TYPE;
+    stream << "[Error] ";
     break;
   case lldb::eSeverityInfo:
   default:
-    event_type = EVENTLOG_INFORMATION_TYPE;
+    stream << "[Info] ";
+    break;
   }
 
-  LPCWSTR messages[1] = {argsUTF16.data()};
-  ReportEventW(h, event_type, 0, 0, nullptr, std::size(messages), 0, messages,
-               nullptr);
+  stream << message;
+  stream.flush();
+
+  OutputDebugStringA(log_msg.c_str());
 }

@llvmbot
Copy link
Member

llvmbot commented Sep 3, 2025

@llvm/pr-subscribers-platform-windows

Author: Charles Zablit (charles-zablit)

Changes

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" />


Full diff: https://github.com/llvm/llvm-project/pull/156474.diff

1 Files Affected:

  • (modified) lldb/source/Host/windows/Host.cpp (+10-36)
diff --git a/lldb/source/Host/windows/Host.cpp b/lldb/source/Host/windows/Host.cpp
index 4277b8edb38e5..e8973a3fb937a 100644
--- a/lldb/source/Host/windows/Host.cpp
+++ b/lldb/source/Host/windows/Host.cpp
@@ -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>
@@ -308,52 +306,28 @@ 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;
+  std::string log_msg;
+  llvm::raw_string_ostream stream(log_msg);
 
-  WORD event_type;
   switch (severity) {
   case lldb::eSeverityWarning:
-    event_type = EVENTLOG_WARNING_TYPE;
+    stream << "[Warning] ";
     break;
   case lldb::eSeverityError:
-    event_type = EVENTLOG_ERROR_TYPE;
+    stream << "[Error] ";
     break;
   case lldb::eSeverityInfo:
   default:
-    event_type = EVENTLOG_INFORMATION_TYPE;
+    stream << "[Info] ";
+    break;
   }
 
-  LPCWSTR messages[1] = {argsUTF16.data()};
-  ReportEventW(h, event_type, 0, 0, nullptr, std::size(messages), 0, messages,
-               nullptr);
+  stream << message;
+  stream.flush();
+
+  OutputDebugStringA(log_msg.c_str());
 }

charles-zablit added a commit to charles-zablit/llvm-project that referenced this pull request Sep 4, 2025
…vm#156474)

In llvm#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"
/>
charles-zablit added a commit to charles-zablit/llvm-project that referenced this pull request Sep 15, 2025
…vm#156474)

In llvm#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"
/>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants