Skip to content

Commit a7e9e3e

Browse files
authored
[lldb] Add a log level to Host::SystemLog (#90904)
Add the ability to specify a log level to Host::SystemLog.
1 parent 2d15855 commit a7e9e3e

File tree

3 files changed

+44
-7
lines changed

3 files changed

+44
-7
lines changed

lldb/include/lldb/Host/Host.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,15 @@ class Host {
8787
StartMonitoringChildProcess(const MonitorChildProcessCallback &callback,
8888
lldb::pid_t pid);
8989

90+
/// System log level.
91+
enum SystemLogLevel {
92+
eSystemLogInfo,
93+
eSystemLogWarning,
94+
eSystemLogError,
95+
};
96+
9097
/// Emit the given message to the operating system log.
91-
static void SystemLog(llvm::StringRef message);
98+
static void SystemLog(SystemLogLevel log_level, llvm::StringRef message);
9299

93100
/// Get the process ID for the calling process.
94101
///

lldb/source/Host/common/Host.cpp

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,15 +91,37 @@ using namespace lldb_private;
9191
#if !defined(__APPLE__)
9292
#if !defined(_WIN32)
9393
#include <syslog.h>
94-
void Host::SystemLog(llvm::StringRef message) {
94+
void Host::SystemLog(SystemLogLevel log_level, llvm::StringRef message) {
9595
static llvm::once_flag g_openlog_once;
9696
llvm::call_once(g_openlog_once, [] {
9797
openlog("lldb", LOG_CONS | LOG_PID | LOG_NDELAY, LOG_USER);
9898
});
99-
syslog(LOG_INFO, "%s", message.data());
99+
int level = LOG_DEBUG;
100+
switch (log_level) {
101+
case eSystemLogInfo:
102+
level = LOG_INFO;
103+
break;
104+
case eSystemLogWarning:
105+
level = LOG_WARNING;
106+
break;
107+
case eSystemLogError:
108+
level = LOG_ERR;
109+
break;
110+
}
111+
syslog(level, "%s", message.data());
100112
}
101113
#else
102-
void Host::SystemLog(llvm::StringRef message) { llvm::errs() << message; }
114+
void Host::SystemLog(SystemLogLevel log_level, llvm::StringRef message) {
115+
switch (log_level) {
116+
case eSystemLogInfo:
117+
case eSystemLogWarning:
118+
llvm::outs() << message;
119+
break;
120+
case eSystemLogError:
121+
llvm::errs() << message;
122+
break;
123+
}
124+
}
103125
#endif
104126
#endif
105127

@@ -629,5 +651,5 @@ char SystemLogHandler::ID;
629651
SystemLogHandler::SystemLogHandler() {}
630652

631653
void SystemLogHandler::Emit(llvm::StringRef message) {
632-
Host::SystemLog(message);
654+
Host::SystemLog(Host::eSystemLogInfo, message);
633655
}

lldb/source/Host/macosx/objcxx/Host.mm

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,20 @@
102102
static os_log_t g_os_log;
103103
static std::once_flag g_os_log_once;
104104

105-
void Host::SystemLog(llvm::StringRef message) {
105+
void Host::SystemLog(SystemLogLevel log_level, llvm::StringRef message) {
106106
if (__builtin_available(macos 10.12, iOS 10, tvOS 10, watchOS 3, *)) {
107107
std::call_once(g_os_log_once, []() {
108108
g_os_log = os_log_create("com.apple.dt.lldb", "lldb");
109109
});
110-
os_log(g_os_log, "%{public}s", message.str().c_str());
110+
switch (log_level) {
111+
case eSystemLogInfo:
112+
case eSystemLogWarning:
113+
os_log(g_os_log, "%{public}s", message.str().c_str());
114+
break;
115+
case eSystemLogError:
116+
os_log_error(g_os_log, "%{public}s", message.str().c_str());
117+
break;
118+
}
111119
} else {
112120
llvm::errs() << message;
113121
}

0 commit comments

Comments
 (0)