From de7f735ad0c1e4e8f8754bc8b1274c86e358621a Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Thu, 9 Jun 2022 16:29:56 +0600 Subject: [PATCH] fixes --- CHANGELOG.md | 2 +- gradle.properties | 4 +- src/commonMain/kotlin/DefaultKSLog.kt | 28 +++++++++---- src/commonMain/kotlin/Extensions.kt | 34 +++++++++++----- src/commonMain/kotlin/KSLog.kt | 31 ++++++++++++--- src/commonMain/kotlin/KSLogTypealiases.kt | 2 +- src/jsMain/kotlin/ActualLogger.kt | 12 ++---- src/jvmMain/kotlin/ActualLogger.kt | 48 ++++++++++++----------- src/main/kotlin/ActualLogger.kt | 22 ++++------- 9 files changed, 111 insertions(+), 72 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 722d63b..7fc10ce 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,6 @@ # Changelog -## 0.2.1 +## 0.3.0 ## 0.2.0 diff --git a/gradle.properties b/gradle.properties index b878de3..357bd15 100644 --- a/gradle.properties +++ b/gradle.properties @@ -9,5 +9,5 @@ android.enableJetifier=true # Project data group=dev.inmo -version=0.2.1 -android_code_version=7 +version=0.3.0 +android_code_version=8 diff --git a/src/commonMain/kotlin/DefaultKSLog.kt b/src/commonMain/kotlin/DefaultKSLog.kt index 5fbd9f5..c59fb53 100644 --- a/src/commonMain/kotlin/DefaultKSLog.kt +++ b/src/commonMain/kotlin/DefaultKSLog.kt @@ -2,25 +2,39 @@ package dev.inmo.kslog.common class DefaultKSLog( private val defaultTag: String, - private val filter: MessageFilter = { _, _, _, _ -> true }, + val filter: MessageFilter = { _, _, _ -> true }, private val messageFormatter: MessageFormatter = defaultMessageFormatter, - private val logging: (level: LogLevel, tag: String, message: String, throwable: Throwable?) -> Unit + private val logging: (level: LogLevel, tag: String, message: String, throwable: Throwable?) -> Unit = defaultLogging ) : KSLog { override fun performLog(level: LogLevel, tag: String?, message: String, throwable: Throwable?) { - TODO("Not yet implemented") + val tag = tag ?: defaultTag + + if (filter(level, tag, throwable)) { + val text = messageFormatter(level, tag, message, throwable) + logging(level, tag, text, throwable) + } } override fun performLog(level: LogLevel, tag: String?, throwable: Throwable?, messageBuilder: () -> String) { - super.performLog(level, tag, throwable, messageBuilder) + val tag = tag ?: defaultTag + + if (filter(level, tag, throwable)) { + val text = messageFormatter(level, tag, messageBuilder(), throwable) + logging(level, tag, text, throwable) + } } - override suspend fun performLog( + override suspend fun performLogS( level: LogLevel, tag: String?, throwable: Throwable?, messageBuilder: suspend () -> String ) { - if (filter(level, tag, )) - super.performLog(level, tag, throwable, messageBuilder) + val tag = tag ?: defaultTag + + if (filter(level, tag, throwable)) { + val text = messageFormatter(level, tag, messageBuilder(), throwable) + logging(level, tag, text, throwable) + } } } diff --git a/src/commonMain/kotlin/Extensions.kt b/src/commonMain/kotlin/Extensions.kt index ac2fbd1..cf7194d 100644 --- a/src/commonMain/kotlin/Extensions.kt +++ b/src/commonMain/kotlin/Extensions.kt @@ -3,71 +3,85 @@ package dev.inmo.kslog.common inline fun KSLog.log(level: LogLevel, tag: String? = null, e: Throwable? = null, noinline messageBuilder: () -> String) = performLog(level, tag, e, messageBuilder) +suspend inline fun KSLog.logS(level: LogLevel, tag: String? = null, e: Throwable? = null, noinline messageBuilder: suspend () -> String) = performLogS(level, tag, e, messageBuilder) inline fun KSLog.log(level: LogLevel, message: String, e: Throwable? = null) = performLog(level, message, e) inline fun KSLog.log(level: LogLevel, tag: String, message: String, e: Throwable? = null) = performLog(level, tag, message, e) +inline fun KSLog.debug(tag: String? = null, e: Throwable? = null, noinline messageBuilder: () -> String) = log(LogLevel.DEBUG, tag, e, messageBuilder) +suspend inline fun KSLog.debugS(tag: String? = null, e: Throwable? = null, noinline messageBuilder: suspend () -> String) = logS(LogLevel.DEBUG, tag, e, messageBuilder) +inline fun KSLog.debug(message: String, e: Throwable? = null) = log(LogLevel.DEBUG, message, e) +inline fun KSLog.debug(tag: String, message: String, e: Throwable? = null) = log(LogLevel.DEBUG, tag, message, e) + + inline fun KSLog.verbose(tag: String? = null, e: Throwable? = null, noinline messageBuilder: () -> String) = log(LogLevel.VERBOSE, tag, e, messageBuilder) +suspend inline fun KSLog.verboseS(tag: String? = null, e: Throwable? = null, noinline messageBuilder: suspend () -> String) = logS(LogLevel.VERBOSE, tag, e, messageBuilder) inline fun KSLog.verbose(message: String, e: Throwable? = null) = log(LogLevel.VERBOSE, message, e) inline fun KSLog.verbose(tag: String, message: String, e: Throwable? = null) = log(LogLevel.VERBOSE, tag, message, e) inline fun KSLog.info(tag: String? = null, e: Throwable? = null, noinline messageBuilder: () -> String) = log(LogLevel.INFO, tag, e, messageBuilder) +suspend inline fun KSLog.infoS(tag: String? = null, e: Throwable? = null, noinline messageBuilder: suspend () -> String) = logS(LogLevel.INFO, tag, e, messageBuilder) inline fun KSLog.info(message: String, e: Throwable? = null) = log(LogLevel.INFO, message, e) inline fun KSLog.info(tag: String, message: String, e: Throwable? = null) = log(LogLevel.INFO, tag, message, e) inline fun KSLog.warning(tag: String? = null, e: Throwable? = null, noinline messageBuilder: () -> String) = log(LogLevel.WARNING, tag, e, messageBuilder) +suspend inline fun KSLog.warningS(tag: String? = null, e: Throwable? = null, noinline messageBuilder: suspend () -> String) = logS(LogLevel.WARNING, tag, e, messageBuilder) inline fun KSLog.warning(message: String, e: Throwable? = null) = log(LogLevel.WARNING, message, e) inline fun KSLog.warning(tag: String, message: String, e: Throwable? = null) = log(LogLevel.WARNING, tag, message, e) inline fun KSLog.error(tag: String? = null, e: Throwable? = null, noinline messageBuilder: () -> String) = log(LogLevel.ERROR, tag, e, messageBuilder) +suspend inline fun KSLog.errorS(tag: String? = null, e: Throwable? = null, noinline messageBuilder: suspend () -> String) = logS(LogLevel.ERROR, tag, e, messageBuilder) inline fun KSLog.error(message: String, e: Throwable? = null) = log(LogLevel.ERROR, message, e) inline fun KSLog.error(tag: String, message: String, e: Throwable? = null) = log(LogLevel.ERROR, tag, message, e) inline fun KSLog.assert(tag: String? = null, e: Throwable? = null, noinline messageBuilder: () -> String) = log(LogLevel.ASSERT, tag, e, messageBuilder) +suspend inline fun KSLog.assertS(tag: String? = null, e: Throwable? = null, noinline messageBuilder: suspend () -> String) = logS(LogLevel.ASSERT, tag, e, messageBuilder) inline fun KSLog.assert(message: String, e: Throwable? = null) = log(LogLevel.ASSERT, message, e) inline fun KSLog.assert(tag: String, message: String, e: Throwable? = null) = log(LogLevel.ASSERT, tag, message, e) - -inline fun KSLog.debug(tag: String? = null, e: Throwable? = null, noinline messageBuilder: () -> String) = log(LogLevel.DEBUG, tag, e, messageBuilder) -inline fun KSLog.debug(message: String, e: Throwable? = null) = log(LogLevel.DEBUG, message, e) -inline fun KSLog.debug(tag: String, message: String, e: Throwable? = null) = log(LogLevel.DEBUG, tag, message, e) - // ----- Short names inline fun KSLog.l(level: LogLevel, tag: String? = null, e: Throwable? = null, noinline messageBuilder: () -> String) = log(level, tag, e, messageBuilder) +suspend inline fun KSLog.lS(level: LogLevel, tag: String? = null, e: Throwable? = null, noinline messageBuilder: suspend () -> String) = logS(level, tag, e, messageBuilder) inline fun KSLog.l(level: LogLevel, message: String, e: Throwable? = null) = log(level, message, e) inline fun KSLog.l(level: LogLevel, tag: String, message: String, e: Throwable? = null) = log(level, tag, message, e) + +inline fun KSLog.d(tag: String? = null, e: Throwable? = null, noinline messageBuilder: () -> String) = debug(tag, e, messageBuilder) +suspend inline fun KSLog.dS(tag: String? = null, e: Throwable? = null, noinline messageBuilder: suspend () -> String) = debugS(tag, e, messageBuilder) +inline fun KSLog.d(message: String, e: Throwable? = null) = debug(message, e) +inline fun KSLog.d(tag: String, message: String, e: Throwable? = null) = debug(tag, message, e) + inline fun KSLog.v(tag: String? = null, e: Throwable? = null, noinline messageBuilder: () -> String) = verbose(tag, e, messageBuilder) +suspend inline fun KSLog.vS(tag: String? = null, e: Throwable? = null, noinline messageBuilder: suspend () -> String) = verboseS(tag, e, messageBuilder) inline fun KSLog.v(message: String, e: Throwable? = null) = verbose(message, e) inline fun KSLog.v(tag: String, message: String, e: Throwable? = null) = verbose(tag, message, e) inline fun KSLog.i(tag: String? = null, e: Throwable? = null, noinline messageBuilder: () -> String) = info(tag, e, messageBuilder) +suspend inline fun KSLog.iS(tag: String? = null, e: Throwable? = null, noinline messageBuilder: suspend () -> String) = infoS(tag, e, messageBuilder) inline fun KSLog.i(message: String, e: Throwable? = null) = info(message, e) inline fun KSLog.i(tag: String, message: String, e: Throwable? = null) = info(tag, message, e) inline fun KSLog.w(tag: String? = null, e: Throwable? = null, noinline messageBuilder: () -> String) = warning(tag, e, messageBuilder) +suspend inline fun KSLog.wS(tag: String? = null, e: Throwable? = null, noinline messageBuilder: suspend () -> String) = warningS(tag, e, messageBuilder) inline fun KSLog.w(message: String, e: Throwable? = null) = warning(message, e) inline fun KSLog.w(tag: String, message: String, e: Throwable? = null) = warning(tag, message, e) inline fun KSLog.e(tag: String? = null, e: Throwable? = null, noinline messageBuilder: () -> String) = error(tag, e, messageBuilder) +suspend inline fun KSLog.eS(tag: String? = null, e: Throwable? = null, noinline messageBuilder: suspend () -> String) = errorS(tag, e, messageBuilder) inline fun KSLog.e(message: String, e: Throwable? = null) = error(message, e) inline fun KSLog.e(tag: String, message: String, e: Throwable? = null) = error(tag, message, e) inline fun KSLog.wtf(tag: String? = null, e: Throwable? = null, noinline messageBuilder: () -> String) = assert(tag, e, messageBuilder) +suspend inline fun KSLog.wtfS(tag: String? = null, e: Throwable? = null, noinline messageBuilder: suspend () -> String) = assertS(tag, e, messageBuilder) inline fun KSLog.wtf(message: String, e: Throwable? = null) = assert(message, e) inline fun KSLog.wtf(tag: String, message: String, e: Throwable? = null) = assert(tag, message, e) - - -inline fun KSLog.d(tag: String? = null, e: Throwable? = null, noinline messageBuilder: () -> String) = debug(tag, e, messageBuilder) -inline fun KSLog.d(message: String, e: Throwable? = null) = debug(message, e) -inline fun KSLog.d(tag: String, message: String, e: Throwable? = null) = debug(tag, message, e) diff --git a/src/commonMain/kotlin/KSLog.kt b/src/commonMain/kotlin/KSLog.kt index 182283e..c387adf 100644 --- a/src/commonMain/kotlin/KSLog.kt +++ b/src/commonMain/kotlin/KSLog.kt @@ -19,7 +19,7 @@ interface KSLog { throwable: Throwable?, messageBuilder: () -> String ) = performLog(level, tag, messageBuilder(), throwable) - suspend fun performLog( + suspend fun performLogS( level: LogLevel, tag: String?, throwable: Throwable?, @@ -37,17 +37,36 @@ interface KSLog { defaultLogger = value } override fun performLog(level: LogLevel, tag: String?, message: String, throwable: Throwable?) = default.performLog(level, tag, message, throwable) + override fun performLog(level: LogLevel, message: String, throwable: Throwable?) = default.performLog(level, message, throwable) + override fun performLog( + level: LogLevel, + tag: String?, + throwable: Throwable?, + messageBuilder: () -> String + ) = default.performLog(level, tag, throwable, messageBuilder) + override suspend fun performLogS( + level: LogLevel, + tag: String?, + throwable: Throwable?, + messageBuilder: suspend () -> String + ) = default.performLogS(level, tag, throwable, messageBuilder) } } operator fun KSLog.invoke(performLogCallback: (level: LogLevel, tag: String?, message: String, throwable: Throwable?) -> Unit) = CallbackKSLog(performLogCallback) -expect fun KSLog( +internal expect val defaultLogging: (level: LogLevel, tag: String, message: String, throwable: Throwable?) -> Unit + +fun KSLog( defaultTag: String, - filter: MessageFilter = { _, _, _, _ -> true }, + filter: MessageFilter = { _, _, _ -> true }, messageFormatter: MessageFormatter = defaultMessageFormatter -): KSLog +): KSLog = DefaultKSLog( + defaultTag, + filter, + messageFormatter +) fun KSLog( defaultTag: String, @@ -55,7 +74,7 @@ fun KSLog( messageFormatter: MessageFormatter = defaultMessageFormatter ): KSLog { val levels = levels.toSet() - return KSLog (defaultTag, { l, _, _, _ -> + return KSLog (defaultTag, { l, _, _ -> l in levels }, messageFormatter) } @@ -74,7 +93,7 @@ fun KSLog( messageFormatter: MessageFormatter = defaultMessageFormatter, ): KSLog = KSLog ( defaultTag, - { l, _, _, _ -> + { l, _, _ -> minLoggingLevel.ordinal <= l.ordinal }, messageFormatter diff --git a/src/commonMain/kotlin/KSLogTypealiases.kt b/src/commonMain/kotlin/KSLogTypealiases.kt index 3f7e1c9..a9ad757 100644 --- a/src/commonMain/kotlin/KSLogTypealiases.kt +++ b/src/commonMain/kotlin/KSLogTypealiases.kt @@ -1,6 +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 +typealias MessageFilter = (l: LogLevel, t: 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 79fa303..0ec2204 100644 --- a/src/jsMain/kotlin/ActualLogger.kt +++ b/src/jsMain/kotlin/ActualLogger.kt @@ -1,15 +1,9 @@ package dev.inmo.kslog.common -actual fun KSLog( - defaultTag: String, - 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) +internal actual val defaultLogging: (level: LogLevel, tag: String, message: String, throwable: Throwable?) -> Unit = { l, t, m, e -> val args = e ?.let { - arrayOf(text, e) - } ?: arrayOf(text) + arrayOf(m, e) + } ?: arrayOf(m) when (l) { LogLevel.DEBUG -> console.log(*args) LogLevel.VERBOSE, diff --git a/src/jvmMain/kotlin/ActualLogger.kt b/src/jvmMain/kotlin/ActualLogger.kt index 1ab1803..c4d1a94 100644 --- a/src/jvmMain/kotlin/ActualLogger.kt +++ b/src/jvmMain/kotlin/ActualLogger.kt @@ -3,26 +3,36 @@ package dev.inmo.kslog.common import java.util.logging.Level import java.util.logging.Logger +private val defaultKSLogLogger by lazy { + Logger.getLogger("KSLog") +} +private fun Logger.doLog( + l: LogLevel, t: String, m: String, e: Throwable? +) = log( + when(l) { + LogLevel.DEBUG -> Level.FINEST + LogLevel.VERBOSE -> Level.FINE + LogLevel.INFO -> Level.INFO + LogLevel.WARNING -> Level.WARNING + LogLevel.ERROR -> Level.SEVERE + LogLevel.ASSERT -> Level.SEVERE + }, + m, + e +) +internal actual val defaultLogging: (level: LogLevel, tag: String, message: String, throwable: Throwable?) -> Unit = { l, t, m, e -> + defaultKSLogLogger.doLog(l, t, m, e) +} + fun KSLog( defaultTag: String, logger: Logger, - filter: MessageFilter = { _, _, _, _ -> true }, + filter: MessageFilter = { _, _, _ -> true }, messageFormatter: MessageFormatter = defaultMessageFormatter ) = KSLog { l, t, m, e -> - if (!filter(l, t, m, e)) return@KSLog + if (!filter(l, t, e)) return@KSLog val text = messageFormatter(l,t ?: defaultTag,m,e) - logger.log( - when(l) { - LogLevel.DEBUG -> Level.FINEST - LogLevel.VERBOSE -> Level.FINE - LogLevel.INFO -> Level.INFO - LogLevel.WARNING -> Level.WARNING - LogLevel.ERROR -> Level.SEVERE - LogLevel.ASSERT -> Level.SEVERE - }, - text, - e - ) + logger.doLog(l, t ?: defaultTag, text, e) } fun KSLog( @@ -32,7 +42,7 @@ fun KSLog( messageFormatter: MessageFormatter = defaultMessageFormatter ): KSLog { val levels = levels.toSet() - return KSLog (defaultTag, logger, { l, _, _, _ -> + return KSLog (defaultTag, logger, { l, _, _ -> l in levels }, messageFormatter) } @@ -51,12 +61,6 @@ fun KSLog( logger: Logger, minLoggingLevel: LogLevel = LogLevel.values().first(), messageFormatter: MessageFormatter = defaultMessageFormatter -): KSLog = KSLog (defaultTag, logger, { l, _, _, _ -> +): 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 b97873b..03f9b03 100644 --- a/src/main/kotlin/ActualLogger.kt +++ b/src/main/kotlin/ActualLogger.kt @@ -2,20 +2,14 @@ package dev.inmo.kslog.common import android.util.Log -actual fun KSLog( - defaultTag: String, - 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) + +internal actual val defaultLogging: (level: LogLevel, tag: String, message: String, throwable: Throwable?) -> Unit = { l, t, m, e -> when(l) { - 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) + LogLevel.DEBUG -> Log.d(t, m, e) + LogLevel.VERBOSE -> Log.v(t, m, e) + LogLevel.INFO -> Log.i(t, m, e) + LogLevel.WARNING -> Log.w(t, m, e) + LogLevel.ERROR -> Log.e(t, m, e) + LogLevel.ASSERT -> Log.wtf(t, m, e) } }