NetNewsWire/Mac/CrashReporter/CrashReporter.swift

74 lines
2.4 KiB
Swift
Raw Normal View History

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
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 {
struct DefaultsKey {
static let sendCrashLogsAutomaticallyKey = "SendCrashLogsAutomatically"
}
2018-12-18 07:44:06 +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.
/// 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
if shouldSendCrashLogsAutomatically() {
2020-12-12 01:09:36 +01:00
sendCrashLogText(crashLogText)
} else {
runCrashReporterWindow(crashLogText)
}
2020-12-12 01:09:36 +01:00
crashReporter.purgePendingCrashReport()
}
2018-12-18 07:44:06 +01:00
2018-12-29 21:31:27 +01:00
static func sendCrashLogText(_ crashLogText: String) {
var request = URLRequest(url: URL(string: "https://services.netnewswire.com/reportCrash.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
2019-01-12 06:59:27 +01:00
download(request) { (_, _, _) in
// Dont care about the result.
}
}
2020-12-12 01:09:36 +01:00
static func runCrashReporterWindow(_ crashLogText: String) {
crashReportWindowController = CrashReportWindowController(crashLogText: crashLogText)
crashReportWindowController!.showWindow(self)
2018-12-18 07:44:06 +01:00
}
}
private extension CrashReporter {
2018-12-18 07:44:06 +01:00
static func shouldSendCrashLogsAutomatically() -> Bool {
return UserDefaults.standard.bool(forKey: DefaultsKey.sendCrashLogsAutomaticallyKey)
}
}