diff --git a/CHANGELOG.md b/CHANGELOG.md index 4e3f7a1..2062367 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ # Changelog +## 0.4.2 + +* Add `setDefaultKSLog` and `addDefaultKSLog` +* Add `plus` operation for two `KSLog` instances to call them both on each log performing +* Add `KSLog` factory for simple creation of `CallbackKSLog` +* Add `MessageFormatter` factory + ## 0.4.1 * Fixes in resolution ambiguity for functions with messages diff --git a/gradle.properties b/gradle.properties index 9283f6f..8bba418 100644 --- a/gradle.properties +++ b/gradle.properties @@ -9,5 +9,5 @@ android.enableJetifier=true # Project data group=dev.inmo -version=0.4.1 -android_code_version=11 +version=0.4.2 +android_code_version=12 diff --git a/src/commonMain/kotlin/CallbackKSLog.kt b/src/commonMain/kotlin/CallbackKSLog.kt index 7d64495..4a43b34 100644 --- a/src/commonMain/kotlin/CallbackKSLog.kt +++ b/src/commonMain/kotlin/CallbackKSLog.kt @@ -7,3 +7,7 @@ class CallbackKSLog( ) : KSLog { override fun performLog(level: LogLevel, tag: String?, message: Any, throwable: Throwable?) = performLogCallback(level, tag, message, throwable) } + +fun KSLog( + performLogCallback: SimpleKSLogCallback +) = CallbackKSLog(performLogCallback) diff --git a/src/commonMain/kotlin/KSLog.kt b/src/commonMain/kotlin/KSLog.kt index be76ca4..40d8270 100644 --- a/src/commonMain/kotlin/KSLog.kt +++ b/src/commonMain/kotlin/KSLog.kt @@ -1,6 +1,7 @@ package dev.inmo.kslog.common import dev.inmo.kslog.common.filter.filtered +import dev.inmo.kslog.common.utils.plus enum class LogLevel { @@ -110,3 +111,6 @@ fun KSLog( ).filtered { l, _, _ -> minLoggingLevel.ordinal <= l.ordinal } + +fun setDefaultKSLog(newDefault: KSLog) { KSLog.default = newDefault } +fun addDefaultKSLog(newDefault: KSLog) { KSLog.default = KSLog.default + newDefault } diff --git a/src/commonMain/kotlin/KSLogTypealiases.kt b/src/commonMain/kotlin/KSLogTypealiases.kt index d36b5a1..0e2b5c8 100644 --- a/src/commonMain/kotlin/KSLogTypealiases.kt +++ b/src/commonMain/kotlin/KSLogTypealiases.kt @@ -1,6 +1,8 @@ package dev.inmo.kslog.common -typealias MessageFormatter = (l: LogLevel, t: String, m: Any, Throwable?) -> String +typealias MessageFormatter = (l: LogLevel, t: String?, m: Any, Throwable?) -> String +@Suppress("NOTHING_TO_INLINE") +inline fun MessageFormatter(noinline formatter: MessageFormatter) = formatter typealias MessageFilter = (l: LogLevel, t: String?, Throwable?) -> Boolean -val defaultMessageFormatter: MessageFormatter = { l, t, m, _ -> "[$l] $t - $m" } +val defaultMessageFormatter: MessageFormatter = { l, t, m, _ -> "[$l] ${t ?.let { "$it " } ?: ""}- $m" } diff --git a/src/commonMain/kotlin/utils/Combination.kt b/src/commonMain/kotlin/utils/Combination.kt new file mode 100644 index 0000000..cee2897 --- /dev/null +++ b/src/commonMain/kotlin/utils/Combination.kt @@ -0,0 +1,15 @@ +package dev.inmo.kslog.common.utils + +import dev.inmo.kslog.common.CallbackKSLog +import dev.inmo.kslog.common.KSLog + +infix operator fun KSLog.plus(other: KSLog) = CallbackKSLog { l, t, m, e -> + val resultOfFirst = runCatching { + this@plus.performLog(l, t, m, e) + } + val resultOfSecond = runCatching { + other.performLog(l, t, m, e) + } + resultOfFirst.onFailure { throw it } + resultOfSecond.onFailure { throw it } +}