From be047cb8b51ba9bb045bfd48a2c62945860ff8ca Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Wed, 8 Jun 2022 22:51:03 +0600 Subject: [PATCH] rewrite of API with expect-actual and removing of redundant platform-specific factory names --- CHANGELOG.md | 4 ++ gradle.properties | 4 +- src/commonMain/kotlin/KSLog.kt | 39 +++++++------- ...matterTypealias.kt => KSLogTypealiases.kt} | 1 + src/jsMain/kotlin/ActualLogger.kt | 35 ++----------- src/jvmMain/kotlin/ActualLogger.kt | 51 +++++++++++-------- src/main/kotlin/ActualLogger.kt | 44 ++++------------ 7 files changed, 72 insertions(+), 106 deletions(-) rename src/commonMain/kotlin/{MessageFormatterTypealias.kt => KSLogTypealiases.kt} (69%) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5595e59..c373ca6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## 0.2.0 + +* Full rewrite of API + ## 0.1.1 * Changes in `JVM` levels diff --git a/gradle.properties b/gradle.properties index ce0643a..6fad9b1 100644 --- a/gradle.properties +++ b/gradle.properties @@ -9,5 +9,5 @@ android.enableJetifier=true # Project data group=dev.inmo -version=0.1.1 -android_code_version=5 +version=0.2.0 +android_code_version=6 diff --git a/src/commonMain/kotlin/KSLog.kt b/src/commonMain/kotlin/KSLog.kt index 51aca1f..000ebcd 100644 --- a/src/commonMain/kotlin/KSLog.kt +++ b/src/commonMain/kotlin/KSLog.kt @@ -36,36 +36,39 @@ class CallbackKSLog( override fun performLog(level: LogLevel, tag: String?, message: String, throwable: Throwable?) = performLogCallback(level, tag, message, throwable) } +expect fun KSLog( + defaultTag: String, + filter: MessageFilter = { _, _, _, _ -> true }, + messageFormatter: MessageFormatter = defaultMessageFormatter +): KSLog fun KSLog( defaultTag: String, - filter: (l: LogLevel, t: String, m: String, Throwable?) -> Boolean -) : KSLog = KSLog { l, t, m, e -> - if (filter(l, t ?: defaultTag, m, e)) { - KSLog.default.performLog(l, t ?: defaultTag, m, e) - } -} - -fun KSLog( - defaultTag: String, - levels: Iterable + levels: Iterable, + messageFormatter: MessageFormatter = defaultMessageFormatter ): KSLog { val levels = levels.toSet() - return KSLog (defaultTag) { l, _, _, _ -> + return KSLog (defaultTag, { l, _, _, _ -> l in levels - } + }, messageFormatter) } fun KSLog( defaultTag: String, firstLevel: LogLevel, secondLevel: LogLevel, - vararg otherLevels: LogLevel -): KSLog = KSLog(defaultTag, setOf(firstLevel, secondLevel, *otherLevels)) + vararg otherLevels: LogLevel, + messageFormatter: MessageFormatter = defaultMessageFormatter, +): KSLog = KSLog(defaultTag, setOf(firstLevel, secondLevel, *otherLevels), messageFormatter) fun KSLog( defaultTag: String, - minLoggingLevel: LogLevel = LogLevel.values().first() -): KSLog = KSLog (defaultTag) { l, _, _, _ -> - minLoggingLevel.ordinal <= l.ordinal -} + minLoggingLevel: LogLevel, + messageFormatter: MessageFormatter = defaultMessageFormatter, +): KSLog = KSLog ( + defaultTag, + { l, _, _, _ -> + minLoggingLevel.ordinal <= l.ordinal + }, + messageFormatter +) diff --git a/src/commonMain/kotlin/MessageFormatterTypealias.kt b/src/commonMain/kotlin/KSLogTypealiases.kt similarity index 69% rename from src/commonMain/kotlin/MessageFormatterTypealias.kt rename to src/commonMain/kotlin/KSLogTypealiases.kt index 8b2ff38..3f7e1c9 100644 --- a/src/commonMain/kotlin/MessageFormatterTypealias.kt +++ b/src/commonMain/kotlin/KSLogTypealiases.kt @@ -1,5 +1,6 @@ package dev.inmo.kslog.common typealias MessageFormatter = (l: LogLevel, t: String, m: String, Throwable?) -> String +typealias MessageFilter = (l: LogLevel, t: String?, m: String, Throwable?) -> Boolean val defaultMessageFormatter: MessageFormatter = { l, t, m, _ -> "[$l] $t - $m" } diff --git a/src/jsMain/kotlin/ActualLogger.kt b/src/jsMain/kotlin/ActualLogger.kt index 1390ee4..79fa303 100644 --- a/src/jsMain/kotlin/ActualLogger.kt +++ b/src/jsMain/kotlin/ActualLogger.kt @@ -1,10 +1,10 @@ package dev.inmo.kslog.common -fun KSJSLog( +actual fun KSLog( defaultTag: String, - messageFormatter: MessageFormatter = defaultMessageFormatter, - filter: (l: LogLevel, t: String?, m: String, Throwable?) -> Boolean -) = KSLog { l, t, m, e -> + filter: MessageFilter, + messageFormatter: MessageFormatter +): KSLog = KSLog { l, t, m, e -> if (!filter(l, t, m, e)) return@KSLog val text = messageFormatter(l, t?:defaultTag, m, e) val args = e ?.let { @@ -19,30 +19,3 @@ fun KSJSLog( LogLevel.ASSERT -> console.error(*args) } } - -fun KSJSLog( - defaultTag: String, - messageFormatter: MessageFormatter = defaultMessageFormatter, - levels: Iterable -): KSLog { - val levels = levels.toSet() - return KSJSLog (defaultTag, messageFormatter) { l, _, _, _ -> - l in levels - } -} - -fun KSJSLog( - defaultTag: String, - messageFormatter: MessageFormatter = defaultMessageFormatter, - firstLevel: LogLevel, - secondLevel: LogLevel, - vararg otherLevels: LogLevel -): KSLog = KSJSLog (defaultTag, messageFormatter, setOf(firstLevel, secondLevel, *otherLevels)) - -fun KSJSLog( - defaultTag: String, - messageFormatter: MessageFormatter = defaultMessageFormatter, - minLoggingLevel: LogLevel = LogLevel.values().first() -): KSLog = KSJSLog (defaultTag, messageFormatter) { l, _, _, _ -> - minLoggingLevel.ordinal <= l.ordinal -} diff --git a/src/jvmMain/kotlin/ActualLogger.kt b/src/jvmMain/kotlin/ActualLogger.kt index febe1d6..1ab1803 100644 --- a/src/jvmMain/kotlin/ActualLogger.kt +++ b/src/jvmMain/kotlin/ActualLogger.kt @@ -1,12 +1,13 @@ package dev.inmo.kslog.common import java.util.logging.Level +import java.util.logging.Logger -fun KSJVMLog( +fun KSLog( defaultTag: String, - logger: java.util.logging.Logger, - messageFormatter: MessageFormatter = defaultMessageFormatter, - filter: (l: LogLevel, t: String?, m: String, Throwable?) -> Boolean + logger: Logger, + filter: MessageFilter = { _, _, _, _ -> true }, + messageFormatter: MessageFormatter = defaultMessageFormatter ) = KSLog { l, t, m, e -> if (!filter(l, t, m, e)) return@KSLog val text = messageFormatter(l,t ?: defaultTag,m,e) @@ -24,32 +25,38 @@ fun KSJVMLog( ) } -fun KSJVMLog( +fun KSLog( defaultTag: String, - logger: java.util.logging.Logger, - messageFormatter: MessageFormatter = defaultMessageFormatter, - levels: Iterable + logger: Logger, + levels: Iterable, + messageFormatter: MessageFormatter = defaultMessageFormatter ): KSLog { val levels = levels.toSet() - return KSJVMLog (defaultTag, logger, messageFormatter) { l, _, _, _ -> + return KSLog (defaultTag, logger, { l, _, _, _ -> l in levels - } + }, messageFormatter) } -fun KSJVMLog( +fun KSLog( defaultTag: String, - logger: java.util.logging.Logger, - messageFormatter: MessageFormatter = defaultMessageFormatter, + logger: Logger, firstLevel: LogLevel, secondLevel: LogLevel, - vararg otherLevels: LogLevel -): KSLog = KSJVMLog (defaultTag, logger, messageFormatter, setOf(firstLevel, secondLevel, *otherLevels)) - -fun KSJVMLog( - defaultTag: String, - logger: java.util.logging.Logger, + vararg otherLevels: LogLevel, messageFormatter: MessageFormatter = defaultMessageFormatter, - minLoggingLevel: LogLevel = LogLevel.values().first() -): KSLog = KSJVMLog (defaultTag, logger, messageFormatter) { l, _, _, _ -> +): KSLog = KSLog (defaultTag, logger, setOf(firstLevel, secondLevel, *otherLevels), messageFormatter) + +fun KSLog( + defaultTag: String, + logger: Logger, + minLoggingLevel: LogLevel = LogLevel.values().first(), + messageFormatter: MessageFormatter = defaultMessageFormatter +): KSLog = KSLog (defaultTag, logger, { l, _, _, _ -> minLoggingLevel.ordinal <= l.ordinal -} +}, messageFormatter) + +actual fun KSLog( + defaultTag: String, + filter: MessageFilter, + messageFormatter: MessageFormatter +): KSLog = KSLog(defaultTag, Logger.getLogger("KSLog"), filter, messageFormatter) diff --git a/src/main/kotlin/ActualLogger.kt b/src/main/kotlin/ActualLogger.kt index 06deb6c..b97873b 100644 --- a/src/main/kotlin/ActualLogger.kt +++ b/src/main/kotlin/ActualLogger.kt @@ -2,42 +2,20 @@ package dev.inmo.kslog.common import android.util.Log -fun KSAndroidLog( +actual fun KSLog( defaultTag: String, - filter: (l: LogLevel, t: String, m: String, Throwable?) -> Boolean -) = KSLog { l, t, m, e -> + filter: MessageFilter, + messageFormatter: MessageFormatter +): KSLog = KSLog { l, t, m, e -> if (!filter(l, t ?: defaultTag, m, e)) return@KSLog val tag = t ?: defaultTag + val text = messageFormatter(l, tag, m, e) when(l) { - LogLevel.DEBUG -> Log.d(tag, m, e) - LogLevel.VERBOSE -> Log.v(tag, m, e) - LogLevel.INFO -> Log.i(tag, m, e) - LogLevel.WARNING -> Log.w(tag, m, e) - LogLevel.ERROR -> Log.e(tag, m, e) - LogLevel.ASSERT -> Log.wtf(tag, m, e) + LogLevel.DEBUG -> Log.d(tag, text, e) + LogLevel.VERBOSE -> Log.v(tag, text, e) + LogLevel.INFO -> Log.i(tag, text, e) + LogLevel.WARNING -> Log.w(tag, text, e) + LogLevel.ERROR -> Log.e(tag, text, e) + LogLevel.ASSERT -> Log.wtf(tag, text, e) } } - -fun KSAndroidLog( - defaultTag: String, - levels: Iterable -): KSLog { - val levels = levels.toSet() - return KSAndroidLog (defaultTag) { l, _, _, _ -> - l in levels - } -} - -inline fun KSAndroidLog( - defaultTag: String, - firstLevel: LogLevel, - secondLevel: LogLevel, - vararg otherLevels: LogLevel -): KSLog = KSAndroidLog(defaultTag, setOf(firstLevel, secondLevel, *otherLevels)) - -fun KSAndroidLog( - defaultTag: String, - minLoggingLevel: LogLevel = LogLevel.VERBOSE -): KSLog = KSAndroidLog (defaultTag) { l, _, _, _ -> - minLoggingLevel.ordinal <= l.ordinal -}