2018-12-18 07:44:06 +01:00
|
|
|
|
//
|
|
|
|
|
// CrashReporter.swift
|
|
|
|
|
// NetNewsWire
|
|
|
|
|
//
|
|
|
|
|
// Created by Brent Simmons on 12/17/18.
|
|
|
|
|
// Copyright © 2018 Ranchero Software. All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
import Foundation
|
2024-04-02 04:31:57 +02:00
|
|
|
|
import Web
|
2020-12-12 01:09:36 +01:00
|
|
|
|
import CrashReporter
|
2018-12-18 07:44:06 +01:00
|
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
|
|
2024-03-20 07:05:30 +01:00
|
|
|
|
@MainActor struct CrashReporter {
|
2018-12-21 07:21:42 +01:00
|
|
|
|
|
2018-12-22 21:01:31 +01:00
|
|
|
|
struct DefaultsKey {
|
|
|
|
|
static let sendCrashLogsAutomaticallyKey = "SendCrashLogsAutomatically"
|
2018-12-21 07:21:42 +01:00
|
|
|
|
}
|
2018-12-18 07:44:06 +01:00
|
|
|
|
|
2019-01-12 07:50:36 +01:00
|
|
|
|
private static var crashReportWindowController: CrashReportWindowController?
|
|
|
|
|
|
2018-12-18 07:44:06 +01:00
|
|
|
|
/// Look in ~/Library/Logs/DiagnosticReports/ for a new crash log for this app.
|
2018-12-22 21:01:31 +01:00
|
|
|
|
/// Show a crash log reporter window if found.
|
2020-12-12 01:09:36 +01:00
|
|
|
|
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 }
|
2018-12-18 07:44:06 +01:00
|
|
|
|
|
2018-12-22 21:01:31 +01:00
|
|
|
|
if shouldSendCrashLogsAutomatically() {
|
2020-12-12 01:09:36 +01:00
|
|
|
|
sendCrashLogText(crashLogText)
|
|
|
|
|
} else {
|
|
|
|
|
runCrashReporterWindow(crashLogText)
|
2018-12-21 07:21:42 +01:00
|
|
|
|
}
|
2020-12-12 01:09:36 +01:00
|
|
|
|
|
|
|
|
|
crashReporter.purgePendingCrashReport()
|
2018-12-22 21:01:31 +01:00
|
|
|
|
}
|
2018-12-18 07:44:06 +01:00
|
|
|
|
|
2018-12-29 21:31:27 +01:00
|
|
|
|
static func sendCrashLogText(_ crashLogText: String) {
|
2021-03-03 07:08:11 +01:00
|
|
|
|
var request = URLRequest(url: URL(string: "https://services.netnewswire.com/reportCrash.php")!)
|
2019-01-10 08:02:01 +01:00
|
|
|
|
request.httpMethod = HTTPMethod.post
|
|
|
|
|
|
|
|
|
|
let boundary = "0xKhTmLbOuNdArY"
|
|
|
|
|
|
2019-01-12 07:11:46 +01:00
|
|
|
|
let contentType = "multipart/form-data; boundary=\(boundary)"
|
2019-01-10 08:02:01 +01:00
|
|
|
|
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
|
|
|
|
|
|
2019-01-12 06:59:27 +01:00
|
|
|
|
download(request) { (_, _, _) in
|
|
|
|
|
// Don’t care about the result.
|
|
|
|
|
}
|
2018-12-22 21:01:31 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-12-12 01:09:36 +01:00
|
|
|
|
static func runCrashReporterWindow(_ crashLogText: String) {
|
|
|
|
|
crashReportWindowController = CrashReportWindowController(crashLogText: crashLogText)
|
2019-01-12 07:50:36 +01:00
|
|
|
|
crashReportWindowController!.showWindow(self)
|
2018-12-18 07:44:06 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-21 07:21:42 +01:00
|
|
|
|
private extension CrashReporter {
|
2018-12-18 07:44:06 +01:00
|
|
|
|
|
2018-12-22 21:01:31 +01:00
|
|
|
|
static func shouldSendCrashLogsAutomatically() -> Bool {
|
|
|
|
|
return UserDefaults.standard.bool(forKey: DefaultsKey.sendCrashLogsAutomaticallyKey)
|
2018-12-21 07:21:42 +01:00
|
|
|
|
}
|
|
|
|
|
}
|