Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use latest write APIs to prevent a crash when storage is full. #87

Merged
merged 2 commits into from
Jul 22, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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