Skip to content

Commit

Permalink
Use latest write APIs to prevent a crash when storage is full. (#87)
Browse files Browse the repository at this point in the history
  • Loading branch information
AvdLee authored Jul 22, 2021
1 parent 3e640df commit 3c72dc5
Showing 1 changed file with 23 additions and 7 deletions.
30 changes: 23 additions & 7 deletions Sources/DiagnosticsLogger.swift
Original file line number Diff line number Diff line change
Expand Up @@ -203,16 +203,32 @@ extension DiagnosticsLogger {
let coordinator = NSFileCoordinator(filePresenter: nil)
var error: NSError?
coordinator.coordinate(writingItemAt: logFileLocation, error: &error) { [weak self] url in
guard let fileHandle = try? FileHandle(forWritingTo: url) else {
return
do {
let fileHandle = try FileHandle(forWritingTo: url)
if #available(OSX 10.15.4, iOS 13.4, watchOS 6.0, tvOS 13.4, *) {
defer {
try? fileHandle.close()
}
try fileHandle.seekToEnd()
try fileHandle.write(contentsOf: data)
} else {
legacyAppend(data, to: fileHandle)
}

self?.logSize += Int64(data.count)
self?.trimLinesIfNecessary()
} catch {
assertionFailure("Writing data failed with error: \(error)")
}
fileHandle.seekToEndOfFile()
fileHandle.write(data)
}
}

private func legacyAppend(_ data: Data, to fileHandle: FileHandle) {
defer {
fileHandle.closeFile()

self?.logSize += Int64(data.count)
self?.trimLinesIfNecessary()
}
fileHandle.seekToEndOfFile()
fileHandle.write(data)
}

private func trimLinesIfNecessary() {
Expand Down

0 comments on commit 3c72dc5

Please sign in to comment.