NetNewsWire/Mac/CrashReporter/CrashReporter.swift

74 lines
2.4 KiB
Swift
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// CrashReporter.swift
// NetNewsWire
//
// Created by Brent Simmons on 12/17/18.
// Copyright © 2018 Ranchero Software. All rights reserved.
//
import Foundation
import RSWeb
import CrashReporter
// Displays a window that shows the crash log  gives the user the chance to add data.
// (Or just decide not to send it.)
// This code is not included in the MAS build.
// At some point this code should probably move into RSCore, so Rainier and any other
// future apps can use it.
struct CrashReporter {
struct DefaultsKey {
static let sendCrashLogsAutomaticallyKey = "SendCrashLogsAutomatically"
}
private static var crashReportWindowController: CrashReportWindowController?
/// Look in ~/Library/Logs/DiagnosticReports/ for a new crash log for this app.
/// Show a crash log reporter window if found.
static func check(crashReporter: PLCrashReporter) {
guard crashReporter.hasPendingCrashReport(),
let crashData = crashReporter.loadPendingCrashReportData(),
let crashReport = try? PLCrashReport(data: crashData),
let crashLogText = PLCrashReportTextFormatter.stringValue(for: crashReport, with: PLCrashReportTextFormatiOS) else { return }
if shouldSendCrashLogsAutomatically() {
sendCrashLogText(crashLogText)
} else {
runCrashReporterWindow(crashLogText)
}
crashReporter.purgePendingCrashReport()
}
static func sendCrashLogText(_ crashLogText: String) {
var request = URLRequest(url: URL(string: "https://netnewswire.com/crashreportcatcher.php")!)
request.httpMethod = HTTPMethod.post
let boundary = "0xKhTmLbOuNdArY"
let contentType = "multipart/form-data; boundary=\(boundary)"
request.setValue(contentType, forHTTPHeaderField:HTTPRequestHeader.contentType)
let formString = "--\(boundary)\r\nContent-Disposition: form-data; name=\"crashlog\"\r\n\r\n\(crashLogText)\r\n--\(boundary)--\r\n"
let formData = formString.data(using: .utf8, allowLossyConversion: true)
request.httpBody = formData
download(request) { (_, _, _) in
// Dont care about the result.
}
}
static func runCrashReporterWindow(_ crashLogText: String) {
crashReportWindowController = CrashReportWindowController(crashLogText: crashLogText)
crashReportWindowController!.showWindow(self)
}
}
private extension CrashReporter {
static func shouldSendCrashLogsAutomatically() -> Bool {
return UserDefaults.standard.bool(forKey: DefaultsKey.sendCrashLogsAutomaticallyKey)
}
}