Skip to content

[lldb] Add a log level to Host::SystemLog #90904

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion lldb/include/lldb/Host/Host.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,15 @@ class Host {
StartMonitoringChildProcess(const MonitorChildProcessCallback &callback,
lldb::pid_t pid);

/// System log level.
enum SystemLogLevel {
eSystemLogInfo,
eSystemLogWarning,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is there a good reason to keep this separate from DiagnosticSeverity?
https://lldb.llvm.org/cpp_reference/namespacelldb__private.html#a3b3fff978827f40d27f288b4f1f29369
Maybe we could factor this into private enums?

Copy link
Member Author

Choose a reason for hiding this comment

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

Good point! #90917

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.
///
Expand Down
30 changes: 26 additions & 4 deletions lldb/source/Host/common/Host.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -629,5 +651,5 @@ char SystemLogHandler::ID;
SystemLogHandler::SystemLogHandler() {}

void SystemLogHandler::Emit(llvm::StringRef message) {
Host::SystemLog(message);
Host::SystemLog(Host::eSystemLogInfo, message);
}
12 changes: 10 additions & 2 deletions lldb/source/Host/macosx/objcxx/Host.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down