Skip to content

Commit

Permalink
Provide service, characteristic and/or descriptor UUIDs to data proce…
Browse files Browse the repository at this point in the history
…ssor
  • Loading branch information
twyatt committed Jul 26, 2023
1 parent 6406fe8 commit da0b17a
Show file tree
Hide file tree
Showing 6 changed files with 322 additions and 61 deletions.
2 changes: 1 addition & 1 deletion core/api/core.api
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,7 @@ public final class com/juul/kable/logs/Logging {
}

public abstract interface class com/juul/kable/logs/Logging$DataProcessor {
public abstract fun process ([B)Ljava/lang/String;
public abstract fun process ([BLjava/util/UUID;Ljava/util/UUID;Ljava/util/UUID;)Ljava/lang/String;
}

public final class com/juul/kable/logs/Logging$Format : java/lang/Enum {
Expand Down
2 changes: 1 addition & 1 deletion core/src/commonMain/kotlin/logs/Hex.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class HexBuilder internal constructor() {

public fun Hex(init: HexBuilder.() -> Unit = {}): Logging.DataProcessor {
val config = HexBuilder().apply(init)
return Logging.DataProcessor { data ->
return Logging.DataProcessor { data, _, _, _ ->
data.toHexString(separator = config.separator, lowerCase = config.lowerCase)
}
}
110 changes: 64 additions & 46 deletions core/src/commonMain/kotlin/logs/LogMessage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,45 @@ import com.juul.kable.Characteristic
import com.juul.kable.Descriptor
import com.juul.kable.Service
import com.juul.kable.WriteType
import com.juul.kable.logs.Logging.Format.Compact
import com.juul.kable.logs.Logging.Format.Multiline

internal expect val LOG_INDENT: String?

internal class LogMessage {
internal class LogMessage(
private val logging: Logging,
platformIdentifier: String?,
private val indent: String? = LOG_INDENT,
) {

var message: String = ""
private val prefix = logging.identifier ?: platformIdentifier

var message: String = ""
private var serviceUuid: Uuid? = null
private var characteristicUuid: Uuid? = null
private var descriptorUuid: Uuid? = null
private val details = mutableListOf<Pair<String, Any>>()
var data: ByteArray? = null

fun detail(key: String, value: String) {
details += key to value
fun detail(service: Service) {
serviceUuid = service.serviceUuid
characteristicUuid = null
descriptorUuid = null
}

fun detail(characteristic: Characteristic) {
serviceUuid = characteristic.serviceUuid
characteristicUuid = characteristic.characteristicUuid
descriptorUuid = null
}

fun detail(descriptor: Descriptor) {
serviceUuid = descriptor.serviceUuid
characteristicUuid = descriptor.characteristicUuid
descriptorUuid = descriptor.descriptorUuid
}

fun detail(key: String, value: ByteArray) {
fun detail(key: String, value: String) {
details += key to value
}

Expand All @@ -30,62 +55,55 @@ internal class LogMessage {
detail(key, value.toString())
}

fun build(logging: Logging, platformIdentifier: String?): String = buildString {
val prefix = logging.identifier ?: platformIdentifier
private var isFirst = true

private fun StringBuilder.append(label: String, value: Any) {
when (logging.format) {
Compact -> if (isFirst) append('(') else append(", ")
Multiline -> {
appendLine()
if (indent != null) append(indent)
}
}
isFirst = false

append(label)
when (logging.format) {
Compact -> append("=")
Multiline -> append(": ")
}
append(value)
}

fun build(): String = buildString {
if (!prefix.isNullOrEmpty()) {
append(prefix)
append(' ')
}
append(message)

when (logging.format) {
Logging.Format.Compact -> if (details.isNotEmpty()) append('(')
Logging.Format.Multiline -> appendLine()
isFirst = true

serviceUuid?.let { append("service", it) }
characteristicUuid?.let { append("characteristic", it) }
descriptorUuid?.let { append("descriptor", it) }

details.forEach { (key, value) ->
append(key, value)
}

details.forEachIndexed { index, detail ->
val (key, value) = detail

if (value !is ByteArray || logging.level == Logging.Level.Data) {
if (index > 0) {
when (logging.format) {
Logging.Format.Compact -> append(", ")
Logging.Format.Multiline -> appendLine()
}
}

if (logging.format == Logging.Format.Multiline && LOG_INDENT != null) append(LOG_INDENT)

append(key)
when (logging.format) {
Logging.Format.Compact -> append("=")
Logging.Format.Multiline -> append(": ")
}
if (value is ByteArray) append(logging.data.process(value)) else append(value)
if (logging.level == Logging.Level.Data) {
data?.let {
append("data", logging.data.process(it, serviceUuid, characteristicUuid, descriptorUuid))
}
}

if (logging.format == Logging.Format.Compact && details.isNotEmpty()) append(')')
if (logging.format == Compact && !isFirst) append(')')
}
}

internal fun LogMessage.detail(data: ByteArray?) {
if (data != null) detail("data", data)
}

internal fun LogMessage.detail(service: Service) {
detail("service", service.serviceUuid)
}

internal fun LogMessage.detail(characteristic: Characteristic) {
detail("service", characteristic.serviceUuid)
detail("characteristic", characteristic.characteristicUuid)
}

internal fun LogMessage.detail(descriptor: Descriptor) {
detail("service", descriptor.serviceUuid)
detail("characteristic", descriptor.characteristicUuid)
detail("descriptor", descriptor.descriptorUuid)
if (data != null) this@detail.data = data
}

internal fun LogMessage.detail(writeType: WriteType) {
Expand Down
24 changes: 12 additions & 12 deletions core/src/commonMain/kotlin/logs/Logger.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,43 +11,43 @@ internal class Logger(

inline fun verbose(throwable: Throwable? = null, crossinline init: LogMessage.() -> Unit) {
if (logging.level == Events || logging.level == Data) {
val message = LogMessage()
val message = LogMessage(logging, identifier)
message.init()
logging.engine.verbose(throwable, tag, message.build(logging, identifier))
logging.engine.verbose(throwable, tag, message.build())
}
}

inline fun debug(throwable: Throwable? = null, crossinline init: LogMessage.() -> Unit) {
if (logging.level == Events || logging.level == Data) {
val message = LogMessage()
val message = LogMessage(logging, identifier)
message.init()
logging.engine.debug(throwable, tag, message.build(logging, identifier))
logging.engine.debug(throwable, tag, message.build())
}
}

inline fun info(throwable: Throwable? = null, crossinline init: LogMessage.() -> Unit) {
if (logging.level == Events || logging.level == Data) {
val message = LogMessage()
val message = LogMessage(logging, identifier)
message.init()
logging.engine.info(throwable, tag, message.build(logging, identifier))
logging.engine.info(throwable, tag, message.build())
}
}

inline fun warn(throwable: Throwable? = null, crossinline init: LogMessage.() -> Unit) {
val message = LogMessage()
val message = LogMessage(logging, identifier)
message.init()
logging.engine.warn(throwable, tag, message.build(logging, identifier))
logging.engine.warn(throwable, tag, message.build())
}

inline fun error(throwable: Throwable? = null, crossinline init: LogMessage.() -> Unit) {
val message = LogMessage()
val message = LogMessage(logging, identifier)
message.init()
logging.engine.error(throwable, tag, message.build(logging, identifier))
logging.engine.error(throwable, tag, message.build())
}

inline fun assert(throwable: Throwable? = null, crossinline init: LogMessage.() -> Unit) {
val message = LogMessage()
val message = LogMessage(logging, identifier)
message.init()
logging.engine.assert(throwable, tag, message.build(logging, identifier))
logging.engine.assert(throwable, tag, message.build())
}
}
9 changes: 8 additions & 1 deletion core/src/commonMain/kotlin/logs/Logging.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.juul.kable.logs

import com.benasher44.uuid.Uuid

internal typealias LoggingBuilder = Logging.() -> Unit

public class Logging {
Expand Down Expand Up @@ -41,7 +43,12 @@ public class Logging {
}

public fun interface DataProcessor {
public fun process(data: ByteArray): String
public fun process(
data: ByteArray,
serviceUuid: Uuid?,
characteristicUuid: Uuid?,
descriptorUuid: Uuid?,
): String
}

/**
Expand Down
Loading

0 comments on commit da0b17a

Please sign in to comment.