|
| 1 | +// |
| 2 | +// SentrySDK+SMFLogger.swift |
| 3 | +// |
| 4 | +// Copyright © 2019 Smart Mobile Factory. All rights reserved. |
| 5 | +// |
| 6 | + |
| 7 | +import Foundation |
| 8 | +import Sentry |
| 9 | +import SMFLogger |
| 10 | + |
| 11 | +class SentrySDK: NSObject { |
| 12 | + |
| 13 | + // MARK: - Private static properties |
| 14 | + |
| 15 | + fileprivate static var shared : SentrySDK? |
| 16 | + |
| 17 | + fileprivate static let plistSentryDsn = "SentryDsn" |
| 18 | + |
| 19 | + fileprivate static let isDebugBuild : Bool = { |
| 20 | + #if DEBUG |
| 21 | + return true |
| 22 | + #else |
| 23 | + return false |
| 24 | + #endif |
| 25 | + }() |
| 26 | + |
| 27 | + fileprivate static let environment : String = { |
| 28 | + #if ALPHA |
| 29 | + return "alpha" |
| 30 | + #endif |
| 31 | + |
| 32 | + #if BETA |
| 33 | + return "beta" |
| 34 | + #endif |
| 35 | + |
| 36 | + #if LIVE |
| 37 | + return "live" |
| 38 | + #endif |
| 39 | + }() |
| 40 | + |
| 41 | + // MARK: - Private properties |
| 42 | + |
| 43 | + fileprivate var isInitialized = false |
| 44 | + |
| 45 | + // MARK: - Public properties |
| 46 | + |
| 47 | + static var wasInitialized : Bool { |
| 48 | + return (SentrySDK.shared?.isInitialized ?? false) |
| 49 | + } |
| 50 | + |
| 51 | + // MARK: - Initialization |
| 52 | + |
| 53 | + override init() { |
| 54 | + super.init() |
| 55 | + |
| 56 | + SentrySDK.shared = self |
| 57 | + } |
| 58 | + |
| 59 | + // MARK: - Methods |
| 60 | + |
| 61 | + /// This will setup the SentrySDK with the common base configuration. Crashes will be detected if the app is build with the release build type and the sentry dsn taken from the info plists. |
| 62 | + /// |
| 63 | + /// - Parameters: |
| 64 | + /// - configuration: sentry dsn string |
| 65 | + static func setup(sentryDsn: String? = nil) { |
| 66 | + |
| 67 | + // Get the Sentry dsn |
| 68 | + let idFromPlist = Bundle.main.object(forInfoDictionaryKey: SentrySDK.plistSentryDsn) as? String |
| 69 | + |
| 70 | + guard let _sentryDsn = (sentryDsn ?? idFromPlist) else { |
| 71 | + assertionFailure("Error: You have to set the `\(SentrySDK.plistSentryDsn)` key in the info plist.") |
| 72 | + return |
| 73 | + } |
| 74 | + |
| 75 | + // Make sure SentrySDK is not setup in a debug build |
| 76 | + guard (self.isDebugBuild == false) else { |
| 77 | + // Configure SentrySDK only for non debug builds or if the exception flag is set to true |
| 78 | + return |
| 79 | + } |
| 80 | + |
| 81 | + let instance = (self.shared ?? SentrySDK()) |
| 82 | + |
| 83 | + do { |
| 84 | + Client.shared = try Client(dsn: _sentryDsn) |
| 85 | + Client.shared?.beforeSerializeEvent = { (event: Event) in |
| 86 | + event.environment = self.environment |
| 87 | + } |
| 88 | + |
| 89 | + try Client.shared?.startCrashHandler() |
| 90 | + instance.isInitialized = true |
| 91 | + } catch let error { |
| 92 | + Log.Channel.Manager.sentry.error("Error initialising sentry: \(error.localizedDescription)") |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + /// This will create a `fatalError` to crash the app. |
| 97 | + static func performTestCrash() { |
| 98 | + |
| 99 | + guard (self.shared?.isInitialized == true) else { |
| 100 | + assertionFailure("Error: You have to setup `SentrySDK` before performing a test crash. The test crash won't be performed otherwise.") |
| 101 | + return |
| 102 | + } |
| 103 | + |
| 104 | + Client.shared?.reportUserException("TestCrash", reason: "Only testing crashes", language: "swift", lineOfCode: "23", stackTrace: [], logAllThreads: false, terminateProgram: true) |
| 105 | + fatalError("This is a test crash to trigger a crash report in Sentry Dashboard") |
| 106 | + } |
| 107 | +} |
0 commit comments