首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何快速设置PLCrashReporter.setCrashCallbacks?

如何快速设置PLCrashReporter.setCrashCallbacks?
EN

Stack Overflow用户
提问于 2021-06-03 10:15:53
回答 1查看 256关注 0票数 1

我试图用setCrashCallbacks快速调用setup setCrashCallbacks,但不知怎么的,回调显然没有被调用。我可以看到我所有的NSLog调用都出现在Console.app中。在下一次发布时,我还可以看到文本格式的报告。但是,我在回调中的NSLog调用从未在Console.app中显示。

我怎么能在斯威夫特做这件事?在斯威夫特能做到这一点吗?还是我要去-C?

代码语言:javascript
复制
// 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演示文件,我从这里得到了一些想法。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-08-18 16:31:41

我找不到一种使用PLCrashReporter的好方法。经过几天的调查之后,我最终搬到了KSCrash,这是相当好的和高度可扩展的。

如果有人有PLCrashReported的解决方案,我会很高兴地给出答案的✅

代码语言:javascript
复制
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)")
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67819590

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档