rewrite of API with expect-actual and removing of redundant platform-specific factory names

This commit is contained in:
InsanusMokrassar 2022-06-08 22:51:03 +06:00
parent f5cc748ac5
commit be047cb8b5
7 changed files with 72 additions and 106 deletions

View File

@ -1,5 +1,9 @@
# Changelog # Changelog
## 0.2.0
* Full rewrite of API
## 0.1.1 ## 0.1.1
* Changes in `JVM` levels * Changes in `JVM` levels

View File

@ -9,5 +9,5 @@ android.enableJetifier=true
# Project data # Project data
group=dev.inmo group=dev.inmo
version=0.1.1 version=0.2.0
android_code_version=5 android_code_version=6

View File

@ -36,36 +36,39 @@ class CallbackKSLog(
override fun performLog(level: LogLevel, tag: String?, message: String, throwable: Throwable?) = performLogCallback(level, tag, message, throwable) 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( fun KSLog(
defaultTag: String, defaultTag: String,
filter: (l: LogLevel, t: String, m: String, Throwable?) -> Boolean levels: Iterable<LogLevel>,
) : KSLog = KSLog { l, t, m, e -> messageFormatter: MessageFormatter = defaultMessageFormatter
if (filter(l, t ?: defaultTag, m, e)) {
KSLog.default.performLog(l, t ?: defaultTag, m, e)
}
}
fun KSLog(
defaultTag: String,
levels: Iterable<LogLevel>
): KSLog { ): KSLog {
val levels = levels.toSet() val levels = levels.toSet()
return KSLog (defaultTag) { l, _, _, _ -> return KSLog (defaultTag, { l, _, _, _ ->
l in levels l in levels
} }, messageFormatter)
} }
fun KSLog( fun KSLog(
defaultTag: String, defaultTag: String,
firstLevel: LogLevel, firstLevel: LogLevel,
secondLevel: LogLevel, secondLevel: LogLevel,
vararg otherLevels: LogLevel vararg otherLevels: LogLevel,
): KSLog = KSLog(defaultTag, setOf(firstLevel, secondLevel, *otherLevels)) messageFormatter: MessageFormatter = defaultMessageFormatter,
): KSLog = KSLog(defaultTag, setOf(firstLevel, secondLevel, *otherLevels), messageFormatter)
fun KSLog( fun KSLog(
defaultTag: String, defaultTag: String,
minLoggingLevel: LogLevel = LogLevel.values().first() minLoggingLevel: LogLevel,
): KSLog = KSLog (defaultTag) { l, _, _, _ -> messageFormatter: MessageFormatter = defaultMessageFormatter,
): KSLog = KSLog (
defaultTag,
{ l, _, _, _ ->
minLoggingLevel.ordinal <= l.ordinal minLoggingLevel.ordinal <= l.ordinal
} },
messageFormatter
)

View File

@ -1,5 +1,6 @@
package dev.inmo.kslog.common package dev.inmo.kslog.common
typealias MessageFormatter = (l: LogLevel, t: String, m: String, Throwable?) -> String 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" } val defaultMessageFormatter: MessageFormatter = { l, t, m, _ -> "[$l] $t - $m" }

View File

@ -1,10 +1,10 @@
package dev.inmo.kslog.common package dev.inmo.kslog.common
fun KSJSLog( actual fun KSLog(
defaultTag: String, defaultTag: String,
messageFormatter: MessageFormatter = defaultMessageFormatter, filter: MessageFilter,
filter: (l: LogLevel, t: String?, m: String, Throwable?) -> Boolean messageFormatter: MessageFormatter
) = KSLog { l, t, m, e -> ): KSLog = KSLog { l, t, m, e ->
if (!filter(l, t, m, e)) return@KSLog if (!filter(l, t, m, e)) return@KSLog
val text = messageFormatter(l, t?:defaultTag, m, e) val text = messageFormatter(l, t?:defaultTag, m, e)
val args = e ?.let { val args = e ?.let {
@ -19,30 +19,3 @@ fun KSJSLog(
LogLevel.ASSERT -> console.error(*args) LogLevel.ASSERT -> console.error(*args)
} }
} }
fun KSJSLog(
defaultTag: String,
messageFormatter: MessageFormatter = defaultMessageFormatter,
levels: Iterable<LogLevel>
): 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
}

View File

@ -1,12 +1,13 @@
package dev.inmo.kslog.common package dev.inmo.kslog.common
import java.util.logging.Level import java.util.logging.Level
import java.util.logging.Logger
fun KSJVMLog( fun KSLog(
defaultTag: String, defaultTag: String,
logger: java.util.logging.Logger, logger: Logger,
messageFormatter: MessageFormatter = defaultMessageFormatter, filter: MessageFilter = { _, _, _, _ -> true },
filter: (l: LogLevel, t: String?, m: String, Throwable?) -> Boolean messageFormatter: MessageFormatter = defaultMessageFormatter
) = KSLog { l, t, m, e -> ) = KSLog { l, t, m, e ->
if (!filter(l, t, m, e)) return@KSLog if (!filter(l, t, m, e)) return@KSLog
val text = messageFormatter(l,t ?: defaultTag,m,e) val text = messageFormatter(l,t ?: defaultTag,m,e)
@ -24,32 +25,38 @@ fun KSJVMLog(
) )
} }
fun KSJVMLog( fun KSLog(
defaultTag: String, defaultTag: String,
logger: java.util.logging.Logger, logger: Logger,
messageFormatter: MessageFormatter = defaultMessageFormatter, levels: Iterable<LogLevel>,
levels: Iterable<LogLevel> messageFormatter: MessageFormatter = defaultMessageFormatter
): KSLog { ): KSLog {
val levels = levels.toSet() val levels = levels.toSet()
return KSJVMLog (defaultTag, logger, messageFormatter) { l, _, _, _ -> return KSLog (defaultTag, logger, { l, _, _, _ ->
l in levels l in levels
} }, messageFormatter)
} }
fun KSJVMLog( fun KSLog(
defaultTag: String, defaultTag: String,
logger: java.util.logging.Logger, logger: Logger,
messageFormatter: MessageFormatter = defaultMessageFormatter,
firstLevel: LogLevel, firstLevel: LogLevel,
secondLevel: LogLevel, secondLevel: LogLevel,
vararg otherLevels: 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, messageFormatter: MessageFormatter = defaultMessageFormatter,
minLoggingLevel: LogLevel = LogLevel.values().first() ): KSLog = KSLog (defaultTag, logger, setOf(firstLevel, secondLevel, *otherLevels), messageFormatter)
): KSLog = KSJVMLog (defaultTag, logger, messageFormatter) { l, _, _, _ ->
fun KSLog(
defaultTag: String,
logger: Logger,
minLoggingLevel: LogLevel = LogLevel.values().first(),
messageFormatter: MessageFormatter = defaultMessageFormatter
): KSLog = KSLog (defaultTag, logger, { l, _, _, _ ->
minLoggingLevel.ordinal <= l.ordinal minLoggingLevel.ordinal <= l.ordinal
} }, messageFormatter)
actual fun KSLog(
defaultTag: String,
filter: MessageFilter,
messageFormatter: MessageFormatter
): KSLog = KSLog(defaultTag, Logger.getLogger("KSLog"), filter, messageFormatter)

View File

@ -2,42 +2,20 @@ package dev.inmo.kslog.common
import android.util.Log import android.util.Log
fun KSAndroidLog( actual fun KSLog(
defaultTag: String, defaultTag: String,
filter: (l: LogLevel, t: String, m: String, Throwable?) -> Boolean filter: MessageFilter,
) = KSLog { l, t, m, e -> messageFormatter: MessageFormatter
): KSLog = KSLog { l, t, m, e ->
if (!filter(l, t ?: defaultTag, m, e)) return@KSLog if (!filter(l, t ?: defaultTag, m, e)) return@KSLog
val tag = t ?: defaultTag val tag = t ?: defaultTag
val text = messageFormatter(l, tag, m, e)
when(l) { when(l) {
LogLevel.DEBUG -> Log.d(tag, m, e) LogLevel.DEBUG -> Log.d(tag, text, e)
LogLevel.VERBOSE -> Log.v(tag, m, e) LogLevel.VERBOSE -> Log.v(tag, text, e)
LogLevel.INFO -> Log.i(tag, m, e) LogLevel.INFO -> Log.i(tag, text, e)
LogLevel.WARNING -> Log.w(tag, m, e) LogLevel.WARNING -> Log.w(tag, text, e)
LogLevel.ERROR -> Log.e(tag, m, e) LogLevel.ERROR -> Log.e(tag, text, e)
LogLevel.ASSERT -> Log.wtf(tag, m, e) LogLevel.ASSERT -> Log.wtf(tag, text, e)
} }
} }
fun KSAndroidLog(
defaultTag: String,
levels: Iterable<LogLevel>
): 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
}