final fixes before publish

This commit is contained in:
InsanusMokrassar 2022-06-07 21:42:19 +06:00
parent aad75a66b7
commit 7fec433344
7 changed files with 158 additions and 121 deletions

View File

@ -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)
}

View File

@ -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)

View File

@ -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> = LogLevel.values().toSet()
): KSLog = KSLog (defaultTag) { l, _, _, _ ->
l in levels
}

View File

@ -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> = LogLevel.values().toSet()
): Logger = Logger (defaultTag) { l, _, _, _ ->
l in levels
}

View File

@ -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) }
)
}

View File

@ -2,15 +2,14 @@ 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(
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
@ -19,8 +18,29 @@ actual fun Logger(
LogLevel.ASSERT -> Level.OFF
LogLevel.DEBUG -> Level.ALL
},
m,
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) }
)
}

View File

@ -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)