diff --git a/strada/src/main/kotlin/dev/hotwire/strada/Bridge.kt b/strada/src/main/kotlin/dev/hotwire/strada/Bridge.kt index f1e77ba..e27d35b 100644 --- a/strada/src/main/kotlin/dev/hotwire/strada/Bridge.kt +++ b/strada/src/main/kotlin/dev/hotwire/strada/Bridge.kt @@ -47,7 +47,7 @@ class Bridge internal constructor(webView: WebView) { } internal fun replyWith(message: Message) { - logMessage("bridgeWillReplyWithMessage", message) + logEvent("bridgeWillReplyWithMessage", message.toString()) val internalMessage = InternalMessage.fromMessage(message) val javascript = generateJavaScript("replyWith", internalMessage.toJson().toJsonElement()) evaluate(javascript) diff --git a/strada/src/main/kotlin/dev/hotwire/strada/BridgeComponent.kt b/strada/src/main/kotlin/dev/hotwire/strada/BridgeComponent.kt index 8c097d5..6afc01e 100644 --- a/strada/src/main/kotlin/dev/hotwire/strada/BridgeComponent.kt +++ b/strada/src/main/kotlin/dev/hotwire/strada/BridgeComponent.kt @@ -63,7 +63,7 @@ abstract class BridgeComponent( */ fun replyTo(event: String): Boolean { val message = receivedMessageFor(event) ?: run { - logEvent("bridgeMessageFailedToReply", "message for event '$event' was not received") + logWarning("bridgeMessageFailedToReply", "message for event '$event' was not received") return false } @@ -79,7 +79,7 @@ abstract class BridgeComponent( */ fun replyTo(event: String, jsonData: String): Boolean { val message = receivedMessageFor(event) ?: run { - logEvent("bridgeMessageFailedToReply", "message for event '$event' was not received") + logWarning("bridgeMessageFailedToReply", "message for event '$event' was not received") return false } diff --git a/strada/src/main/kotlin/dev/hotwire/strada/BridgeDelegate.kt b/strada/src/main/kotlin/dev/hotwire/strada/BridgeDelegate.kt index 6d71ded..d25c07e 100644 --- a/strada/src/main/kotlin/dev/hotwire/strada/BridgeDelegate.kt +++ b/strada/src/main/kotlin/dev/hotwire/strada/BridgeDelegate.kt @@ -35,7 +35,7 @@ class BridgeDelegate( bridge?.load() } } else { - logEvent("bridgeNotInitializedForWebView", location) + logWarning("bridgeNotInitializedForWebView", location) } } @@ -46,7 +46,7 @@ class BridgeDelegate( fun replyWith(message: Message): Boolean { bridge?.replyWith(message) ?: run { - logEvent("bridgeMessageFailedToReply", "bridge is not available") + logWarning("bridgeMessageFailedToReply", "bridge is not available") return false } @@ -59,11 +59,11 @@ class BridgeDelegate( internal fun bridgeDidReceiveMessage(message: Message): Boolean { return if (destinationIsActive && location == message.metadata?.url) { - logMessage("bridgeDidReceiveMessage", message) + logEvent("bridgeDidReceiveMessage", message.toString()) getOrCreateComponent(message.component)?.didReceive(message) true } else { - logMessage("bridgeDidIgnoreMessage", message) + logWarning("bridgeDidIgnoreMessage", message.toString()) false } } diff --git a/strada/src/main/kotlin/dev/hotwire/strada/JsonExtensions.kt b/strada/src/main/kotlin/dev/hotwire/strada/JsonExtensions.kt index 930a07f..e70c50a 100644 --- a/strada/src/main/kotlin/dev/hotwire/strada/JsonExtensions.kt +++ b/strada/src/main/kotlin/dev/hotwire/strada/JsonExtensions.kt @@ -16,14 +16,14 @@ internal inline fun T.toJson() = json.encodeToString(this) internal inline fun JsonElement.decode(): T? = try { json.decodeFromJsonElement(this) } catch (e: Exception) { - StradaLog.e("jsonElementDecodeException: ${e.stackTraceToString()}") + logError("jsonElementDecodeException", e) null } internal inline fun String.decode(): T? = try { json.decodeFromString(this) } catch (e: Exception) { - StradaLog.e("jsonStringDecodeException: ${e.stackTraceToString()}") + logError("jsonStringDecodeException", e) null } diff --git a/strada/src/main/kotlin/dev/hotwire/strada/StradaJsonConverter.kt b/strada/src/main/kotlin/dev/hotwire/strada/StradaJsonConverter.kt index 8b67a6f..278dbb9 100644 --- a/strada/src/main/kotlin/dev/hotwire/strada/StradaJsonConverter.kt +++ b/strada/src/main/kotlin/dev/hotwire/strada/StradaJsonConverter.kt @@ -58,6 +58,6 @@ class KotlinXJsonConverter : StradaJsonConverter() { } fun logException(e: Exception) { - logEvent("kotlinXJsonConverterFailedWithError", e.toString()) + logError("kotlinXJsonConverterFailedWithError", e) } } diff --git a/strada/src/main/kotlin/dev/hotwire/strada/StradaLog.kt b/strada/src/main/kotlin/dev/hotwire/strada/StradaLog.kt index 1a6eda2..91c63d4 100644 --- a/strada/src/main/kotlin/dev/hotwire/strada/StradaLog.kt +++ b/strada/src/main/kotlin/dev/hotwire/strada/StradaLog.kt @@ -6,22 +6,31 @@ import android.util.Log internal object StradaLog { private const val DEFAULT_TAG = "StradaLog" - internal fun d(msg: String) = log(Log.DEBUG, DEFAULT_TAG, msg) + private val debugEnabled get() = Strada.config.debugLoggingEnabled - internal fun e(msg: String) = log(Log.ERROR, DEFAULT_TAG, msg) + internal fun d(msg: String) = log(Log.DEBUG, msg) - private fun log(logLevel: Int, tag: String, msg: String) { + internal fun w(msg: String) = log(Log.WARN, msg) + + internal fun e(msg: String) = log(Log.ERROR, msg) + + private fun log(logLevel: Int, msg: String) { when (logLevel) { - Log.DEBUG -> if (Strada.config.debugLoggingEnabled) Log.d(tag, msg) - Log.ERROR -> Log.e(tag, msg) + Log.DEBUG -> if (debugEnabled) Log.d(DEFAULT_TAG, msg) + Log.WARN -> Log.w(DEFAULT_TAG, msg) + Log.ERROR -> Log.e(DEFAULT_TAG, msg) } } } -internal fun logMessage(event: String, message: Message) { - logEvent(event, message.toString()) -} - internal fun logEvent(event: String, details: String = "") { StradaLog.d("$event ".padEnd(35, '.') + " [$details]") } + +internal fun logWarning(event: String, details: String) { + StradaLog.w("$event ".padEnd(35, '.') + " [$details]") +} + +internal fun logError(event: String, error: Exception) { + StradaLog.e("$event: ${error.stackTraceToString()}") +}