-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathLogger.swift
More file actions
217 lines (190 loc) · 6.72 KB
/
Logger.swift
File metadata and controls
217 lines (190 loc) · 6.72 KB
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
// capture-es - bitdrift's ES SDK
// Copyright Bitdrift, Inc. All rights reserved.
//
// Use of this source code is governed by a source available license that can be found in the
// LICENSE file or at:
// https://polyformproject.org/wp-content/uploads/2020/06/PolyForm-Shield-1.0.0.txt
import Capture
import Foundation
// Must match src/index.tsx ISSUE_REPORT_EVENT and Android/iOS bridge constants.
let CAPRNIssueReportDidEmitNotification = Notification.Name("BdReactNative.onBeforeReportSend")
@objc public class CAPRNLogger: NSObject {
private static let debugId: String? = {
DebugId.fromBundle()
}()
@objc public static func start(
key: String,
sessionStrategy: String,
url: String?,
enableNetworkInstrumentation: Bool,
enableNativeFatalIssues: Bool,
enableJsErrors: Bool,
enableIssueCallbackBridge: Bool
) {
ReportDirectory.setupWatcherDirectory(enableJsErrors: enableJsErrors)
let strategy =
switch sessionStrategy {
case "fixed":
SessionStrategy.fixed()
case "activity":
SessionStrategy.activityBased()
default:
SessionStrategy.fixed()
}
let issueCallbackConfiguration: IssueCallbackConfiguration?
if enableIssueCallbackBridge {
issueCallbackConfiguration = IssueCallbackConfiguration(
callbackQueue: DispatchQueue(label: "io.bitdrift.capture.reactnative.issue-callback", qos: .utility),
issueReportCallback: CAPRNIssueReportCallback()
)
} else {
issueCallbackConfiguration = nil
}
let configuration = Configuration(
enableFatalIssueReporting: enableNativeFatalIssues,
apiURL: URL(string: url ?? "https://api.bitdrift.io")!,
issueCallbackConfiguration: issueCallbackConfiguration
)
let integrator = Capture.Logger.start(
withAPIKey: key,
sessionStrategy: strategy,
configuration: configuration
)
if enableNetworkInstrumentation {
integrator?.enableIntegrations([.urlSession()])
}
}
@objc
public static func getDeviceID(
_ resolve: @escaping RCTPromiseResolveBlock,
rejecter reject: @escaping RCTPromiseRejectBlock
) {
let deviceID = Capture.Logger.deviceID
if deviceID == nil {
reject("device_id_undefined", "Device ID is undefined", nil)
return
}
resolve(deviceID)
}
@objc
public static func log(_ level: Double, message: String, fields: [String: String]) {
switch level {
case 0.0:
Capture.Logger.logTrace(message, fields: fields)
case 1.0:
Capture.Logger.logDebug(message, fields: fields)
case 2.0:
Capture.Logger.logInfo(message, fields: fields)
case 3.0:
Capture.Logger.logWarning(message, fields: fields)
case 4.0:
Capture.Logger.logError(message, fields: fields)
default:
return
}
}
@objc
public static func addField(_ key: String, value: String) {
Capture.Logger.addField(withKey: key, value: value)
}
@objc
public static func removeField(_ key: String) {
Capture.Logger.removeField(withKey: key)
}
@objc
public static func getSessionID(
_ resolve: @escaping RCTPromiseResolveBlock,
rejecter reject: @escaping RCTPromiseRejectBlock
) {
let sessionID = Capture.Logger.sessionID
if sessionID == nil {
reject("session_id_undefined", "Session ID is undefined", nil)
return
}
resolve(sessionID)
}
@objc
public static func getSessionURL(
_ resolve: @escaping RCTPromiseResolveBlock,
rejecter reject: @escaping RCTPromiseRejectBlock
) {
let sessionURL = Capture.Logger.sessionURL
if sessionURL == nil {
reject("session_url_undefined", "Session URL is undefined", nil)
return
}
resolve(sessionURL)
}
@objc
public static func getPreviousRunInfo() -> [String: Any]? {
guard let info = Capture.Logger.previousRunInfo else {
return nil
}
return ["hasFatallyTerminated": info.hasFatallyTerminated]
}
@objc
public static func logScreenView(screenName: String) {
Capture.Logger.logScreenView(screenName: screenName)
}
@objc
public static func logAppLaunchTTI(TTI: TimeInterval) {
Capture.Logger.logAppLaunchTTI(TTI)
}
@objc
public static func reportJsError(
errorName: String,
message: String,
stack: String,
isFatal: Bool,
engine: String,
reactNativeVersion: String
) {
guard let destinationPath = ReportDirectory.destinationPath(isFatal: isFatal) else {
return
}
let debugId = Self.debugId ?? ""
let deviceMetadata = DeviceMetadata.current()
let appMetadata = AppMetadata.current()
let timeInterval = Date().timeIntervalSince1970
let timestampSeconds = UInt64(timeInterval)
let fractionalSeconds = timeInterval - Double(timestampSeconds)
let timestampNanos = UInt32(fractionalSeconds * 1_000_000_000)
JavaScriptErrorReport.persist(
errorName: errorName,
message: message,
stack: stack,
isFatal: isFatal,
engine: engine,
debugId: debugId,
timestampSeconds: timestampSeconds,
timestampNanos: timestampNanos,
destinationPath: destinationPath,
deviceMetadata: deviceMetadata,
appMetadata: appMetadata,
sdkVersion: reactNativeVersion
)
}
@objc
public static func setFeatureFlagExposureString(name: String, variant: String) {
Capture.Logger.setFeatureFlagExposure(withName: name, variant: variant)
}
@objc
public static func setFeatureFlagExposureBool(name: String, variant: Bool) {
Capture.Logger.setFeatureFlagExposure(withName: name, variant: variant)
}
}
private final class CAPRNIssueReportCallback: NSObject, IssueReportCallback {
func onBeforeReportSend(report: IssueReport) {
NotificationCenter.default.post(
name: CAPRNIssueReportDidEmitNotification,
object: nil,
userInfo: [
"reportType": report.reportType,
"reason": report.reason,
"details": report.details,
"sessionId": report.sessionID,
"fields": report.fields,
]
)
}
}