Skip to content

Commit

Permalink
Merge pull request #165 from WeTransfer/feature/tmob-5114-read-log-fa…
Browse files Browse the repository at this point in the history
…ilure

[TMOB 5114] Log failures happening during log reading
  • Loading branch information
AvdLee authored Mar 4, 2024
2 parents 0449744 + 45a56ed commit 4a362cc
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 24 deletions.
22 changes: 17 additions & 5 deletions Sources/Logging/DiagnosticsLogger.swift
Original file line number Diff line number Diff line change
Expand Up @@ -138,19 +138,31 @@ extension DiagnosticsLogger {
}

/// Reads the log and converts it to a `Data` object.
func readLog() -> Data? {
func readLog() throws -> Data? {
guard isSetup else {
assertionFailure("Trying to read the log while not set up")
return nil
}

return queue.sync {
return try queue.sync {
let coordinator = NSFileCoordinator(filePresenter: nil)
var error: NSError?
var coordinateError: NSError?
var dataError: Error?
var logData: Data?
coordinator.coordinate(readingItemAt: logFileLocation, error: &error) { url in
logData = try? Data(contentsOf: url)
coordinator.coordinate(readingItemAt: logFileLocation, error: &coordinateError) { url in
do {
logData = try Data(contentsOf: url)
} catch {
dataError = error
}
}

if let coordinateError {
throw coordinateError
} else if let dataError {
throw dataError
}

return logData
}
}
Expand Down
42 changes: 23 additions & 19 deletions Sources/Reporters/LogsReporter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,32 @@ struct LogsReporter: DiagnosticsReporting {
let title: String = "Session Logs"

var diagnostics: String {
guard let data = DiagnosticsLogger.standard.readLog(), let logs = String(data: data, encoding: .utf8) else {
return "Parsing the log failed"
}
do {
guard let data = try DiagnosticsLogger.standard.readLog(), let logs = String(data: data, encoding: .utf8) else {
return "Parsing the log failed (Unknown error)"
}

let sessions = logs.components(separatedBy: "\n\n---\n\n").reversed()
var diagnostics = ""
sessions.forEach { session in
guard !session.isEmpty else { return }

diagnostics += "<div class=\"collapsible-session\">"
diagnostics += "<details>"
if session.isOldStyleSession {
let title = session.split(whereSeparator: \.isNewline).first ?? "Unknown session title"
diagnostics += "<summary>\(title)</summary>"
diagnostics += "<pre>\(session.addingHTMLEncoding())</pre>"
} else {
diagnostics += session
let sessions = logs.components(separatedBy: "\n\n---\n\n").reversed()
var diagnostics = ""
sessions.forEach { session in
guard !session.isEmpty else { return }

diagnostics += "<div class=\"collapsible-session\">"
diagnostics += "<details>"
if session.isOldStyleSession {
let title = session.split(whereSeparator: \.isNewline).first ?? "Unknown session title"
diagnostics += "<summary>\(title)</summary>"
diagnostics += "<pre>\(session.addingHTMLEncoding())</pre>"
} else {
diagnostics += session
}
diagnostics += "</details>"
diagnostics += "</div>"
}
diagnostics += "</details>"
diagnostics += "</div>"
return diagnostics
} catch {
return "Parsing the log failed (\(error.localizedDescription))"
}
return diagnostics
}

func report() -> DiagnosticsChapter {
Expand Down

0 comments on commit 4a362cc

Please sign in to comment.