Skip to content

Commit 6e2348c

Browse files
Implement a single API for configuration
1 parent 63a5776 commit 6e2348c

21 files changed

Lines changed: 159 additions & 165 deletions

File tree

‎.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,4 @@ buildboth.sh
7373
Pods
7474
.build/
7575
Package.resolved
76+
.swiftpm/

‎Sources/CrashKiOSBugsnag/BugsnagSink.swift‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,13 @@ public func configureBugsnagForKotlin(_ config: BugsnagConfiguration) {
2626
/// Reference `CrashKiOSBugsnagSink` implementation.
2727
///
2828
/// Start Bugsnag through this class so the configuration is prepared for Kotlin
29-
/// crash handling, then register the returned sink with the Kotlin side:
29+
/// crash handling, then configure CrashKiOS with the returned sink:
3030
/// ```swift
3131
/// let sink = BugsnagSink.start(BugsnagConfiguration.loadConfig())
32-
/// BugsnagKt.registerBugsnagSink(sink: sink)
32+
/// CrashKiOS.shared.configure(crashReporting: BugsnagCrashReporting(sink: sink))
3333
/// ```
34-
/// (Add `export("co.touchlab.crashkios:bugsnag")` to the Kotlin framework for clean names.)
34+
/// (Add `export("co.touchlab.crashkios:bugsnag")` AND `export("co.touchlab.crashkios:core")`
35+
/// to the Kotlin framework for clean names.)
3536
public final class BugsnagSink: NSObject, CrashKiOSBugsnagSink {
3637

3738
/// Configures `config` via `configureBugsnagForKotlin(_:)`, starts Bugsnag,

‎Sources/CrashKiOSCrashlytics/CrashlyticsSink.swift‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ import FirebaseCrashlytics
44

55
/// Reference `CrashKiOSCrashlyticsSink` implementation.
66
///
7-
/// Register from your `AppDelegate`, right after Firebase is configured and before
7+
/// Configure from your `AppDelegate`, right after Firebase is configured and before
88
/// any Kotlin code runs:
99
/// ```swift
1010
/// FirebaseApp.configure()
11-
/// CrashlyticsKt.registerCrashlyticsSink(sink: CrashlyticsSink())
11+
/// CrashKiOS.shared.configure(crashReporting: CrashlyticsCrashReporting(sink: CrashlyticsSink()))
1212
/// ```
13-
/// (The Kotlin function name depends on your framework's export configuration; add
14-
/// `export("co.touchlab.crashkios:crashlytics")` to the framework for clean names.)
13+
/// (Add `export("co.touchlab.crashkios:crashlytics")` AND `export("co.touchlab.crashkios:core")`
14+
/// to the framework for clean names.)
1515
public final class CrashlyticsSink: NSObject, CrashKiOSCrashlyticsSink {
1616

1717
private let crashlytics: Crashlytics

‎bugsnag/build.gradle.kts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ kotlin {
5555
sourceSets {
5656
commonMain {
5757
dependencies {
58-
implementation(project(":core"))
58+
api(project(":core"))
5959
}
6060
}
6161
commonTest {

‎bugsnag/src/appleMain/kotlin/co/touchlab/crashkios/bugsnag/Bugsnag.kt‎

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,37 +3,35 @@
33
package co.touchlab.crashkios.bugsnag
44

55
import co.touchlab.crashkios.bugsnag.objc.CrashKiOSBugsnagSinkProtocol
6+
import co.touchlab.crashkios.core.CrashReportingImplementation
67
import kotlinx.cinterop.ExperimentalForeignApi
78

89
/**
9-
* Registers the Swift-implemented [sink] that forwards CrashKiOS events to Bugsnag,
10-
* and installs the unhandled-exception hook so unhandled Kotlin exceptions are
11-
* reported as fatal crashes.
10+
* The Bugsnag backend for [co.touchlab.crashkios.core.CrashKiOS.configure]. Wraps the
11+
* Swift-implemented [sink] that forwards CrashKiOS events to Bugsnag.
1212
*
1313
* Call once, from Swift, at startup. Start Bugsnag through the shipped sink so the
1414
* configuration suppresses the duplicate termination crash:
1515
* ```swift
1616
* let sink = BugsnagSink.start(BugsnagConfiguration.loadConfig())
17-
* BugsnagKt.registerBugsnagSink(sink: sink)
17+
* CrashKiOS.shared.configure(crashReporting: BugsnagCrashReporting(sink: sink))
1818
* ```
1919
* `BugsnagSink` ships in the `CrashKiOSBugsnag` Swift package (this repo's
2020
* `Package.swift`); custom sink implementations must call
2121
* `configureBugsnagForKotlin(config)` (from the same package) before `Bugsnag.start`
22-
* to keep the duplicate-crash suppression. Add
23-
* `export("co.touchlab.crashkios:bugsnag")` to your framework configuration for
22+
* to keep the duplicate-crash suppression. Add `export("co.touchlab.crashkios:bugsnag")`
23+
* AND `export("co.touchlab.crashkios:core")` to your framework configuration for
2424
* clean unmangled names.
2525
*
26-
* Replaces `startBugsnag()` / `configureBugsnag()` / `setBugsnagUnhandledExceptionHook()`,
27-
* which were removed along with the Bugsnag cinterop — Bugsnag start and configuration
28-
* now live in Swift.
29-
*
3026
* Calling `enableBugsnag()` (or constructing `BugsnagCallsActual`) on an Apple target
31-
* WITHOUT having registered a sink fails fast with a descriptive error rather than
27+
* WITHOUT having configured a sink fails fast with a descriptive error rather than
3228
* silently dropping crash reports.
3329
*/
34-
public fun registerBugsnagSink(sink: CrashKiOSBugsnagSinkProtocol) {
35-
bugsnagRegistry.register(sink, fatalHook(sink))
36-
BugsnagKotlin.implementation = BugsnagCallsActual()
30+
public class BugsnagCrashReporting(private val sink: CrashKiOSBugsnagSinkProtocol) : CrashReportingImplementation {
31+
override fun install() {
32+
bugsnagRegistry.register(sink, fatalHook(sink))
33+
BugsnagKotlin.implementation = BugsnagCallsActual()
34+
}
3735
}
3836

3937
/**

‎bugsnag/src/appleTest/kotlin/co/touchlab/crashkios/bugsnag/BugsnagSinkTest.kt‎

Lines changed: 36 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -3,57 +3,19 @@
33
package co.touchlab.crashkios.bugsnag
44

55
import co.touchlab.crashkios.bugsnag.objc.CrashKiOSBugsnagSinkProtocol
6+
import co.touchlab.crashkios.core.CrashKiOS
67
import kotlin.test.Test
78
import kotlin.test.assertEquals
89
import kotlin.test.assertTrue
910
import kotlinx.cinterop.ExperimentalForeignApi
1011
import platform.Foundation.NSException
1112
import platform.darwin.NSObject
1213

13-
// Kotlin stand-in for the Swift sink: exercises the same ObjC-protocol boundary and
14-
// proves the module links with zero linker flags (the old -U / RND-91 failure mode).
15-
private class FakeBugsnagSink :
16-
NSObject(),
17-
CrashKiOSBugsnagSinkProtocol {
18-
val breadcrumbs = mutableListOf<String>()
19-
val notifications = mutableListOf<Pair<List<*>, Boolean>>()
20-
val metadata = mutableListOf<Triple<Any?, String, String>>()
21-
var fatalMarks = 0
22-
23-
// Call order across notify/mark — proves the hook sequences them correctly,
24-
// not just that both eventually happened.
25-
val events = mutableListOf<String>()
26-
27-
override fun leaveBreadcrumb(message: String) {
28-
breadcrumbs += message
29-
}
30-
31-
override fun markFatalCrashRecorded() {
32-
fatalMarks++
33-
events += "mark"
34-
}
35-
36-
override fun notifyWithExceptions(exceptions: List<*>, handled: Boolean) {
37-
notifications += exceptions to handled
38-
events += "notify"
39-
}
40-
41-
override fun addMetadata(value: Any, key: String, section: String) {
42-
metadata += Triple(value, key, section)
43-
}
44-
}
45-
4614
class BugsnagSinkTest {
47-
// registerBugsnagSink() mutates the module-level `bugsnagRegistry` singleton. Safe
48-
// to call from every test that needs BugsnagKotlin's facade routed to its own sink
49-
// (sinkRef always overwrites) — NOT safe to rely on for the real unhandled-exception
50-
// hook actually firing with a given test's sink: hook-install is a once-ever guard,
51-
// permanently bound to whichever sink registered first. That's why fatalHookNotifiesThenMarksSession
52-
// below calls fatalHook() directly instead of triggering the real hook.
5315
@Test
5416
fun facadeForwardsToRegisteredSink() {
5517
val sink = FakeBugsnagSink()
56-
registerBugsnagSink(sink)
18+
CrashKiOS.configure(BugsnagCrashReporting(sink))
5719

5820
BugsnagKotlin.logMessage("crumb")
5921
BugsnagKotlin.setCustomValue("section1", "key1", "value1")
@@ -78,31 +40,51 @@ class BugsnagSinkTest {
7840
assertEquals(2, fatalExceptions.size, "fatal should carry the cause chain")
7941
assertEquals("kotlin.IllegalStateException", (fatalExceptions[1] as NSException).name)
8042

81-
// Only the terminating unhandled-exception hook may mark the session —
82-
// a direct sendFatalException() must not (it would poison the OnSendError filter).
43+
// Only the terminating unhandled-exception hook may mark the session,
44+
// but a direct sendFatalException() must not (it would poison the OnSendError filter).
8345
assertEquals(0, sink.fatalMarks)
8446
}
8547

8648
@Test
8749
fun fatalHookNotifiesThenMarksSession() {
88-
// fatalHook() is the terminating hook's actual body (see Bugsnag.kt), exercised
89-
// directly rather than through wrapUnhandledExceptionHook — invoking the real
90-
// hook would terminate the process.
9150
val sink = FakeBugsnagSink()
92-
// Rebinds BugsnagKotlin.implementation to this sink (register() always
93-
// overwrites sinkRef) so fatalHook's sendFatalException() call routes here
94-
// rather than to whatever sink an earlier test left registered. Does NOT wire
95-
// this sink into the real unhandled-exception hook — that binds once, to
96-
// whichever sink registers first — which is exactly why fatalHook is called
97-
// directly below instead of relying on the real hook firing.
98-
registerBugsnagSink(sink)
51+
CrashKiOS.configure(BugsnagCrashReporting(sink))
9952

10053
fatalHook(sink)(RuntimeException("terminating boom"))
10154

10255
assertEquals(1, sink.fatalMarks)
10356
assertEquals(1, sink.notifications.size)
104-
// Order matters: markFatalCrashRecorded() must run AFTER notify, never before —
105-
// marking first would let the OnSendError filter drop a report that was never sent.
57+
// Order matters: markFatalCrashRecorded() must run AFTER notify, never before.
58+
// Marking first would let the OnSendError filter drop a report that was never sent.
10659
assertEquals(listOf("notify", "mark"), sink.events)
10760
}
10861
}
62+
63+
private class FakeBugsnagSink :
64+
NSObject(),
65+
CrashKiOSBugsnagSinkProtocol {
66+
val breadcrumbs = mutableListOf<String>()
67+
val notifications = mutableListOf<Pair<List<*>, Boolean>>()
68+
val metadata = mutableListOf<Triple<Any?, String, String>>()
69+
var fatalMarks = 0
70+
71+
val events = mutableListOf<String>()
72+
73+
override fun leaveBreadcrumb(message: String) {
74+
breadcrumbs += message
75+
}
76+
77+
override fun markFatalCrashRecorded() {
78+
fatalMarks++
79+
events += "mark"
80+
}
81+
82+
override fun notifyWithExceptions(exceptions: List<*>, handled: Boolean) {
83+
notifications += exceptions to handled
84+
events += "notify"
85+
}
86+
87+
override fun addMetadata(value: Any, key: String, section: String) {
88+
metadata += Triple(value, key, section)
89+
}
90+
}

‎bugsnag/src/commonMain/kotlin/co/touchlab/crashkios/bugsnag/BugsnagKotlin.kt‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,10 @@ object BugsnagKotlin {
2525
/**
2626
* Call in startup code on Android. Tests should generally skip this.
2727
*
28-
* On Apple targets this alone is NOT enough: a Swift sink must be registered via
29-
* `registerBugsnagSink()` (which also sets the implementation, making this call
30-
* redundant there) — otherwise events are dropped with an NSLog warning.
28+
* On Apple targets this alone is NOT enough: a Swift sink must be configured via
29+
* `CrashKiOS.configure(BugsnagCrashReporting(sink))` (which also sets the
30+
* implementation, making this call redundant there) — otherwise events are dropped
31+
* with an NSLog warning.
3132
*/
3233
fun enableBugsnag() {
3334
BugsnagKotlin.implementation = BugsnagCallsActual()
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package co.touchlab.crashkios.core
2+
3+
/**
4+
* A crash-reporting backend that knows how to wire itself into CrashKiOS. Each SDK
5+
* module (bugsnag, crashlytics, ...) ships one implementation, constructed with its
6+
* Swift-side sink — see `BugsnagCrashReporting`/`CrashlyticsCrashReporting`.
7+
*/
8+
public fun interface CrashReportingImplementation {
9+
public fun install()
10+
}
11+
12+
/**
13+
* Single configuration entry point for CrashKiOS. Which backend you get is a choice
14+
* of [CrashReportingImplementation] value passed in, not a choice of which function
15+
* you call — call this the same way regardless of backend. Kotlin `object`s bridge to
16+
* Swift as a `shared` singleton:
17+
* ```swift
18+
* CrashKiOS.shared.configure(crashReporting: BugsnagCrashReporting(sink: sink))
19+
* ```
20+
* Requires `export("co.touchlab.crashkios:core")` (in addition to exporting the
21+
* backend module) in your framework configuration for clean, unmangled Swift names.
22+
*/
23+
public object CrashKiOS {
24+
public fun configure(crashReporting: CrashReportingImplementation) {
25+
crashReporting.install()
26+
}
27+
}

‎core/src/appleMain/kotlin/co/touchlab/crashkios/core/CrashSinkRegistry.kt‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ public class CrashSinkRegistry<T : Any>(private val name: String) {
1919
* than crashing at startup with a clear message.
2020
*/
2121
public fun requireSink(): T = sinkRef.value ?: error(
22-
"CrashKiOS: no $name sink registered. Call register${name}Sink() from Swift at " +
23-
"startup (after the $name SDK is initialized) before enabling CrashKiOS from Kotlin.",
22+
"CrashKiOS: no $name sink registered. Call CrashKiOS.configure(...) with a $name " +
23+
"implementation from Swift at startup (after the $name SDK is initialized) " +
24+
"before enabling CrashKiOS from Kotlin.",
2425
)
2526

2627
public fun register(sink: T, fatalHook: (Throwable) -> Unit) {

‎core/src/appleTest/kotlin/co/touchlab/crashkios/core/ThrowableNSExceptionTest.kt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,6 @@ class ThrowableNSExceptionTest {
3939
fun requireSinkFailsLoudWhenUnregistered() {
4040
val registry = CrashSinkRegistry<Any>("Crashlytics")
4141
val error = assertFailsWith<IllegalStateException> { registry.requireSink() }
42-
assertEquals(true, error.message?.contains("registerCrashlyticsSink"))
42+
assertEquals(true, error.message?.contains("CrashKiOS.configure"))
4343
}
4444
}

0 commit comments

Comments
 (0)