From ec4dbb667950fa56a299fd85df4de9116d9f53e8 Mon Sep 17 00:00:00 2001 From: naturecodevoid <44983869+naturecodevoid@users.noreply.github.com> Date: Mon, 13 Feb 2023 21:06:59 -0800 Subject: [PATCH] OutputCapturer: fix logging disappearing from Xcode/idevicedebug run --- AltStore/Manager/OutputCapturer.swift | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/AltStore/Manager/OutputCapturer.swift b/AltStore/Manager/OutputCapturer.swift index cf334f1e..8c59e28c 100644 --- a/AltStore/Manager/OutputCapturer.swift +++ b/AltStore/Manager/OutputCapturer.swift @@ -15,26 +15,30 @@ class OutputCapturer { private let consoleManager = LCManager.shared - private var inputPipe = Pipe() - private var errorPipe = Pipe() + private var stdoutPipe = Pipe() + private var stderrPipe = Pipe() + private var outputPipe = Pipe() private init() { // Setup pipe file handlers - self.inputPipe.fileHandleForReading.readabilityHandler = { [weak self] fileHandle in + self.stdoutPipe.fileHandleForReading.readabilityHandler = { [weak self] fileHandle in self?.handle(data: fileHandle.availableData) } - self.errorPipe.fileHandleForReading.readabilityHandler = { [weak self] fileHandle in + self.stderrPipe.fileHandleForReading.readabilityHandler = { [weak self] fileHandle in self?.handle(data: fileHandle.availableData, isError: true) } + + // Keep output in STDOUT without causing infinite loop + dup2(STDOUT_FILENO, self.outputPipe.fileHandleForWriting.fileDescriptor) // Intercept STDOUT and STDERR - dup2(self.inputPipe.fileHandleForWriting.fileDescriptor, STDOUT_FILENO) - dup2(self.errorPipe.fileHandleForWriting.fileDescriptor, STDERR_FILENO) + dup2(self.stdoutPipe.fileHandleForWriting.fileDescriptor, STDOUT_FILENO) + dup2(self.stderrPipe.fileHandleForWriting.fileDescriptor, STDERR_FILENO) } deinit { - try? self.inputPipe.fileHandleForReading.close() - try? self.errorPipe.fileHandleForReading.close() + try? self.stdoutPipe.fileHandleForReading.close() + try? self.stderrPipe.fileHandleForReading.close() } private func handle(data: Data, isError: Bool = false) { @@ -44,6 +48,8 @@ class OutputCapturer { DispatchQueue.main.async { self.consoleManager.print(string) + // put data back into STDOUT so it appears in Xcode/idevicedebug run + self.outputPipe.fileHandleForWriting.write(data) // this might not need to be async } } }