From 10576d32f666e1be44caba1f22158c35a9e11393 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Thu, 9 Jun 2022 09:56:27 +0600 Subject: [PATCH] start implement lazy messages api --- src/commonMain/kotlin/CallbackKSLog.kt | 7 ++++++ src/commonMain/kotlin/DefaultKSLog.kt | 26 +++++++++++++++++++++ src/commonMain/kotlin/Extensions.kt | 32 +++++++++++++------------- src/commonMain/kotlin/KSLog.kt | 19 ++++++++++----- 4 files changed, 62 insertions(+), 22 deletions(-) create mode 100644 src/commonMain/kotlin/CallbackKSLog.kt create mode 100644 src/commonMain/kotlin/DefaultKSLog.kt diff --git a/src/commonMain/kotlin/CallbackKSLog.kt b/src/commonMain/kotlin/CallbackKSLog.kt new file mode 100644 index 0000000..13c496f --- /dev/null +++ b/src/commonMain/kotlin/CallbackKSLog.kt @@ -0,0 +1,7 @@ +package dev.inmo.kslog.common + +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) +} diff --git a/src/commonMain/kotlin/DefaultKSLog.kt b/src/commonMain/kotlin/DefaultKSLog.kt new file mode 100644 index 0000000..5fbd9f5 --- /dev/null +++ b/src/commonMain/kotlin/DefaultKSLog.kt @@ -0,0 +1,26 @@ +package dev.inmo.kslog.common + +class DefaultKSLog( + private val defaultTag: String, + private val filter: MessageFilter = { _, _, _, _ -> true }, + private val messageFormatter: MessageFormatter = defaultMessageFormatter, + private val logging: (level: LogLevel, tag: String, message: String, throwable: Throwable?) -> Unit +) : KSLog { + override fun performLog(level: LogLevel, tag: String?, message: String, throwable: Throwable?) { + TODO("Not yet implemented") + } + + override fun performLog(level: LogLevel, tag: String?, throwable: Throwable?, messageBuilder: () -> String) { + super.performLog(level, tag, throwable, messageBuilder) + } + + override suspend fun performLog( + level: LogLevel, + tag: String?, + throwable: Throwable?, + messageBuilder: suspend () -> String + ) { + if (filter(level, tag, )) + super.performLog(level, tag, throwable, messageBuilder) + } +} diff --git a/src/commonMain/kotlin/Extensions.kt b/src/commonMain/kotlin/Extensions.kt index abdf527..ac2fbd1 100644 --- a/src/commonMain/kotlin/Extensions.kt +++ b/src/commonMain/kotlin/Extensions.kt @@ -2,72 +2,72 @@ package dev.inmo.kslog.common -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 KSLog.log(level: LogLevel, tag: String? = null, e: Throwable? = null, noinline messageBuilder: () -> String) = performLog(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.verbose(tag: String? = null, e: Throwable? = null, messageBuilder: () -> String) = log(LogLevel.VERBOSE, tag, e, messageBuilder) +inline fun KSLog.verbose(tag: String? = null, e: Throwable? = null, noinline 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 KSLog.info(tag: String? = null, e: Throwable? = null, messageBuilder: () -> String) = log(LogLevel.INFO, tag, e, messageBuilder) +inline fun KSLog.info(tag: String? = null, e: Throwable? = null, noinline 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 KSLog.warning(tag: String? = null, e: Throwable? = null, messageBuilder: () -> String) = log(LogLevel.WARNING, tag, e, messageBuilder) +inline fun KSLog.warning(tag: String? = null, e: Throwable? = null, noinline 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 KSLog.error(tag: String? = null, e: Throwable? = null, messageBuilder: () -> String) = log(LogLevel.ERROR, tag, e, messageBuilder) +inline fun KSLog.error(tag: String? = null, e: Throwable? = null, noinline 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 KSLog.assert(tag: String? = null, e: Throwable? = null, messageBuilder: () -> String) = log(LogLevel.ASSERT, tag, e, messageBuilder) +inline fun KSLog.assert(tag: String? = null, e: Throwable? = null, noinline 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 KSLog.debug(tag: String? = null, e: Throwable? = null, messageBuilder: () -> String) = log(LogLevel.DEBUG, tag, e, messageBuilder) +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, messageBuilder: () -> String) = log(level, tag, e, messageBuilder) +inline fun KSLog.l(level: LogLevel, tag: String? = null, e: Throwable? = null, noinline 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 KSLog.v(tag: String? = null, e: Throwable? = null, messageBuilder: () -> String) = verbose(tag, e, messageBuilder) +inline fun KSLog.v(tag: String? = null, e: Throwable? = null, noinline 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 KSLog.i(tag: String? = null, e: Throwable? = null, messageBuilder: () -> String) = info(tag, e, messageBuilder) +inline fun KSLog.i(tag: String? = null, e: Throwable? = null, noinline 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 KSLog.w(tag: String? = null, e: Throwable? = null, messageBuilder: () -> String) = warning(tag, e, messageBuilder) +inline fun KSLog.w(tag: String? = null, e: Throwable? = null, noinline 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 KSLog.e(tag: String? = null, e: Throwable? = null, messageBuilder: () -> String) = error(tag, e, messageBuilder) +inline fun KSLog.e(tag: String? = null, e: Throwable? = null, noinline 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 KSLog.wtf(tag: String? = null, e: Throwable? = null, messageBuilder: () -> String) = assert(tag, e, messageBuilder) +inline fun KSLog.wtf(tag: String? = null, e: Throwable? = null, noinline 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 KSLog.d(tag: String? = null, e: Throwable? = null, messageBuilder: () -> String) = debug(tag, e, messageBuilder) +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 000ebcd..182283e 100644 --- a/src/commonMain/kotlin/KSLog.kt +++ b/src/commonMain/kotlin/KSLog.kt @@ -12,6 +12,19 @@ enum class LogLevel { interface KSLog { fun performLog(level: LogLevel, tag: String?, message: String, throwable: Throwable?) + fun performLog(level: LogLevel, message: String, throwable: Throwable?) = performLog(level, null, message, throwable) + fun performLog( + level: LogLevel, + tag: String?, + throwable: Throwable?, + messageBuilder: () -> String + ) = performLog(level, tag, messageBuilder(), throwable) + suspend fun performLog( + level: LogLevel, + tag: String?, + throwable: Throwable?, + messageBuilder: suspend () -> String + ) = performLog(level, tag, messageBuilder(), throwable) companion object : KSLog { private var defaultLogger: KSLog? = null var default: KSLog @@ -30,12 +43,6 @@ interface KSLog { 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: MessageFilter = { _, _, _, _ -> true },