Skip to content

Commit c430d6f

Browse files
committed
Add callback for iOS example
1 parent a1e2439 commit c430d6f

File tree

3 files changed

+133
-30
lines changed

3 files changed

+133
-30
lines changed

examples/swift/hello_world/LoggerCustomer.swift

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,19 @@ final class LoggerCustomer: NSObject, URLSessionDelegate {
8888
return
8989
}
9090

91+
let issueCallbackConfiguration = IssueCallbackConfiguration(
92+
callbackQueue: .main,
93+
issueReportCallback: CustomerIssueReportCallback()
94+
)
95+
9196
Logger
9297
.start(
9398
withAPIKey: Configuration.storedAPIKey ?? "",
9499
sessionStrategy: .fixed(),
95-
configuration: Capture.Configuration(apiURL: apiURL),
100+
configuration: Capture.Configuration(
101+
apiURL: apiURL,
102+
issueCallbackConfiguration: issueCallbackConfiguration
103+
),
96104
fieldProviders: [CustomFieldProvider()]
97105
)?
98106
.enableIntegrations(
@@ -279,3 +287,18 @@ struct CustomNetworkResponseFieldProvider: URLSessionResponseFieldProvider {
279287
]
280288
}
281289
}
290+
291+
private final class CustomerIssueReportCallback: IssueReportCallback {
292+
func onBeforeReportSend(report: IssueReport) {
293+
Capture.Logger.logInfo(
294+
"Callback issue Report occurred \(report.details): \(report.reason)",
295+
fields: [
296+
"reportType": report.reportType,
297+
"session": report.sessionID,
298+
"details": report.details,
299+
"reason": report.reason,
300+
"fields": report.fields.description,
301+
]
302+
)
303+
}
304+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
// capture-sdk - bitdrift's client SDK
2+
// Copyright Bitdrift, Inc. All rights reserved.
3+
//
4+
// Use of this source code is governed by a source available license that can be found in the
5+
// LICENSE file or at:
6+
// https://polyformproject.org/wp-content/uploads/2020/06/PolyForm-Shield-1.0.0.txt
7+
8+
import Foundation
9+
10+
/// Metadata about a detected fatal issue.
11+
@objc(CAPIssueReport)
12+
public final class IssueReport: NSObject {
13+
/// The type of issue (for example, "ANR", "Native Crash", "Crash").
14+
@objc public let reportType: String
15+
16+
/// The primary error identifier (for example, exception class name or signal name).
17+
@objc public let reason: String
18+
19+
/// Additional details about the issue (for example, error message).
20+
@objc public let details: String
21+
22+
/// The bitdrift session ID of the session in which the issue occurred.
23+
@objc public let sessionID: String
24+
25+
/// Additional crash fields associated with this issue report.
26+
@objc public let fields: [String: String]
27+
28+
@objc
29+
public init(
30+
reportType: String,
31+
reason: String,
32+
details: String,
33+
sessionID: String,
34+
fields: [String: String]
35+
) {
36+
self.reportType = reportType
37+
self.reason = reason
38+
self.details = details
39+
self.sessionID = sessionID
40+
self.fields = fields
41+
}
42+
}
43+
44+
/// Callback invoked before an issue report (crash, ANR, etc.) is sent.
45+
@objc
46+
public protocol IssueReportCallback: AnyObject {
47+
/// Called before an issue report is sent.
48+
///
49+
/// - parameter report: The issue report metadata.
50+
func onBeforeReportSend(report: IssueReport)
51+
}
52+
53+
/// Configuration for issue report callbacks.
54+
///
55+
/// Use this to provide:
56+
/// - the queue where callbacks run
57+
/// - the callback that receives issue report metadata before send
58+
@objc(CAPIssueCallbackConfiguration)
59+
public final class IssueCallbackConfiguration: NSObject {
60+
private let callbackQueue: DispatchQueue
61+
private let issueReportCallback: IssueReportCallback
62+
63+
/// Creates issue callback configuration.
64+
///
65+
/// - parameter callbackQueue: Queue used to run callback invocations.
66+
/// - parameter issueReportCallback: Callback invoked before an issue report is sent.
67+
@objc
68+
public init(callbackQueue: DispatchQueue, issueReportCallback: IssueReportCallback) {
69+
self.callbackQueue = callbackQueue
70+
self.issueReportCallback = issueReportCallback
71+
}
72+
73+
// Called from Rust/FFI via Objective-C selector lookup. Keep selector stable.
74+
@objc(dispatch:)
75+
internal func dispatch(_ report: IssueReport) {
76+
callbackQueue.async { [issueReportCallback] in
77+
issueReportCallback.onBeforeReportSend(report: report)
78+
}
79+
}
80+
}

platform/swift/source/reports/IssueReportCallback.swift

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,35 @@
77

88
import Foundation
99

10+
/// Configuration for issue report callbacks.
11+
///
12+
/// Use this to provide:
13+
/// - the queue where callbacks run
14+
/// - the callback that receives issue report metadata before send
15+
@objc(CAPIssueCallbackConfiguration)
16+
public final class IssueCallbackConfiguration: NSObject {
17+
private let callbackQueue: DispatchQueue
18+
private let issueReportCallback: IssueReportCallback
19+
20+
/// Creates issue callback configuration.
21+
///
22+
/// - parameter callbackQueue: Queue used to run callback invocations.
23+
/// - parameter issueReportCallback: Callback invoked before an issue report is sent.
24+
@objc
25+
public init(callbackQueue: DispatchQueue, issueReportCallback: IssueReportCallback) {
26+
self.callbackQueue = callbackQueue
27+
self.issueReportCallback = issueReportCallback
28+
}
29+
30+
// Called from Rust/FFI via Objective-C selector lookup. Keep selector stable.
31+
@objc(dispatch:)
32+
internal func dispatch(_ report: IssueReport) {
33+
callbackQueue.async { [issueReportCallback] in
34+
issueReportCallback.onBeforeReportSend(report: report)
35+
}
36+
}
37+
}
38+
1039
/// Metadata about a detected fatal issue.
1140
@objc(CAPIssueReport)
1241
public final class IssueReport: NSObject {
@@ -49,32 +78,3 @@ public protocol IssueReportCallback: AnyObject {
4978
/// - parameter report: The issue report metadata.
5079
func onBeforeReportSend(report: IssueReport)
5180
}
52-
53-
/// Configuration for issue report callbacks.
54-
///
55-
/// Use this to provide:
56-
/// - the queue where callbacks run
57-
/// - the callback that receives issue report metadata before send
58-
@objc(CAPIssueCallbackConfiguration)
59-
public final class IssueCallbackConfiguration: NSObject {
60-
private let callbackQueue: DispatchQueue
61-
private let issueReportCallback: IssueReportCallback
62-
63-
/// Creates issue callback configuration.
64-
///
65-
/// - parameter callbackQueue: Queue used to run callback invocations.
66-
/// - parameter issueReportCallback: Callback invoked before an issue report is sent.
67-
@objc
68-
public init(callbackQueue: DispatchQueue, issueReportCallback: IssueReportCallback) {
69-
self.callbackQueue = callbackQueue
70-
self.issueReportCallback = issueReportCallback
71-
}
72-
73-
// Called from Rust/FFI via Objective-C selector lookup. Keep selector stable.
74-
@objc(dispatch:)
75-
internal func dispatch(_ report: IssueReport) {
76-
callbackQueue.async { [issueReportCallback] in
77-
issueReportCallback.onBeforeReportSend(report: report)
78-
}
79-
}
80-
}

0 commit comments

Comments
 (0)