[Lldb-commits] [lldb] [lldb] Log to system log instead of stderr from Host::SystemLog (PR #83366)

Jonas Devlieghere via lldb-commits lldb-commits at lists.llvm.org
Wed Feb 28 21:03:50 PST 2024


https://github.com/JDevlieghere updated https://github.com/llvm/llvm-project/pull/83366

>From c6599a05b5be0cbd279a96e98bed1c89d9de707e Mon Sep 17 00:00:00 2001
From: Jonas Devlieghere <jonas at devlieghere.com>
Date: Wed, 28 Feb 2024 18:12:45 -0800
Subject: [PATCH 1/2] [lldb] Log to system log instead of stderr from
 Host::SystemLog

Currently, calls to Host::SystemLog print to stderr on all host
platforms except Darwin. This severely limits its value on the command
line, where we don't want to overload the user with log messages. Switch
to using the syslog function on POSIX systems to send messages to the
system logger instead of stdout.

On Darwin systems this sends the log message to os_log, which matches
what we do today. Nevertheless I kept the current implementation that
uses os_log directly as it gives us more freedom.

I'm not sure if there's an equivalent on Windows, so I kept the existing
behavior of logging to stderr.
---
 lldb/source/Host/common/Host.cpp | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/lldb/source/Host/common/Host.cpp b/lldb/source/Host/common/Host.cpp
index f4cec97f5af633..1af01ef5025bc4 100644
--- a/lldb/source/Host/common/Host.cpp
+++ b/lldb/source/Host/common/Host.cpp
@@ -89,8 +89,17 @@ using namespace lldb;
 using namespace lldb_private;
 
 #if !defined(__APPLE__)
+#if !defined(_WIN32)
+#include <syslog.h>
+void Host::SystemLog(llvm::StringRef message) {
+  openlog("lldb", LOG_CONS | LOG_PID | LOG_NDELAY, LOG_USER);
+  syslog(LOG_INFO, "%s", message.data());
+  closelog();
+}
+#else
 void Host::SystemLog(llvm::StringRef message) { llvm::errs() << message; }
 #endif
+#endif
 
 #if !defined(__APPLE__) && !defined(_WIN32)
 static thread_result_t

>From 6656cf1248b8d04c2c7e7fe3c90b51479242b8ce Mon Sep 17 00:00:00 2001
From: Jonas Devlieghere <jonas at devlieghere.com>
Date: Wed, 28 Feb 2024 21:03:28 -0800
Subject: [PATCH 2/2] [lldb] Call openlog only once and don't call closelog

---
 lldb/source/Host/common/Host.cpp | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/lldb/source/Host/common/Host.cpp b/lldb/source/Host/common/Host.cpp
index 1af01ef5025bc4..565138ba17031f 100644
--- a/lldb/source/Host/common/Host.cpp
+++ b/lldb/source/Host/common/Host.cpp
@@ -92,9 +92,11 @@ using namespace lldb_private;
 #if !defined(_WIN32)
 #include <syslog.h>
 void Host::SystemLog(llvm::StringRef message) {
-  openlog("lldb", LOG_CONS | LOG_PID | LOG_NDELAY, LOG_USER);
+  static llvm::once_flag g_openlog_once;
+  llvm::call_once(g_openlog_once, [] {
+    openlog("lldb", LOG_CONS | LOG_PID | LOG_NDELAY, LOG_USER);
+  });
   syslog(LOG_INFO, "%s", message.data());
-  closelog();
 }
 #else
 void Host::SystemLog(llvm::StringRef message) { llvm::errs() << message; }



More information about the lldb-commits mailing list