Skip to content

Commit 6b4ca38

Browse files
authored
Add ability to set logging identifier (#160)
1 parent f4b9302 commit 6b4ca38

12 files changed

Lines changed: 56 additions & 19 deletions

File tree

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,23 @@ val peripheral = scope.peripheral(advertisement) {
155155

156156
_I/O data is only shown in logs when logging `level` is set to `Data`._
157157

158+
When logging, the identity of the peripheral is prefixed on log messages to differentiate messages when multiple
159+
peripherals are logging. The identifier (for the purposes of logging) can be set via the `identifier` property:
160+
161+
```kotlin
162+
val peripheral = scope.peripheral(advertisement) {
163+
logging {
164+
identifier = "Example"
165+
}
166+
}
167+
```
168+
169+
The default (when not specified, or set to `null`) is to use the platform specific peripheral identifier:
170+
171+
- Android: Hardware (MAC) address (e.g. "00:11:22:AA:BB:CC")
172+
- Apple: The UUID associated with the peer
173+
- JavaScript: A `DOMString` that uniquely identifies a device
174+
158175
#### Service Discovery
159176

160177
All platforms support an `onServicesDiscovered` action (that is executed after service discovery but before observations

core/api/core.api

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,10 +399,12 @@ public final class com/juul/kable/logs/Logging {
399399
public final fun getData ()Lcom/juul/kable/logs/Logging$DataProcessor;
400400
public final fun getEngine ()Lcom/juul/kable/logs/LogEngine;
401401
public final fun getFormat ()Lcom/juul/kable/logs/Logging$Format;
402+
public final fun getIdentifier ()Ljava/lang/String;
402403
public final fun getLevel ()Lcom/juul/kable/logs/Logging$Level;
403404
public final fun setData (Lcom/juul/kable/logs/Logging$DataProcessor;)V
404405
public final fun setEngine (Lcom/juul/kable/logs/LogEngine;)V
405406
public final fun setFormat (Lcom/juul/kable/logs/Logging$Format;)V
407+
public final fun setIdentifier (Ljava/lang/String;)V
406408
public final fun setLevel (Lcom/juul/kable/logs/Logging$Level;)V
407409
}
408410

core/src/androidMain/kotlin/Observers.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ internal class Observers(
5353
logging: Logging,
5454
) {
5555

56-
private val logger = Logger(logging, tag = "Kable/Observers")
56+
private val logger = Logger(logging, tag = "Kable/Observers", peripheral.bluetoothDevice.address)
5757

5858
val characteristicChanges = MutableSharedFlow<AndroidObservationEvent>()
5959
private val observations = Observations()

core/src/androidMain/kotlin/Peripheral.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,14 +125,14 @@ public enum class Priority { Low, Balanced, High }
125125

126126
public class AndroidPeripheral internal constructor(
127127
parentCoroutineContext: CoroutineContext,
128-
private val bluetoothDevice: BluetoothDevice,
128+
internal val bluetoothDevice: BluetoothDevice,
129129
private val transport: Transport,
130130
private val phy: Phy,
131131
private val onServicesDiscovered: ServicesDiscoveredAction,
132132
private val logging: Logging,
133133
) : Peripheral {
134134

135-
private val logger = Logger(logging, tag = "Kable/Peripheral", prefix = "$bluetoothDevice ")
135+
private val logger = Logger(logging, tag = "Kable/Peripheral", identifier = bluetoothDevice.address)
136136

137137
private val _state = MutableStateFlow<State>(State.Disconnected())
138138
public override val state: Flow<State> = _state.asStateFlow()

core/src/androidMain/kotlin/Scanner.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public class AndroidScanner internal constructor(
2626
logging: Logging,
2727
) : Scanner {
2828

29-
private val logger = Logger(logging, tag = "Kable/Scanner")
29+
private val logger = Logger(logging, tag = "Kable/Scanner", identifier = null)
3030

3131
private val bluetoothAdapter = BluetoothAdapter.getDefaultAdapter()
3232
?: error("Bluetooth not supported")

core/src/androidMain/kotlin/gatt/Callback.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ internal class Callback(
5858
macAddress: String,
5959
) : BluetoothGattCallback() {
6060

61-
private val logger = Logger(logging, tag = "Kable/Callback", prefix = "$macAddress ")
61+
private val logger = Logger(logging, tag = "Kable/Callback", identifier = macAddress)
6262

6363
private var disconnectedAction: DisconnectedAction? = null
6464
fun invokeOnDisconnected(action: DisconnectedAction) {

core/src/appleMain/kotlin/Peripheral.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public class ApplePeripheral internal constructor(
9797

9898
private val centralManager: CentralManager = CentralManager.Default
9999

100-
private val logger = Logger(logging, prefix = "${cbPeripheral.identifier} ")
100+
private val logger = Logger(logging, identifier = cbPeripheral.identifier.UUIDString)
101101

102102
private val _state = MutableStateFlow<State>(State.Disconnected())
103103
override val state: Flow<State> = _state.asStateFlow()
@@ -151,7 +151,9 @@ public class ApplePeripheral internal constructor(
151151
}.launchIn(scope)
152152

153153
try {
154-
val delegate = PeripheralDelegate(logging).freeze() // todo: Create in `connectPeripheral`.
154+
// todo: Create in `connectPeripheral`.
155+
val delegate = PeripheralDelegate(logging, cbPeripheral.identifier.UUIDString).freeze()
156+
155157
val connection = centralManager.connectPeripheral(cbPeripheral, delegate).also {
156158
_connection.value = it
157159
}

core/src/appleMain/kotlin/PeripheralDelegate.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@ import platform.darwin.NSObject
3030
import kotlin.native.concurrent.freeze
3131

3232
// https://developer.apple.com/documentation/corebluetooth/cbperipheraldelegate
33-
internal class PeripheralDelegate(logging: Logging) : NSObject(), CBPeripheralDelegateProtocol {
33+
internal class PeripheralDelegate(
34+
logging: Logging,
35+
identifier: String
36+
) : NSObject(), CBPeripheralDelegateProtocol {
3437

3538
sealed class Response {
3639

@@ -100,7 +103,7 @@ internal class PeripheralDelegate(logging: Logging) : NSObject(), CBPeripheralDe
100103
private val _characteristicChanges = MutableSharedFlow<DidUpdateValueForCharacteristic>(extraBufferCapacity = 64)
101104
val characteristicChanges = _characteristicChanges.asSharedFlow()
102105

103-
private val logger = Logger(logging)
106+
private val logger = Logger(logging, tag = "Kable/Delegate", identifier = identifier)
104107

105108
/* Discovering Services */
106109

core/src/commonMain/kotlin/logs/LogMessage.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,12 @@ internal class LogMessage {
3030
detail(key, value.toString())
3131
}
3232

33-
fun build(logging: Logging, prefix: String?): String = buildString {
34-
if (prefix != null) append(prefix)
33+
fun build(logging: Logging, platformIdentifier: String?): String = buildString {
34+
val prefix = logging.identifier ?: platformIdentifier
35+
if (!prefix.isNullOrEmpty()) {
36+
append(prefix)
37+
append(' ')
38+
}
3539
append(message)
3640

3741
when (logging.format) {

core/src/commonMain/kotlin/logs/Logger.kt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,48 +6,48 @@ import com.juul.kable.logs.Logging.Level.Events
66
internal class Logger(
77
private val logging: Logging,
88
private val tag: String = "Kable",
9-
private val prefix: String? = null,
9+
private val identifier: String?,
1010
) {
1111

1212
inline fun verbose(throwable: Throwable? = null, crossinline init: LogMessage.() -> Unit) {
1313
if (logging.level == Events || logging.level == Data) {
1414
val message = LogMessage()
1515
message.init()
16-
logging.engine.verbose(throwable, tag, message.build(logging, prefix))
16+
logging.engine.verbose(throwable, tag, message.build(logging, identifier))
1717
}
1818
}
1919

2020
inline fun debug(throwable: Throwable? = null, crossinline init: LogMessage.() -> Unit) {
2121
if (logging.level == Events || logging.level == Data) {
2222
val message = LogMessage()
2323
message.init()
24-
logging.engine.debug(throwable, tag, message.build(logging, prefix))
24+
logging.engine.debug(throwable, tag, message.build(logging, identifier))
2525
}
2626
}
2727

2828
inline fun info(throwable: Throwable? = null, crossinline init: LogMessage.() -> Unit) {
2929
if (logging.level == Events || logging.level == Data) {
3030
val message = LogMessage()
3131
message.init()
32-
logging.engine.info(throwable, tag, message.build(logging, prefix))
32+
logging.engine.info(throwable, tag, message.build(logging, identifier))
3333
}
3434
}
3535

3636
inline fun warn(throwable: Throwable? = null, crossinline init: LogMessage.() -> Unit) {
3737
val message = LogMessage()
3838
message.init()
39-
logging.engine.warn(throwable, tag, message.build(logging, prefix))
39+
logging.engine.warn(throwable, tag, message.build(logging, identifier))
4040
}
4141

4242
inline fun error(throwable: Throwable? = null, crossinline init: LogMessage.() -> Unit) {
4343
val message = LogMessage()
4444
message.init()
45-
logging.engine.error(throwable, tag, message.build(logging, prefix))
45+
logging.engine.error(throwable, tag, message.build(logging, identifier))
4646
}
4747

4848
inline fun assert(throwable: Throwable? = null, crossinline init: LogMessage.() -> Unit) {
4949
val message = LogMessage()
5050
message.init()
51-
logging.engine.assert(throwable, tag, message.build(logging, prefix))
51+
logging.engine.assert(throwable, tag, message.build(logging, identifier))
5252
}
5353
}

0 commit comments

Comments
 (0)