如何在swift中设置PLCrashReporter.setCrashCallbacks?
How to set PLCrashReporter.setCrashCallbacks in swift?
我试图在 swift 中使用 setCrashCallbacks
调用设置 PLCrashReporter,但不知何故显然没有调用回调。我可以看到我所有的 NSLog 调用都出现在 Console.app 中。我还可以在下次发布时看到文本格式的报告。但是我在回调中的 NSLog 调用从未在 Console.app.
中显示
如何在 Swift 中执行此操作?甚至可以在 Swift 中执行此操作吗?或者我需要去 Objetive-C?
// Added via SPM
import CrashReporter
// global scope function
func handleSignalForCrashReport(_ siginfo: UnsafeMutablePointer<siginfo_t>?, _ context: UnsafeMutablePointer<ucontext_t>?, _ something: UnsafeMutableRawPointer?) {
// This never shows ?
NSLog("crash rep handleSignalForCrashReport")
// Do my thing here ...
}
// global scope variable
var callbacks = PLCrashReporterCallbacks(version: 0, context: nil, handleSignal: handleSignalForCrashReport)
class AppDelegate: UIResponder, UIApplicationDelegate {
func setupCrashReporter() {
let config = PLCrashReporterConfig(signalHandlerType: .mach, symbolicationStrategy: .all)
NSLog("crash rep 1")
guard let crashReporter = PLCrashReporter(configuration: config) else {
NSLog("crash rep 1.1")
return
}
NSLog("crash rep 2")
crashReporter.setCrash(&callbacks)
do {
try crashReporter.enableAndReturnError()
NSLog("crash rep 3")
} catch {
NSLog("crash rep 4 Warning: Could not enable crash reporter: \(error.localizedDescription)")
}
if crashReporter.hasPendingCrashReport() {
do {
let data = try crashReporter.loadPendingCrashReportDataAndReturnError()
let report = try PLCrashReport(data: data)
guard let reportText = PLCrashReportTextFormatter .stringValue(for: report, with: PLCrashReportTextFormatiOS) else {
NSLog("crash rep 5 report text nil")
return
}
NSLog("crash rep 6 previous report: \(reportText)")
try crashReporter.purgePendingCrashReportAndReturnError()
NSLog("crash rep 7")
} catch {
NSLog("crash rep Error: \(error)")
}
}
NSLog("crash rep finished installation")
}
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
setupCrashReporter()
return true
}
顺便说一句,这是来自官方存储库的 objective-C demo file,我从中得到了灵感。
我找不到使用 PLCrashReporter 的好方法。在花了几天时间调查之后,我最终转向了 KSCrash,它非常好并且高度可扩展。
如果有人有 PLCrashReported 的解决方案,我会很乐意将其作为答案 ✅
import KSCrash_Installations
import KSCrash_Recording
KSCrash.sharedInstance().deleteBehaviorAfterSendAll = KSCDeleteNever
KSCrash.sharedInstance()?.maxReportCount = 1
// MyCrashInstallation could be any subclass of KSCrashInstallation
var installation: KSCrashInstallation? = MyCrashInstallation.shared
installation?.appleReportStyle = KSAppleReportStyleUnsymbolicated
installation?.install()
installation?.onCrash = { writer in
let userId:String = "example123"
let now = "20210831"
writer?.pointee.addStringElement(writer, "my_key_userid", "\(userId)")
writer?.pointee.addStringElement(writer, "my_error_date", "\(now)")
}
我试图在 swift 中使用 setCrashCallbacks
调用设置 PLCrashReporter,但不知何故显然没有调用回调。我可以看到我所有的 NSLog 调用都出现在 Console.app 中。我还可以在下次发布时看到文本格式的报告。但是我在回调中的 NSLog 调用从未在 Console.app.
如何在 Swift 中执行此操作?甚至可以在 Swift 中执行此操作吗?或者我需要去 Objetive-C?
// Added via SPM
import CrashReporter
// global scope function
func handleSignalForCrashReport(_ siginfo: UnsafeMutablePointer<siginfo_t>?, _ context: UnsafeMutablePointer<ucontext_t>?, _ something: UnsafeMutableRawPointer?) {
// This never shows ?
NSLog("crash rep handleSignalForCrashReport")
// Do my thing here ...
}
// global scope variable
var callbacks = PLCrashReporterCallbacks(version: 0, context: nil, handleSignal: handleSignalForCrashReport)
class AppDelegate: UIResponder, UIApplicationDelegate {
func setupCrashReporter() {
let config = PLCrashReporterConfig(signalHandlerType: .mach, symbolicationStrategy: .all)
NSLog("crash rep 1")
guard let crashReporter = PLCrashReporter(configuration: config) else {
NSLog("crash rep 1.1")
return
}
NSLog("crash rep 2")
crashReporter.setCrash(&callbacks)
do {
try crashReporter.enableAndReturnError()
NSLog("crash rep 3")
} catch {
NSLog("crash rep 4 Warning: Could not enable crash reporter: \(error.localizedDescription)")
}
if crashReporter.hasPendingCrashReport() {
do {
let data = try crashReporter.loadPendingCrashReportDataAndReturnError()
let report = try PLCrashReport(data: data)
guard let reportText = PLCrashReportTextFormatter .stringValue(for: report, with: PLCrashReportTextFormatiOS) else {
NSLog("crash rep 5 report text nil")
return
}
NSLog("crash rep 6 previous report: \(reportText)")
try crashReporter.purgePendingCrashReportAndReturnError()
NSLog("crash rep 7")
} catch {
NSLog("crash rep Error: \(error)")
}
}
NSLog("crash rep finished installation")
}
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
setupCrashReporter()
return true
}
顺便说一句,这是来自官方存储库的 objective-C demo file,我从中得到了灵感。
我找不到使用 PLCrashReporter 的好方法。在花了几天时间调查之后,我最终转向了 KSCrash,它非常好并且高度可扩展。
如果有人有 PLCrashReported 的解决方案,我会很乐意将其作为答案 ✅
import KSCrash_Installations
import KSCrash_Recording
KSCrash.sharedInstance().deleteBehaviorAfterSendAll = KSCDeleteNever
KSCrash.sharedInstance()?.maxReportCount = 1
// MyCrashInstallation could be any subclass of KSCrashInstallation
var installation: KSCrashInstallation? = MyCrashInstallation.shared
installation?.appleReportStyle = KSAppleReportStyleUnsymbolicated
installation?.install()
installation?.onCrash = { writer in
let userId:String = "example123"
let now = "20210831"
writer?.pointee.addStringElement(writer, "my_key_userid", "\(userId)")
writer?.pointee.addStringElement(writer, "my_error_date", "\(now)")
}