From 95779af7b6c56131be129d31ee96cf3daa3e4fa8 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Wed, 8 Jun 2022 18:23:11 +0600 Subject: [PATCH] it's relovution, Johny --- CHANGELOG.md | 4 ++ gradle.properties | 4 +- src/commonMain/kotlin/KSLog.kt | 10 ++-- .../kotlin/MessageFormatterTypealias.kt | 5 ++ src/jsMain/kotlin/ActualLogger.kt | 36 ++++++++++---- src/jvmMain/kotlin/ActualLogger.kt | 45 ++++++++++------- src/main/kotlin/ActualLogger.kt | 48 ++++++++++++++----- 7 files changed, 107 insertions(+), 45 deletions(-) create mode 100644 src/commonMain/kotlin/MessageFormatterTypealias.kt diff --git a/CHANGELOG.md b/CHANGELOG.md index 27df7c0..922ca3a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## 0.1.0 + +* All the platform specific logged factories has been renamed + ## 0.0.3 * Fixes in `JVM` logging diff --git a/gradle.properties b/gradle.properties index 0f9aa76..c927c80 100644 --- a/gradle.properties +++ b/gradle.properties @@ -9,5 +9,5 @@ android.enableJetifier=true # Project data group=dev.inmo -version=0.0.3 -android_code_version=3 +version=0.1.0 +android_code_version=4 diff --git a/src/commonMain/kotlin/KSLog.kt b/src/commonMain/kotlin/KSLog.kt index 1a9a902..b26a98f 100644 --- a/src/commonMain/kotlin/KSLog.kt +++ b/src/commonMain/kotlin/KSLog.kt @@ -37,10 +37,14 @@ class CallbackKSLog( } -expect fun KSLog( +fun KSLog( defaultTag: String, filter: (l: LogLevel, t: String, m: String, Throwable?) -> Boolean -) : KSLog +) : 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, @@ -52,7 +56,7 @@ fun KSLog( } } -inline fun KSLog( +fun KSLog( defaultTag: String, firstLevel: LogLevel, secondLevel: LogLevel, diff --git a/src/commonMain/kotlin/MessageFormatterTypealias.kt b/src/commonMain/kotlin/MessageFormatterTypealias.kt new file mode 100644 index 0000000..8b2ff38 --- /dev/null +++ b/src/commonMain/kotlin/MessageFormatterTypealias.kt @@ -0,0 +1,5 @@ +package dev.inmo.kslog.common + +typealias MessageFormatter = (l: LogLevel, t: String, m: String, Throwable?) -> String +val defaultMessageFormatter: MessageFormatter = { l, t, m, _ -> "[$l] $t - $m" } + diff --git a/src/jsMain/kotlin/ActualLogger.kt b/src/jsMain/kotlin/ActualLogger.kt index 1c74fcb..21b0c4e 100644 --- a/src/jsMain/kotlin/ActualLogger.kt +++ b/src/jsMain/kotlin/ActualLogger.kt @@ -1,11 +1,12 @@ package dev.inmo.kslog.common -fun KSLog( - messageFormatter: (l: LogLevel, t: String?, m: String, Throwable?) -> String, +fun KSJSLog( + defaultTag: String, + messageFormatter: MessageFormatter = defaultMessageFormatter, filter: (l: LogLevel, t: String?, m: String, Throwable?) -> Boolean ) = KSLog { l, t, m, e -> if (!filter(l, t, m, e)) return@KSLog - val text = messageFormatter(l,t,m,e) + val text = messageFormatter(l, t?:defaultTag, m, e) val args = e ?.let { arrayOf(text, e) } ?: arrayOf(text) @@ -19,12 +20,29 @@ fun KSLog( } } -actual fun KSLog( +fun KSJSLog( defaultTag: String, - filter: (l: LogLevel, t: String, m: String, Throwable?) -> Boolean + messageFormatter: MessageFormatter = defaultMessageFormatter, + levels: Iterable ): KSLog { - return KSLog( - { l, t, m, _ -> "[$l] ${t ?: defaultTag} - $m" }, - { l, t, m, e -> filter(l, t ?: defaultTag, m, e) } - ) + 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.VERBOSE +): KSLog = KSJSLog (defaultTag, messageFormatter) { l, _, _, _ -> + minLoggingLevel.ordinal <= l.ordinal } diff --git a/src/jvmMain/kotlin/ActualLogger.kt b/src/jvmMain/kotlin/ActualLogger.kt index 1f3e556..259fc45 100644 --- a/src/jvmMain/kotlin/ActualLogger.kt +++ b/src/jvmMain/kotlin/ActualLogger.kt @@ -2,13 +2,14 @@ package dev.inmo.kslog.common import java.util.logging.Level -fun KSLog( +fun KSJVMLog( + defaultTag: String, logger: java.util.logging.Logger, - filter: (l: LogLevel, t: String?, m: String, Throwable?) -> Boolean, - messageFormatter: (l: LogLevel, t: String?, m: String, Throwable?) -> String + messageFormatter: MessageFormatter = defaultMessageFormatter, + filter: (l: LogLevel, t: String?, m: String, Throwable?) -> Boolean ) = KSLog { l, t, m, e -> if (!filter(l, t, m, e)) return@KSLog - val text = messageFormatter(l,t,m,e) + val text = messageFormatter(l,t ?: defaultTag,m,e) logger.log( when(l) { LogLevel.VERBOSE -> Level.FINEST @@ -23,24 +24,32 @@ fun KSLog( ) } -fun KSLog( +fun KSJVMLog( defaultTag: String, logger: java.util.logging.Logger, - filter: (l: LogLevel, t: String, m: String, Throwable?) -> Boolean = { _, _, _, _ -> true } + messageFormatter: MessageFormatter = defaultMessageFormatter, + levels: Iterable ): KSLog { - return KSLog( - logger, - { l, t, m, e -> filter(l, t ?: defaultTag, m, e) } - ) { _, t, m, _ -> "${t ?: defaultTag} - $m" } + val levels = levels.toSet() + return KSJVMLog (defaultTag, logger, messageFormatter) { l, _, _, _ -> + l in levels + } } -actual fun KSLog( +fun KSJVMLog( defaultTag: String, - filter: (l: LogLevel, t: String, m: String, Throwable?) -> Boolean -): KSLog { - return KSLog( - defaultTag, - logger = java.util.logging.Logger.getAnonymousLogger(), - filter = { l, t, m, e -> filter(l, t, m, e) } - ) + logger: java.util.logging.Logger, + messageFormatter: MessageFormatter = defaultMessageFormatter, + 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, + messageFormatter: MessageFormatter = defaultMessageFormatter, + minLoggingLevel: LogLevel = LogLevel.VERBOSE +): KSLog = KSJVMLog (defaultTag, logger, messageFormatter) { l, _, _, _ -> + minLoggingLevel.ordinal <= l.ordinal } diff --git a/src/main/kotlin/ActualLogger.kt b/src/main/kotlin/ActualLogger.kt index e09626c..681b04b 100644 --- a/src/main/kotlin/ActualLogger.kt +++ b/src/main/kotlin/ActualLogger.kt @@ -2,20 +2,42 @@ package dev.inmo.kslog.common import android.util.Log -actual fun KSLog( +fun KSAndroidLog( defaultTag: String, filter: (l: LogLevel, t: String, m: String, Throwable?) -> Boolean -): KSLog { - return KSLog { l, t, m, e -> - val tag = t ?: defaultTag - if (!filter(l, t ?: defaultTag, m, e)) return@KSLog - when(l) { - 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, m, e) - } +) = KSLog { l, t, m, e -> + if (!filter(l, t ?: defaultTag, m, e)) return@KSLog + val tag = t ?: defaultTag + when(l) { + 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, m, 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 +}