Skip to content

Commit

Permalink
Merge commit 'ad8702e6a3d258f48fe3b585b5318948afa96104' into feature/…
Browse files Browse the repository at this point in the history
…63-crashes
  • Loading branch information
AvdLee committed May 27, 2020
2 parents bf5d708 + ad8702e commit 50ea2e6
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions Sources/DiagnosticsLogger.swift
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ public final class DiagnosticsLogger {

// MARK: - Setup
extension DiagnosticsLogger {

private func setup() throws {
if !FileManager.default.fileExists(atPath: logFileLocation.path) {
try FileManager.default.createDirectory(atPath: FileManager.default.documentsDirectory.path, withIntermediateDirectories: true, attributes: nil)
Expand Down Expand Up @@ -152,7 +153,7 @@ extension DiagnosticsLogger {
/// Reads the log and converts it to a `Data` object.
func readLog() -> Data? {
guard isSetup else {
assertionFailure()
assertionFailure("Trying to read the log while not set up")
return nil
}

Expand All @@ -166,7 +167,7 @@ extension DiagnosticsLogger {
}

private func log(message: String, file: String = #file, function: String = #function, line: UInt = #line) {
guard isSetup else { return assertionFailure() }
guard isSetup else { return assertionFailure("Trying to log a message while not set up") }

self.queue.async { [unowned self] in
let date = self.formatter.string(from: Date())
Expand All @@ -180,7 +181,7 @@ extension DiagnosticsLogger {
guard
let data = output.data(using: .utf8),
let fileHandle = logFileHandle else {
return assertionFailure()
return assertionFailure("Missing file handle or invalid output logged")
}

// Make sure we have enough disk space left. This prevents a crash due to a lack of space.
Expand All @@ -199,7 +200,7 @@ extension DiagnosticsLogger {
var data = try? Data(contentsOf: self.logFileLocation, options: .mappedIfSafe),
!data.isEmpty,
let newline = "\n".data(using: .utf8) else {
return assertionFailure()
return assertionFailure("Trimming the current log file failed")
}

var position: Int = 0
Expand All @@ -212,7 +213,7 @@ extension DiagnosticsLogger {
data.removeSubrange(0 ..< position)

guard (try? data.write(to: logFileLocation, options: .atomic)) != nil else {
return assertionFailure()
return assertionFailure("Could not write trimmed log to target file location: \(logFileLocation)")
}
}
}
Expand Down Expand Up @@ -243,7 +244,7 @@ private extension DiagnosticsLogger {
outputPipe.fileHandleForWriting.write(data)

guard let string = String(data: data, encoding: .utf8) else {
return assertionFailure()
return assertionFailure("Invalid data is logged")
}

string.enumerateLines(invoking: { [weak self] (line, _) in
Expand Down

0 comments on commit 50ea2e6

Please sign in to comment.