Skip to content

Commit

Permalink
Merge pull request #116 from WeTransfer/feature/performance-improvements
Browse files Browse the repository at this point in the history
Catch obj-c exception, remove unneeded dispatch
  • Loading branch information
AvdLee authored Feb 22, 2022
2 parents b6750fe + 69c7355 commit ae80595
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
4 changes: 4 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
27 changes: 17 additions & 10 deletions Sources/Logging/DiagnosticsLogger.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import Foundation
import MetricKit
import ExceptionCatcher

#if os(macOS)
import AppKit
Expand Down Expand Up @@ -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
Expand All @@ -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)")
}
}
}

Expand Down

0 comments on commit ae80595

Please sign in to comment.