From b8b2bfeb638b34e4c205d59e5d9e3d0e249fe84e Mon Sep 17 00:00:00 2001 From: Dan Palmer Date: Tue, 4 Mar 2025 05:19:49 +1100 Subject: [PATCH 1/3] Fix debug logging for large/binary output --- Sources/Command/CommandRunner.swift | 31 +++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/Sources/Command/CommandRunner.swift b/Sources/Command/CommandRunner.swift index 1942969..c892443 100644 --- a/Sources/Command/CommandRunner.swift +++ b/Sources/Command/CommandRunner.swift @@ -122,9 +122,11 @@ public enum CommandError: Error, CustomStringConvertible, Sendable { public struct CommandRunner: CommandRunning, Sendable { let logger: Logger? + let debugLogAsUTF8: Bool - public init(logger: Logger? = nil) { + public init(logger: Logger? = nil, debugLogAsUTF8: Bool = false) { self.logger = logger + self.debugLogAsUTF8 = debugLogAsUTF8 } // swiftlint:disable:next function_body_length @@ -144,7 +146,7 @@ public struct CommandRunner: CommandRunning, Sendable { workingDirectory = try! .init(validating: FileManager.default.currentDirectoryPath) } - let collectedStdErr: ThreadSafe = ThreadSafe("") + let collectedStdErr: ThreadSafe = ThreadSafe(.init()) // Process let process = Process() @@ -155,8 +157,14 @@ public struct CommandRunner: CommandRunning, Sendable { do { for try await data in stdoutPipe.fileHandleForReading.byteStream() { continuation.yield(.standardOutput([UInt8](data))) - if let output = String(data: data, encoding: .utf8) { - logger?.debug("\(output)", metadata: loggerMetadata) + if debugLogAsUTF8 { + logger?.debug({ + if let output = String(data: data, encoding: .utf8) { + return output + } else { + return "\(data)" + } + }, metadata: loggerMetadata) } } } catch { @@ -168,9 +176,15 @@ public struct CommandRunner: CommandRunning, Sendable { do { for try await data in stderrPipe.fileHandleForReading.byteStream() { continuation.yield(.standardError([UInt8](data))) - if let output = String(data: data, encoding: .utf8) { - collectedStdErr.mutate { $0.append(output) } - logger?.error("\(output)", metadata: loggerMetadata) + collectedStdErr.mutate { $0.append(data) } + if debugLogAsUTF8 { + logger?.debug({ + if let output = String(data: data, encoding: .utf8) { + return output + } else { + return "\(data)" + } + }, metadata: loggerMetadata) } } } catch { @@ -217,7 +231,8 @@ public struct CommandRunner: CommandRunning, Sendable { switch process.terminationReason { case .exit: if process.terminationStatus != 0 { - throw CommandError.terminated(process.terminationStatus, stderr: collectedStdErr.value) + let stderr = String(data: collectedStdErr.value, encoding: .utf8) ?? "Failed to decode stderr" + throw CommandError.terminated(process.terminationStatus, stderr: stderr) } case .uncaughtSignal: if process.terminationStatus != 0 { From 86facd507128a61e33d1f5d9b87cd34966087283 Mon Sep 17 00:00:00 2001 From: Dan Palmer Date: Tue, 4 Mar 2025 05:24:43 +1100 Subject: [PATCH 2/3] Fix type --- Sources/Command/CommandRunner.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Sources/Command/CommandRunner.swift b/Sources/Command/CommandRunner.swift index c892443..68fab2c 100644 --- a/Sources/Command/CommandRunner.swift +++ b/Sources/Command/CommandRunner.swift @@ -160,7 +160,7 @@ public struct CommandRunner: CommandRunning, Sendable { if debugLogAsUTF8 { logger?.debug({ if let output = String(data: data, encoding: .utf8) { - return output + return "\(output)" } else { return "\(data)" } @@ -180,7 +180,7 @@ public struct CommandRunner: CommandRunning, Sendable { if debugLogAsUTF8 { logger?.debug({ if let output = String(data: data, encoding: .utf8) { - return output + return "\(output)" } else { return "\(data)" } From 58572696510f0b764f82a4a6a5ef6c93a0c16345 Mon Sep 17 00:00:00 2001 From: Dan Palmer Date: Tue, 4 Mar 2025 05:50:58 +1100 Subject: [PATCH 3/3] Fix autoclosure --- Sources/Command/CommandRunner.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Sources/Command/CommandRunner.swift b/Sources/Command/CommandRunner.swift index 68fab2c..88dbf3a 100644 --- a/Sources/Command/CommandRunner.swift +++ b/Sources/Command/CommandRunner.swift @@ -164,7 +164,7 @@ public struct CommandRunner: CommandRunning, Sendable { } else { return "\(data)" } - }, metadata: loggerMetadata) + }(), metadata: loggerMetadata) } } } catch { @@ -184,7 +184,7 @@ public struct CommandRunner: CommandRunning, Sendable { } else { return "\(data)" } - }, metadata: loggerMetadata) + }(), metadata: loggerMetadata) } } } catch {