将进程stdout重定向到Swift中的Apple系统日志工具
我正在为macOS构建一个Swift应用程序,启动一个子流程。这个子流程将有用的信息记录到stdout中,我可以在Xcode控制台中看到它 我现在想要实现的是将子进程将进程stdout重定向到Swift中的Apple系统日志工具,swift,macos,logging,Swift,Macos,Logging,我正在为macOS构建一个Swift应用程序,启动一个子流程。这个子流程将有用的信息记录到stdout中,我可以在Xcode控制台中看到它 我现在想要实现的是将子进程stdout重定向到Apple日志工具,以便我们可以在部署应用程序时访问数据 基本上,这是: let task=Process() task.launchPath=“子流程” task.standardOutput=//AppleSystemLogPipe task.launch() 但我不知道如何引用苹果系统日志管道。有线索吗
stdout
重定向到Apple日志工具,以便我们可以在部署应用程序时访问数据
基本上,这是:
let task=Process()
task.launchPath=“子流程”
task.standardOutput=//AppleSystemLogPipe
task.launch()
但我不知道如何引用苹果系统日志管道。有线索吗
致以最良好的祝愿 我终于做到了。基本上,我们定义一个
管道
来捕获流程输出,并在处理程序中调用OS函数OS_log
,读取数据:
let pipe = Pipe()
let outHandle = pipe.fileHandleForReading
outHandle.readabilityHandler = { pipe in
if let line = String(data: pipe.availableData, encoding: .utf8) {
// Define the placeholder as public, otherwise the Console obfuscate it
os_log("%{public}@", line)
}
}
let task = Process()
task.launchPath = "subprocess"
task.standardOutput = pipe // Redirect stdout to our pipe
task.launch()
在开发中,它显示在Xcode控制台中;在部署时,它被重定向到Apple日志系统