Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将进程stdout重定向到Swift中的Apple系统日志工具_Swift_Macos_Logging - Fatal编程技术网

将进程stdout重定向到Swift中的Apple系统日志工具

将进程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() 但我不知道如何引用苹果系统日志管道。有线索吗

我正在为macOS构建一个Swift应用程序,启动一个子流程。这个子流程将有用的信息记录到stdout中,我可以在Xcode控制台中看到它

我现在想要实现的是将子进程
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日志系统