feat: add throwable support to Log.d/i and inline lambda overloads

- Add optional throwable parameter to d() and i() across all platforms
- Add inline lambda overloads for all log levels to defer message
  construction, avoiding string allocation when level is filtered
- Restore VoiceMessagePreview to pass throwable directly

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
davotoula
2026-03-28 11:19:06 +01:00
parent 250d89ca0a
commit 541b197a64
6 changed files with 71 additions and 9 deletions
@@ -275,7 +275,7 @@ private fun ManageMediaPlayer(
try { try {
player?.stop() player?.stop()
} catch (e: IllegalStateException) { } catch (e: IllegalStateException) {
Log.d("VoiceMessagePreview", "MediaPlayer stop failed (already stopped): ${e.message}") Log.d("VoiceMessagePreview", "MediaPlayer stop failed (already stopped)", e)
} }
player?.release() player?.release()
onPlayerChanged(null) onPlayerChanged(null)
@@ -48,14 +48,24 @@ actual object PlatformLog {
actual fun d( actual fun d(
tag: String, tag: String,
message: String, message: String,
throwable: Throwable?,
) { ) {
android.util.Log.d(tag, message) if (throwable != null) {
android.util.Log.d(tag, message, throwable)
} else {
android.util.Log.d(tag, message)
}
} }
actual fun i( actual fun i(
tag: String, tag: String,
message: String, message: String,
throwable: Throwable?,
) { ) {
android.util.Log.i(tag, message) if (throwable != null) {
android.util.Log.i(tag, message, throwable)
} else {
android.util.Log.i(tag, message)
}
} }
} }
@@ -50,14 +50,24 @@ actual object PlatformLog {
actual fun d( actual fun d(
tag: String, tag: String,
message: String, message: String,
throwable: Throwable?,
) { ) {
NSLog("DEBUG: [$tag] $message") if (throwable != null) {
NSLog("DEBUG: [$tag] $message. Throwable: $throwable CAUSE ${throwable.cause}")
} else {
NSLog("DEBUG: [$tag] $message")
}
} }
actual fun i( actual fun i(
tag: String, tag: String,
message: String, message: String,
throwable: Throwable?,
) { ) {
NSLog("INFO: [$tag] $message") if (throwable != null) {
NSLog("INFO: [$tag] $message. Throwable: $throwable CAUSE ${throwable.cause}")
} else {
NSLog("INFO: [$tag] $message")
}
} }
} }
@@ -26,15 +26,31 @@ object Log {
fun d( fun d(
tag: String, tag: String,
message: String, message: String,
throwable: Throwable? = null,
) { ) {
if (minLevel <= LogLevel.DEBUG) PlatformLog.d(tag, message) if (minLevel <= LogLevel.DEBUG) PlatformLog.d(tag, message, throwable)
}
inline fun d(
tag: String,
message: () -> String,
) {
if (minLevel <= LogLevel.DEBUG) PlatformLog.d(tag, message())
} }
fun i( fun i(
tag: String, tag: String,
message: String, message: String,
throwable: Throwable? = null,
) { ) {
if (minLevel <= LogLevel.INFO) PlatformLog.i(tag, message) if (minLevel <= LogLevel.INFO) PlatformLog.i(tag, message, throwable)
}
inline fun i(
tag: String,
message: () -> String,
) {
if (minLevel <= LogLevel.INFO) PlatformLog.i(tag, message())
} }
fun w( fun w(
@@ -45,6 +61,13 @@ object Log {
if (minLevel <= LogLevel.WARN) PlatformLog.w(tag, message, throwable) if (minLevel <= LogLevel.WARN) PlatformLog.w(tag, message, throwable)
} }
inline fun w(
tag: String,
message: () -> String,
) {
if (minLevel <= LogLevel.WARN) PlatformLog.w(tag, message())
}
fun e( fun e(
tag: String, tag: String,
message: String, message: String,
@@ -52,4 +75,11 @@ object Log {
) { ) {
if (minLevel <= LogLevel.ERROR) PlatformLog.e(tag, message, throwable) if (minLevel <= LogLevel.ERROR) PlatformLog.e(tag, message, throwable)
} }
inline fun e(
tag: String,
message: () -> String,
) {
if (minLevel <= LogLevel.ERROR) PlatformLog.e(tag, message())
}
} }
@@ -36,10 +36,12 @@ expect object PlatformLog {
fun d( fun d(
tag: String, tag: String,
message: String, message: String,
throwable: Throwable? = null,
) )
fun i( fun i(
tag: String, tag: String,
message: String, message: String,
throwable: Throwable? = null,
) )
} }
@@ -56,14 +56,24 @@ actual object PlatformLog {
actual fun d( actual fun d(
tag: String, tag: String,
message: String, message: String,
throwable: Throwable?,
) { ) {
println("${time()} DEBUG: [$tag] $message") if (throwable != null) {
println("${time()} DEBUG: [$tag] $message. Throwable: ${throwable.message}")
} else {
println("${time()} DEBUG: [$tag] $message")
}
} }
actual fun i( actual fun i(
tag: String, tag: String,
message: String, message: String,
throwable: Throwable?,
) { ) {
println("${time()} INFO : [$tag] $message") if (throwable != null) {
println("${time()} INFO : [$tag] $message. Throwable: ${throwable.message}")
} else {
println("${time()} INFO : [$tag] $message")
}
} }
} }