I'm building a Swift app for macOS that launch a sub-process. This subprocess logs useful information to stdout, and I see it in the Xcode console.
What I would want to achieve now is to redirect the sub-process stdout to the Apple Log Facility so that we can access the data when the app is deployed.
Basically, this:
let task = Process()
task.launchPath = "subprocess"
task.standardOutput = // AppleSystemLogPipe
task.launch()
But I don't know how to refer to the Apple System Log Pipe. Any clues for this?
Best regards!
I finally managed to do this. Basically, we define a Pipe to catch the process output and, in an handler, call the OS function os_log with the data read:
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()
In development, it is displayed in the Xcode console ; and when deployed it is redirected to the Apple Log System.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With