[Lldb-commits] [lldb] [lldb] Add a log level to Host::SystemLog (PR #90904)

Jonas Devlieghere via lldb-commits lldb-commits at lists.llvm.org
Thu May 2 14:34:36 PDT 2024


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

>From 1109b65209d49046e029b780bf484a810c1bd42e Mon Sep 17 00:00:00 2001
From: Jonas Devlieghere <jonas at devlieghere.com>
Date: Thu, 2 May 2024 13:20:16 -0700
Subject: [PATCH] [lldb] Add a log level to Host::SystemLog

Add the ability to specify a log level to Host::SystemLog.
---
 lldb/include/lldb/Host/Host.h          |  9 +++++++-
 lldb/source/Host/common/Host.cpp       | 30 ++++++++++++++++++++++----
 lldb/source/Host/macosx/objcxx/Host.mm | 12 +++++++++--
 3 files changed, 44 insertions(+), 7 deletions(-)

diff --git a/lldb/include/lldb/Host/Host.h b/lldb/include/lldb/Host/Host.h
index 30549cd7891497..b0cb477d82fa14 100644
--- a/lldb/include/lldb/Host/Host.h
+++ b/lldb/include/lldb/Host/Host.h
@@ -87,8 +87,15 @@ class Host {
   StartMonitoringChildProcess(const MonitorChildProcessCallback &callback,
                               lldb::pid_t pid);
 
+  /// System log level.
+  enum SystemLogLevel {
+    eSystemLogInfo,
+    eSystemLogWarning,
+    eSystemLogError,
+  };
+
   /// Emit the given message to the operating system log.
-  static void SystemLog(llvm::StringRef message);
+  static void SystemLog(SystemLogLevel log_level, llvm::StringRef message);
 
   /// Get the process ID for the calling process.
   ///
diff --git a/lldb/source/Host/common/Host.cpp b/lldb/source/Host/common/Host.cpp
index 565138ba17031f..45b2a62b5c72a7 100644
--- a/lldb/source/Host/common/Host.cpp
+++ b/lldb/source/Host/common/Host.cpp
@@ -91,15 +91,37 @@ using namespace lldb_private;
 #if !defined(__APPLE__)
 #if !defined(_WIN32)
 #include <syslog.h>
-void Host::SystemLog(llvm::StringRef message) {
+void Host::SystemLog(SystemLogLevel log_level, llvm::StringRef message) {
   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());
+  int level = LOG_DEBUG;
+  switch (log_level) {
+  case eSystemLogInfo:
+    level = LOG_INFO;
+    break;
+  case eSystemLogWarning:
+    level = LOG_WARNING;
+    break;
+  case eSystemLogError:
+    level = LOG_ERR;
+    break;
+  }
+  syslog(level, "%s", message.data());
 }
 #else
-void Host::SystemLog(llvm::StringRef message) { llvm::errs() << message; }
+void Host::SystemLog(SystemLogLevel log_level, llvm::StringRef message) {
+  switch (log_level) {
+  case eSystemLogInfo:
+  case eSystemLogWarning:
+    llvm::outs() << message;
+    break;
+  case eSystemLogError:
+    llvm::errs() << message;
+    break;
+  }
+}
 #endif
 #endif
 
@@ -629,5 +651,5 @@ char SystemLogHandler::ID;
 SystemLogHandler::SystemLogHandler() {}
 
 void SystemLogHandler::Emit(llvm::StringRef message) {
-  Host::SystemLog(message);
+  Host::SystemLog(Host::eSystemLogInfo, message);
 }
diff --git a/lldb/source/Host/macosx/objcxx/Host.mm b/lldb/source/Host/macosx/objcxx/Host.mm
index 070a49208639a2..1f67141de55d70 100644
--- a/lldb/source/Host/macosx/objcxx/Host.mm
+++ b/lldb/source/Host/macosx/objcxx/Host.mm
@@ -102,12 +102,20 @@
 static os_log_t g_os_log;
 static std::once_flag g_os_log_once;
 
-void Host::SystemLog(llvm::StringRef message) {
+void Host::SystemLog(SystemLogLevel log_level, llvm::StringRef message) {
   if (__builtin_available(macos 10.12, iOS 10, tvOS 10, watchOS 3, *)) {
     std::call_once(g_os_log_once, []() {
       g_os_log = os_log_create("com.apple.dt.lldb", "lldb");
     });
-    os_log(g_os_log, "%{public}s", message.str().c_str());
+    switch (log_level) {
+    case eSystemLogInfo:
+    case eSystemLogWarning:
+      os_log(g_os_log, "%{public}s", message.str().c_str());
+      break;
+    case eSystemLogError:
+      os_log_error(g_os_log, "%{public}s", message.str().c_str());
+      break;
+    }
   } else {
     llvm::errs() << message;
   }



More information about the lldb-commits mailing list