Skip to content

Commit 1e5d526

Browse files
committed
[lldb] Add SystemLogHandler for emitting log messages to the system log
Add a system log handler that emits log messages to the operating system log. In addition to the log handler itself, this patch also introduces a new Host::SystemLog helper function to abstract over writing to the system log. Differential revision: https://reviews.llvm.org/D128321
1 parent 4821508 commit 1e5d526

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

lldb/include/lldb/Host/Host.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "lldb/Host/HostThread.h"
1414
#include "lldb/Utility/Environment.h"
1515
#include "lldb/Utility/FileSpec.h"
16+
#include "lldb/Utility/Log.h"
1617
#include "lldb/Utility/Timeout.h"
1718
#include "lldb/lldb-private-forward.h"
1819
#include "lldb/lldb-private.h"
@@ -86,6 +87,9 @@ class Host {
8687
StartMonitoringChildProcess(const MonitorChildProcessCallback &callback,
8788
lldb::pid_t pid);
8889

90+
/// Emit the given message to the operating system log.
91+
static void SystemLog(llvm::StringRef message);
92+
8993
/// Get the process ID for the calling process.
9094
///
9195
/// \return
@@ -252,6 +256,13 @@ class Host {
252256
ProcessInstanceInfoList &proc_infos);
253257
};
254258

259+
/// Log handler that emits log messages to the operating system log.
260+
class SystemLogHandler : public LogHandler {
261+
public:
262+
SystemLogHandler();
263+
void Emit(llvm::StringRef message) override;
264+
};
265+
255266
} // namespace lldb_private
256267

257268
namespace llvm {

lldb/source/Host/common/Host.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,10 @@ llvm::Expected<HostThread> Host::StartMonitoringChildProcess(
106106
});
107107
}
108108

109+
#if !defined(__APPLE__)
110+
void Host::SystemLog(llvm::StringRef message) { llvm::errs() << message; }
111+
#endif
112+
109113
#ifndef __linux__
110114
// Scoped class that will disable thread canceling when it is constructed, and
111115
// exception safely restore the previous value it when it goes out of scope.
@@ -627,3 +631,9 @@ uint32_t Host::FindProcesses(const ProcessInstanceInfoMatch &match_info,
627631

628632
return result;
629633
}
634+
635+
SystemLogHandler::SystemLogHandler() {}
636+
637+
void SystemLogHandler::Emit(llvm::StringRef message) {
638+
Host::SystemLog(message);
639+
}

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
#include "../cfcpp/CFCString.h"
8383

8484
#include <objc/objc-auto.h>
85+
#include <os/log.h>
8586

8687
#include <CoreFoundation/CoreFoundation.h>
8788
#include <Foundation/Foundation.h>
@@ -98,6 +99,20 @@
9899
using namespace lldb;
99100
using namespace lldb_private;
100101

102+
static os_log_t g_os_log;
103+
static std::once_flag g_os_log_once;
104+
105+
void Host::SystemLog(llvm::StringRef message) {
106+
if (__builtin_available(macos 10.12, iOS 10, tvOS 10, watchOS 3, *)) {
107+
std::call_once(g_os_log_once, []() {
108+
g_os_log = os_log_create("com.apple.dt.lldb", "lldb");
109+
});
110+
os_log(g_os_log, "%{public}s", message.str().c_str());
111+
} else {
112+
llvm::errs() << message;
113+
}
114+
}
115+
101116
bool Host::GetBundleDirectory(const FileSpec &file,
102117
FileSpec &bundle_directory) {
103118
#if defined(__APPLE__)

0 commit comments

Comments
 (0)