From 7fec4333442bda887715f8d781a4e5c1bed4f891 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Tue, 7 Jun 2022 21:42:19 +0600 Subject: [PATCH] final fixes before publish --- src/commonMain/kotlin/AutoLoggers.kt | 8 +++ src/commonMain/kotlin/Extensions.kt | 84 ++++++++++++++-------------- src/commonMain/kotlin/KSLog.kt | 50 +++++++++++++++++ src/commonMain/kotlin/Logger.kt | 40 ------------- src/jsMain/kotlin/ActualLogger.kt | 25 ++++----- src/jvmMain/kotlin/ActualLogger.kt | 62 +++++++++++++------- src/main/kotlin/ActualLogger.kt | 10 ++-- 7 files changed, 158 insertions(+), 121 deletions(-) create mode 100644 src/commonMain/kotlin/AutoLoggers.kt create mode 100644 src/commonMain/kotlin/KSLog.kt delete mode 100644 src/commonMain/kotlin/Logger.kt diff --git a/src/commonMain/kotlin/AutoLoggers.kt b/src/commonMain/kotlin/AutoLoggers.kt new file mode 100644 index 0000000..88d9c0e --- /dev/null +++ b/src/commonMain/kotlin/AutoLoggers.kt @@ -0,0 +1,8 @@ +package dev.inmo.kslog.common + +val Any.logTag + get() = this::class.simpleName ?: error("Unable to retrieve log tag") +val Any.logger + get() = CallbackKSLog { l, t, m, e -> + KSLog.DEFAULT.performLog(l, t ?: logTag, m, e) + } diff --git a/src/commonMain/kotlin/Extensions.kt b/src/commonMain/kotlin/Extensions.kt index c0a3a5f..abdf527 100644 --- a/src/commonMain/kotlin/Extensions.kt +++ b/src/commonMain/kotlin/Extensions.kt @@ -2,72 +2,72 @@ package dev.inmo.kslog.common -inline fun Logger.log(level: LogLevel, tag: String? = null, e: Throwable? = null, messageBuilder: () -> String) = invoke(level, tag, messageBuilder(), e) -inline fun Logger.log(level: LogLevel, message: String, e: Throwable? = null) = log(level, null, e) { message } -inline fun Logger.log(level: LogLevel, tag: String, message: String, e: Throwable? = null) = log(level, tag, e) { message } +inline fun KSLog.log(level: LogLevel, tag: String? = null, e: Throwable? = null, messageBuilder: () -> String) = performLog(level, tag, messageBuilder(), e) +inline fun KSLog.log(level: LogLevel, message: String, e: Throwable? = null) = log(level, null, e) { message } +inline fun KSLog.log(level: LogLevel, tag: String, message: String, e: Throwable? = null) = log(level, tag, e) { message } -inline fun Logger.verbose(tag: String? = null, e: Throwable? = null, messageBuilder: () -> String) = log(LogLevel.VERBOSE, tag, e, messageBuilder) -inline fun Logger.verbose(message: String, e: Throwable? = null) = log(LogLevel.VERBOSE, message, e) -inline fun Logger.verbose(tag: String, message: String, e: Throwable? = null) = log(LogLevel.VERBOSE, tag, message, e) +inline fun KSLog.verbose(tag: String? = null, e: Throwable? = null, messageBuilder: () -> String) = log(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 Logger.info(tag: String? = null, e: Throwable? = null, messageBuilder: () -> String) = log(LogLevel.INFO, tag, e, messageBuilder) -inline fun Logger.info(message: String, e: Throwable? = null) = log(LogLevel.INFO, message, e) -inline fun Logger.info(tag: String, message: String, e: Throwable? = null) = log(LogLevel.INFO, tag, message, e) +inline fun KSLog.info(tag: String? = null, e: Throwable? = null, messageBuilder: () -> String) = log(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 Logger.warning(tag: String? = null, e: Throwable? = null, messageBuilder: () -> String) = log(LogLevel.WARNING, tag, e, messageBuilder) -inline fun Logger.warning(message: String, e: Throwable? = null) = log(LogLevel.WARNING, message, e) -inline fun Logger.warning(tag: String, message: String, e: Throwable? = null) = log(LogLevel.WARNING, tag, message, e) +inline fun KSLog.warning(tag: String? = null, e: Throwable? = null, messageBuilder: () -> String) = log(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 Logger.error(tag: String? = null, e: Throwable? = null, messageBuilder: () -> String) = log(LogLevel.ERROR, tag, e, messageBuilder) -inline fun Logger.error(message: String, e: Throwable? = null) = log(LogLevel.ERROR, message, e) -inline fun Logger.error(tag: String, message: String, e: Throwable? = null) = log(LogLevel.ERROR, tag, message, e) +inline fun KSLog.error(tag: String? = null, e: Throwable? = null, messageBuilder: () -> String) = log(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 Logger.assert(tag: String? = null, e: Throwable? = null, messageBuilder: () -> String) = log(LogLevel.ASSERT, tag, e, messageBuilder) -inline fun Logger.assert(message: String, e: Throwable? = null) = log(LogLevel.ASSERT, message, e) -inline fun Logger.assert(tag: String, message: String, e: Throwable? = null) = log(LogLevel.ASSERT, tag, message, e) +inline fun KSLog.assert(tag: String? = null, e: Throwable? = null, messageBuilder: () -> String) = log(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 Logger.debug(tag: String? = null, e: Throwable? = null, messageBuilder: () -> String) = log(LogLevel.DEBUG, tag, e, messageBuilder) -inline fun Logger.debug(message: String, e: Throwable? = null) = log(LogLevel.DEBUG, message, e) -inline fun Logger.debug(tag: String, message: String, e: Throwable? = null) = log(LogLevel.DEBUG, tag, message, e) +inline fun KSLog.debug(tag: String? = null, e: Throwable? = null, 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 Logger.l(level: LogLevel, tag: String? = null, e: Throwable? = null, messageBuilder: () -> String) = log(level, tag, e, messageBuilder) -inline fun Logger.l(level: LogLevel, message: String, e: Throwable? = null) = log(level, message, e) -inline fun Logger.l(level: LogLevel, tag: String, message: String, e: Throwable? = null) = log(level, tag, message, e) +inline fun KSLog.l(level: LogLevel, tag: String? = null, e: Throwable? = null, messageBuilder: () -> String) = log(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 Logger.v(tag: String? = null, e: Throwable? = null, messageBuilder: () -> String) = verbose(tag, e, messageBuilder) -inline fun Logger.v(message: String, e: Throwable? = null) = verbose(message, e) -inline fun Logger.v(tag: String, message: String, e: Throwable? = null) = verbose(tag, message, e) +inline fun KSLog.v(tag: String? = null, e: Throwable? = null, messageBuilder: () -> String) = verbose(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 Logger.i(tag: String? = null, e: Throwable? = null, messageBuilder: () -> String) = info(tag, e, messageBuilder) -inline fun Logger.i(message: String, e: Throwable? = null) = info(message, e) -inline fun Logger.i(tag: String, message: String, e: Throwable? = null) = info(tag, message, e) +inline fun KSLog.i(tag: String? = null, e: Throwable? = null, messageBuilder: () -> String) = info(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 Logger.w(tag: String? = null, e: Throwable? = null, messageBuilder: () -> String) = warning(tag, e, messageBuilder) -inline fun Logger.w(message: String, e: Throwable? = null) = warning(message, e) -inline fun Logger.w(tag: String, message: String, e: Throwable? = null) = warning(tag, message, e) +inline fun KSLog.w(tag: String? = null, e: Throwable? = null, messageBuilder: () -> String) = warning(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 Logger.e(tag: String? = null, e: Throwable? = null, messageBuilder: () -> String) = error(tag, e, messageBuilder) -inline fun Logger.e(message: String, e: Throwable? = null) = error(message, e) -inline fun Logger.e(tag: String, message: String, e: Throwable? = null) = error(tag, message, e) +inline fun KSLog.e(tag: String? = null, e: Throwable? = null, messageBuilder: () -> String) = error(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 Logger.wtf(tag: String? = null, e: Throwable? = null, messageBuilder: () -> String) = assert(tag, e, messageBuilder) -inline fun Logger.wtf(message: String, e: Throwable? = null) = assert(message, e) -inline fun Logger.wtf(tag: String, message: String, e: Throwable? = null) = assert(tag, message, e) +inline fun KSLog.wtf(tag: String? = null, e: Throwable? = null, messageBuilder: () -> String) = assert(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 Logger.d(tag: String? = null, e: Throwable? = null, messageBuilder: () -> String) = debug(tag, e, messageBuilder) -inline fun Logger.d(message: String, e: Throwable? = null) = debug(message, e) -inline fun Logger.d(tag: String, message: String, e: Throwable? = null) = debug(tag, message, e) +inline fun KSLog.d(tag: String? = null, e: Throwable? = null, 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 new file mode 100644 index 0000000..4a3741b --- /dev/null +++ b/src/commonMain/kotlin/KSLog.kt @@ -0,0 +1,50 @@ +package dev.inmo.kslog.common + + +enum class LogLevel { + VERBOSE, + INFO, + WARNING, + ERROR, + ASSERT, + DEBUG +} + +interface KSLog { + fun performLog(level: LogLevel, tag: String?, message: String, throwable: Throwable?) + companion object : KSLog { + private var defaultLogger: KSLog? = null + var DEFAULT: KSLog + get() { + return defaultLogger ?: KSLog("app").also { + defaultLogger = it + } + } + set(value) { + defaultLogger = value + } + override fun performLog(level: LogLevel, tag: String?, message: String, throwable: Throwable?) = DEFAULT.performLog(level, tag, message, throwable) + } +} + + +operator fun KSLog.invoke(performLogCallback: (level: LogLevel, tag: String?, message: String, throwable: Throwable?) -> Unit) = CallbackKSLog(performLogCallback) + +class CallbackKSLog( + private val performLogCallback: (level: LogLevel, tag: String?, message: String, throwable: Throwable?) -> Unit +) : KSLog { + override fun performLog(level: LogLevel, tag: String?, message: String, throwable: Throwable?) = performLogCallback(level, tag, message, throwable) +} + + +expect fun KSLog( + defaultTag: String, + filter: (l: LogLevel, t: String, m: String, Throwable?) -> Boolean +) : KSLog + +fun KSLog( + defaultTag: String, + levels: Set = LogLevel.values().toSet() +): KSLog = KSLog (defaultTag) { l, _, _, _ -> + l in levels +} diff --git a/src/commonMain/kotlin/Logger.kt b/src/commonMain/kotlin/Logger.kt deleted file mode 100644 index 15dfd4a..0000000 --- a/src/commonMain/kotlin/Logger.kt +++ /dev/null @@ -1,40 +0,0 @@ -package dev.inmo.kslog.common - - -enum class LogLevel { - VERBOSE, - INFO, - WARNING, - ERROR, - ASSERT, - DEBUG -} - -interface Logger { - operator fun invoke(level: LogLevel, tag: String?, message: String, throwable: Throwable?) - companion object : Logger { - var DEFAULT: Logger = Logger("app") - override fun invoke(level: LogLevel, tag: String?, message: String, throwable: Throwable?) = DEFAULT(level, tag, message, throwable) - - operator fun invoke (log: (level: LogLevel, message: String, tag: String?, throwable: Throwable?) -> Unit) = CallbackLogger(log) - } -} - -class CallbackLogger( - private val logger: (level: LogLevel, message: String, tag: String?, throwable: Throwable?) -> Unit -) : Logger { - override fun invoke(level: LogLevel, tag: String?, message: String, throwable: Throwable?) = logger(level, message, tag, throwable) -} - - -expect fun Logger( - defaultTag: String, - filter: (l: LogLevel, m: String, t: String, Throwable?) -> Boolean -) : Logger - -fun Logger( - defaultTag: String, - levels: Set = LogLevel.values().toSet() -): Logger = Logger (defaultTag) { l, _, _, _ -> - l in levels -} diff --git a/src/jsMain/kotlin/ActualLogger.kt b/src/jsMain/kotlin/ActualLogger.kt index 358e16d..e80dd52 100644 --- a/src/jsMain/kotlin/ActualLogger.kt +++ b/src/jsMain/kotlin/ActualLogger.kt @@ -1,11 +1,11 @@ package dev.inmo.kslog.common -fun Logger( - messageFormatter: (l: LogLevel, m: String, t: String?, Throwable?) -> String, - filter: (l: LogLevel, m: String, t: String?, Throwable?) -> Boolean -) = Logger { l, m, t, e -> - if (!filter(l, m, t, e)) return@Logger - val text = messageFormatter(l,m,t,e) +fun KSLog( + messageFormatter: (l: LogLevel, t: String?, m: String, Throwable?) -> String, + 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) when (l) { LogLevel.VERBOSE, LogLevel.INFO -> console.info(text, e) @@ -14,15 +14,14 @@ fun Logger( LogLevel.ASSERT -> console.error(text, e) LogLevel.DEBUG -> console.log(text, e) } - } -actual fun Logger( +actual fun KSLog( defaultTag: String, - filter: (l: LogLevel, m: String, t: String, Throwable?) -> Boolean -): Logger { - return Logger( - { l, m, t, e -> "[$l] ${t ?: defaultTag} - $m" }, - { l, m, t, e -> filter(l, m, t ?: defaultTag, e) } + filter: (l: LogLevel, t: String, m: String, Throwable?) -> Boolean +): KSLog { + return KSLog( + { l, t, m, _ -> "[$l] ${t ?: defaultTag} - $m" }, + { l, t, m, e -> filter(l, t ?: defaultTag, m, e) } ) } diff --git a/src/jvmMain/kotlin/ActualLogger.kt b/src/jvmMain/kotlin/ActualLogger.kt index e404cd3..1f3e556 100644 --- a/src/jvmMain/kotlin/ActualLogger.kt +++ b/src/jvmMain/kotlin/ActualLogger.kt @@ -2,25 +2,45 @@ package dev.inmo.kslog.common import java.util.logging.Level -actual fun Logger( - defaultTag: String, - filter: (l: LogLevel, m: String, t: String, Throwable?) -> Boolean -): Logger { - val logger = java.util.logging.Logger.getLogger(defaultTag) - return Logger { l, m, t, e -> - if (!filter(l, m, t ?: defaultTag, e)) return@Logger - val currentLogger = t ?.let { java.util.logging.Logger.getLogger(t) } ?: logger - currentLogger.log( - when(l) { - LogLevel.VERBOSE -> Level.FINEST - LogLevel.INFO -> Level.INFO - LogLevel.WARNING -> Level.WARNING - LogLevel.ERROR -> Level.SEVERE - LogLevel.ASSERT -> Level.OFF - LogLevel.DEBUG -> Level.ALL - }, - m, - e - ) - } +fun KSLog( + logger: java.util.logging.Logger, + filter: (l: LogLevel, t: String?, m: String, Throwable?) -> Boolean, + messageFormatter: (l: LogLevel, t: String?, m: String, Throwable?) -> String +) = KSLog { l, t, m, e -> + if (!filter(l, t, m, e)) return@KSLog + val text = messageFormatter(l,t,m,e) + logger.log( + when(l) { + LogLevel.VERBOSE -> Level.FINEST + LogLevel.INFO -> Level.INFO + LogLevel.WARNING -> Level.WARNING + LogLevel.ERROR -> Level.SEVERE + LogLevel.ASSERT -> Level.OFF + LogLevel.DEBUG -> Level.ALL + }, + text, + e + ) +} + +fun KSLog( + defaultTag: String, + logger: java.util.logging.Logger, + filter: (l: LogLevel, t: String, m: String, Throwable?) -> Boolean = { _, _, _, _ -> true } +): KSLog { + return KSLog( + logger, + { l, t, m, e -> filter(l, t ?: defaultTag, m, e) } + ) { _, t, m, _ -> "${t ?: defaultTag} - $m" } +} + +actual fun KSLog( + 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) } + ) } diff --git a/src/main/kotlin/ActualLogger.kt b/src/main/kotlin/ActualLogger.kt index 67d1e0d..e09626c 100644 --- a/src/main/kotlin/ActualLogger.kt +++ b/src/main/kotlin/ActualLogger.kt @@ -2,13 +2,13 @@ package dev.inmo.kslog.common import android.util.Log -actual fun Logger( +actual fun KSLog( defaultTag: String, - filter: (l: LogLevel, m: String, t: String, Throwable?) -> Boolean -): Logger { - return Logger { l, m, t, e -> + filter: (l: LogLevel, t: String, m: String, Throwable?) -> Boolean +): KSLog { + return KSLog { l, t, m, e -> val tag = t ?: defaultTag - if (!filter(l, m, t ?: defaultTag, e)) return@Logger + 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)