Skip to content

Commit 03f5ab9

Browse files
authored
Merge pull request #90 from smartmobilefactory/feature/SentrySDK
Added SentrySDK and SentrySDK+SMFLogger
2 parents 62097fe + 35367e5 commit 03f5ab9

3 files changed

Lines changed: 214 additions & 0 deletions

File tree

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ This Repo contains our common project setup files.
88
Helpers which can be added manually to the Xcode project which should be used:
99

1010
- [HockeySDK.swift](#hockeyapp-sdk)
11+
- [SentrySDK.swift](#sentry-sdk)
1112
- [BuglifeSDK.swift](#buglife-sdk)
1213
- [LifetimeTrackerSDK](#lifetimetracker-sdk)
1314

@@ -48,6 +49,10 @@ If you want to add more parameters, eg. to disable SwiftLint, you have to add th
4849

4950
This repo contains the `HockeySDK` helper struct which takes care of the default HockeyApp SDK setup. The SDK will be initialized with the App ID and the Crash Manager started.
5051

52+
### Sentry-SDK
53+
54+
This repo contains the `SentrySKD` (plus the SMFLogger variant) helper struct which takes care of the default Sentry SDK setup. The SDK will be initialized with the Sentry DSN (in the info.plist).
55+
5156
#### Integrate the HockeyApp SDK
5257
To use the HockeySDK.swift helper struct you have to manually add the HockeyApp SDK to your project first. Use the preferred way to do this - at this time it's [CocoaPods](https://cocoapods.org).
5358

Sentry/SentrySDK+SMFLogger.swift

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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+
}

Sentry/SentrySDK.swift

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

0 commit comments

Comments
 (0)