-
Notifications
You must be signed in to change notification settings - Fork 12
/
CrashLogging.swift
328 lines (261 loc) · 12.3 KB
/
CrashLogging.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
import Foundation
import Sentry
#if SWIFT_PACKAGE
import AutomatticTracksEvents
import AutomatticTracksModel
import AutomatticEncryptedLogs
#endif
/// A class that provides support for logging crashes. Not compatible with Objective-C.
public class CrashLogging {
/// We haven't fully evicted global state from all of Tracks yet, so we keep a global reference around for now
struct Internals {
static var crashLogging: CrashLogging?
}
private let dataProvider: CrashLoggingDataProvider
private let eventLogging: EventLogging?
/// If you set this key to `true` in UserDefaults, crash logging will be
/// sent even in DEBUG builds. If it is `false` or not present, then
/// crash log events will only be sent in Release builds.
public static let forceCrashLoggingKey = "force-crash-logging"
public let flushTimeout: TimeInterval
/// Initializes the crash logging system
///
/// - Parameters:
/// - dataProvider: An object that provides any configuration to the crash logging system
/// - eventLogging: An associated `EventLogging` object that provides integration between the Crash Logging and Event Logging subsystems
/// - flushTimeout: The `TimeInterval` to wait for when flushing events and crahses queued to be sent to the remote
public init(
dataProvider: CrashLoggingDataProvider,
eventLogging: EventLogging? = nil,
flushTimeout: TimeInterval = 15
) {
self.dataProvider = dataProvider
self.eventLogging = eventLogging
self.flushTimeout = flushTimeout
}
/// Starts the CrashLogging subsystem by initializing Sentry.
///
/// Should be called as early as possible in the application lifecycle
public func start() throws -> CrashLogging {
/// Validate the DSN ourselves before initializing, because the SentrySDK silently prints the error to the log instead of telling us if the DSN is valid
_ = try SentryDsn(string: self.dataProvider.sentryDSN)
SentrySDK.start { options in
options.dsn = self.dataProvider.sentryDSN
options.debug = false
options.diagnosticLevel = .error
options.environment = self.dataProvider.buildType
options.enableAutoSessionTracking = self.dataProvider.shouldEnableAutomaticSessionTracking
options.enableAppHangTracking = self.dataProvider.enableAppHangTracking
options.enableCaptureFailedRequests = self.dataProvider.enableCaptureFailedRequests
options.beforeSend = self.beforeSend
/// Attach stack traces to non-fatal errors
options.attachStacktrace = true
// Events
options.sampleRate = NSNumber(value: min(max(self.dataProvider.errorEventsSamplingRate, 0), 1))
// Performance monitoring options
options.enableAutoPerformanceTracing = self.dataProvider.enableAutoPerformanceTracking
options.tracesSampler = { _ in
// To keep our implementation as Sentry agnostic as possible, we don't pass the
// input `SamplingContext` down the chain.
NSNumber(value: self.dataProvider.tracesSampler())
}
options.profilesSampleRate = NSNumber(value: self.dataProvider.profilingRate)
options.enableNetworkTracking = self.dataProvider.enableNetworkTracking
options.enableFileIOTracing = self.dataProvider.enableFileIOTracking
options.enableCoreDataTracing = self.dataProvider.enableCoreDataTracking
#if os(iOS) || os(tvOS) || targetEnvironment(macCatalyst)
options.enableUserInteractionTracing = self.dataProvider.enableUserInteractionTracing
options.enableUIViewControllerTracing = self.dataProvider.enableUIViewControllerTracking
#endif
}
Internals.crashLogging = self
return self
}
func beforeSend(event: Sentry.Event?) -> Sentry.Event? {
TracksLogDebug("📜 Firing `beforeSend`")
#if DEBUG
TracksLogDebug("📜 This is a debug build")
let shouldSendEvent = UserDefaults.standard.bool(forKey: Self.forceCrashLoggingKey) && !dataProvider.userHasOptedOut
#else
let shouldSendEvent = !dataProvider.userHasOptedOut
#endif
if shouldSendEvent == false {
TracksLogDebug("📜 Events will not be sent because user has opted-out.")
}
/// If we shouldn't send the event we have nothing else to do here
guard let event = event, shouldSendEvent else {
return nil
}
if event.tags == nil {
event.tags = [String: String]()
}
event.tags?["locale"] = NSLocale.current.languageCode
/// Always provide a value in order to determine how often we're unable to retrieve application state
event.tags?["app.state"] = ApplicationFacade().applicationState ?? "unknown"
/// Read the current user from the Data Provider (though the Data Provider can decide not to provide it for functional or privacy reasons)
event.user = dataProvider.currentUser?.sentryUser
/// Everything below this line is related to event logging, so if it's not set up we can exit
guard let eventLogging = self.eventLogging else {
TracksLogDebug("📜 Cancelling log file attachment – Event Logging is not initialized")
return event
}
eventLogging.attachLogToEventIfNeeded(event: event)
return event
}
/// Immediately crashes the application and generates a crash report.
public func crash() {
SentrySDK.crash()
}
enum Errors: LocalizedError {
case unableToConstructAuthStringError
}
}
// MARK: - Manual Error Logging
public extension CrashLogging {
/// Writes a JavaScript exception to the Crash Logging system, including its stack trace.
/// Note that this function is provided mainly for hybrid sources like React Native.
///
/// - Parameters:
/// - exception: The exception object
/// - callback: Callback triggered upon completion
func logJavaScriptException(_ jsException: JSException, callback: @escaping () -> Void) {
SentrySDK.capture(event: SentryEventJSException.init(jsException: jsException))
DispatchQueue.global().async {
SentrySDK.flush(timeout: self.flushTimeout)
callback()
}
}
/// Writes the error to the Crash Logging system, and includes a stack trace. This API supports for rich events.
/// By setting a Tag/Value pair, you'll be able to filter these events, directly, with the `has:` operator (Sentry Web Interface).
///
/// - Parameters:
/// - error: The error object
/// - tags: Tag Key/Value pairs to be set in the Error's Scope
/// - level: The level of severity to report in Sentry (`.error` by default)
func logError(_ error: Error, tags: [String: String], level: SentryLevel = .error) {
let event = Event.from(
error: error as NSError,
level: level
)
SentrySDK.capture(event: event) { scope in
for (key, value) in tags {
scope.setTag(value: value, key: key)
}
}
dataProvider.didLogErrorCallback?(event)
}
/// Writes the error to the Crash Logging system, and includes a stack trace.
///
/// - Parameters:
/// - error: The error object
/// - userInfo: A dictionary containing additional data about this error.
/// - level: The level of severity to report in Sentry (`.error` by default)
func logError(_ error: Error, userInfo: [String: Any]? = nil, level: SentryLevel = .error) {
let userInfo = userInfo ?? (error as NSError).userInfo
let event = Event.from(
error: error as NSError,
level: level,
extra: userInfo
)
SentrySDK.capture(event: event)
dataProvider.didLogErrorCallback?(event)
}
/// Writes a message to the Crash Logging system, and includes a stack trace.
///
/// - Parameters:
/// - message: The message
/// - properties: A dictionary containing additional information about this error
/// - level: The level of severity to report in Sentry (`.info` by default)
func logMessage(_ message: String, properties: [String: Any]? = nil, level: SentryLevel = .info) {
let event = Event(level: level)
event.message = SentryMessage(formatted: message)
event.extra = properties
event.timestamp = Date()
SentrySDK.capture(event: event)
dataProvider.didLogMessageCallback?(event)
}
/// Sends an `Event` to Sentry and triggers a callback on completion
func logErrorImmediately(_ error: Error, userInfo: [String: Any]? = nil, level: SentryLevel = .error, callback: @escaping () -> Void) {
logErrorsImmediately([error], userInfo: userInfo, level: level, callback: callback)
}
func logErrorsImmediately(_ errors: [Error], userInfo: [String: Any]? = nil, level: SentryLevel = .error, callback: @escaping () -> Void) {
logErrorsImmediately(errors, userInfo: userInfo, level: level, andWait: false, callback: callback)
}
/**
Writes the error to the Crash Logging system, and includes a stack trace. This method will block the thread until the event is fired.
- Parameters:
- error: The error object
- userInfo: A dictionary containing additional data about this error.
- level: The level of severity to report in Sentry (`.error` by default)
*/
func logErrorAndWait(_ error: Error, userInfo: [String: Any]? = nil, level: SentryLevel = .error) {
logErrorsImmediately([error], userInfo: userInfo, level: level, andWait: true, callback: {})
TracksLogDebug("💥 Events flush completed. When using Sentry, this either means all events were sent or that the flush timeout was reached.")
}
private func logErrorsImmediately(
_ errors: [Error],
userInfo: [String: Any]? = nil,
level: SentryLevel = .error,
andWait wait: Bool,
callback: @escaping () -> Void
) {
errors.forEach { error in
// Amending the global scope on a per-event basis seems like the best way to add the
// caller-provided `userInfo` and `level`.
SentrySDK.capture(error: error) { scope in
// Under the hood, `setExtras` uses `NSMutableDictionary` `addEntriesFromDictionary`
// meaning this won't replace the whole extras dictionary.
scope.setExtras(userInfo)
scope.setLevel(level)
}
}
let flushEventThenCallCallback: () -> Void = {
SentrySDK.flush(timeout: self.flushTimeout)
callback()
}
if wait {
flushEventThenCallCallback()
} else {
DispatchQueue.global().async { flushEventThenCallCallback() }
}
}
}
// MARK: - User Tracking
extension CrashLogging {
internal var currentUser: Sentry.User {
let anonymousUser = TracksUser(userID: nil, email: nil, username: nil).sentryUser
/// Don't continue if the data source doesn't yet have a user
guard let user = dataProvider.currentUser else { return anonymousUser }
let data = dataProvider.additionalUserData
return user.sentryUser(withData: data)
}
/// Causes the Crash Logging System to refresh its knowledge about the current state of the system.
///
/// This is required in situations like login / logout, when the system otherwise might not
/// know a change has occured.
///
/// Calling this method in these situations prevents
public func setNeedsDataRefresh() {
SentrySDK.setUser(currentUser)
}
}
internal extension TracksUser {
var sentryUser: Sentry.User {
let user = Sentry.User()
if let userID = self.userID {
user.userId = userID
}
if let email = self.email {
user.email = email
}
if let username = user.username {
user.username = username
}
return user
}
func sentryUser(withData additionalUserData: [String: Any]) -> Sentry.User {
let user = self.sentryUser
user.data = additionalUserData
return user
}
}