From 69c7355a2f9c4c49cf0a7ecd069c29ce2d3b1b71 Mon Sep 17 00:00:00 2001 From: Antoine van der Lee Date: Sun, 20 Feb 2022 10:48:15 +0100 Subject: [PATCH] Catch obj-c exception, remove unneeded dispatch --- Package.swift | 4 ++++ Sources/Logging/DiagnosticsLogger.swift | 27 ++++++++++++++++--------- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/Package.swift b/Package.swift index bb6b976..f91f1ba 100644 --- a/Package.swift +++ b/Package.swift @@ -12,9 +12,13 @@ let package = Package(name: "Diagnostics", products: [ .library(name: "Diagnostics", type: .static, targets: ["Diagnostics"]) ], + dependencies: [ + .package(url: "https://github.com/sindresorhus/ExceptionCatcher", from: "2.0.0") + ], targets: [ .target( name: "Diagnostics", + dependencies: ["ExceptionCatcher"], path: "Sources", resources: [ .process("style.css"), diff --git a/Sources/Logging/DiagnosticsLogger.swift b/Sources/Logging/DiagnosticsLogger.swift index 8cc64f6..ee9354f 100644 --- a/Sources/Logging/DiagnosticsLogger.swift +++ b/Sources/Logging/DiagnosticsLogger.swift @@ -8,6 +8,7 @@ import Foundation import MetricKit +import ExceptionCatcher #if os(macOS) import AppKit @@ -210,9 +211,7 @@ private extension DiagnosticsLogger { inputPipe.fileHandleForReading.readabilityHandler = { [weak self] handle in let data = handle.availableData - self?.queue.async { - self?.handleLoggedData(data) - } + self?.handleLoggedData(data) } // Copy the STDOUT file descriptor into our output pipe's file descriptor @@ -225,15 +224,23 @@ private extension DiagnosticsLogger { } private func handleLoggedData(_ data: Data) { - outputPipe.fileHandleForWriting.write(data) + do { + try ExceptionCatcher.catch { () -> Void in + autoreleasepool { + outputPipe.fileHandleForWriting.write(data) - guard let string = String(data: data, encoding: .utf8) else { - return assertionFailure("Invalid data is logged") - } + guard let string = String(data: data, encoding: .utf8) else { + return assertionFailure("Invalid data is logged") + } - string.enumerateLines(invoking: { [weak self] (line, _) in - self?.log(SystemLog(line: line)) - }) + string.enumerateLines(invoking: { [weak self] (line, _) in + self?.log(SystemLog(line: line)) + }) + } + } + } catch { + print("Exception was catched \(error)") + } } }