Skip to content

Commit ad0055b

Browse files
authored
Merge pull request #91 from smartmobilefactory/feature/SentrySDK
Sentry smf logger integration and breadcrumbs integration.
2 parents 03f5ab9 + 8cab3fe commit ad0055b

1 file changed

Lines changed: 74 additions & 36 deletions

File tree

Sentry/SentrySDK+SMFLogger.swift

Lines changed: 74 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -14,33 +14,9 @@ class SentrySDK: NSObject {
1414

1515
fileprivate static var shared : SentrySDK?
1616

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-
4117
// MARK: - Private properties
4218

43-
fileprivate var isInitialized = false
19+
fileprivate var isInitialized = false
4420

4521
// MARK: - Public properties
4622

@@ -62,15 +38,7 @@ class SentrySDK: NSObject {
6238
///
6339
/// - Parameters:
6440
/// - 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-
}
41+
static func setup(configuration: SentrySDK.Configuration) {
7442

7543
// Make sure SentrySDK is not setup in a debug build
7644
guard (self.isDebugBuild == false) else {
@@ -81,9 +49,18 @@ class SentrySDK: NSObject {
8149
let instance = (self.shared ?? SentrySDK())
8250

8351
do {
84-
Client.shared = try Client(dsn: _sentryDsn)
52+
Client.shared = try Client(dsn: configuration.sentryDSN)
8553
Client.shared?.beforeSerializeEvent = { (event: Event) in
86-
event.environment = self.environment
54+
event.environment = SentrySDK.Configuration.environment
55+
56+
if (configuration.enableSMFLogUpload == true),
57+
let loggerContents = Logger.logFilesContent(maxSize: configuration.smfLogUploadMaxSize) {
58+
event.message = loggerContents
59+
}
60+
}
61+
62+
if (configuration.enableBreadcrumbs == true) {
63+
Client.shared?.enableAutomaticBreadcrumbTracking()
8764
}
8865

8966
try Client.shared?.startCrashHandler()
@@ -105,3 +82,64 @@ class SentrySDK: NSObject {
10582
fatalError("This is a test crash to trigger a crash report in Sentry Dashboard")
10683
}
10784
}
85+
86+
extension SentrySDK {
87+
88+
struct Configuration {
89+
fileprivate static let plistSentryDsn = "SentryDsn"
90+
91+
fileprivate static let isDebugBuild : Bool = {
92+
#if DEBUG
93+
return true
94+
#else
95+
return false
96+
#endif
97+
}()
98+
99+
fileprivate static let environment : String = {
100+
#if ALPHA
101+
return "alpha"
102+
#endif
103+
104+
#if BETA
105+
return "beta"
106+
#endif
107+
108+
#if LIVE
109+
return "live"
110+
#endif
111+
}()
112+
113+
fileprivate var sentryDSN : String = ""
114+
fileprivate var enableSMFLogUpload : Bool = true
115+
fileprivate var enableBreadcrumbs : Bool = true
116+
fileprivate var smfLogUploadMaxSize : Int = 5000
117+
118+
/// Initializes a SentrySDK Configuration
119+
///
120+
/// - Parameters:
121+
/// - sentryDSN: supply this manually if you dont want it in the info.plist
122+
/// - enableSMFLogUpload: true if you want the SMFLogger logs to be submitted with a crash
123+
/// - enableBreadcrumbs: true if you want Sentry to attach the last user interaction before a crash
124+
/// - smfLogUploadMaxSize: The max count of characters which should be uploaded
125+
init(sentryDSN: String? = nil, enableSMFLogUpload: Bool = true, smfLogUploadMaxSize: Int = 5000, smfEnableBreadcrumbs: Bool = true) {
126+
127+
// Get the Sentry DSN
128+
let dsnFromPlist = Bundle.main.object(forInfoDictionaryKey: SentrySDK.Configuration.plistSentryDsn) as? String
129+
130+
guard let _sentryDsn = (sentryDSN ?? dsnFromPlist) else {
131+
assertionFailure("Error: You have to set the `\(SentrySDK.Configuration.plistSentryDsn)` key in the info plist or specify your own when initializing teh SDK.")
132+
return
133+
}
134+
135+
self.sentryDSN = _sentryDsn
136+
self.enableBreadcrumbs = smfEnableBreadcrumbs
137+
self.enableSMFLogUpload = enableSMFLogUpload
138+
self.smfLogUploadMaxSize = smfLogUploadMaxSize
139+
}
140+
141+
static var `default`: SentrySDK.Configuration {
142+
return Configuration()
143+
}
144+
}
145+
}

0 commit comments

Comments
 (0)