Merge pull request #1 from InsanusMokrassar/0.0.1

0.0.1
This commit is contained in:
InsanusMokrassar 2022-06-07 22:06:42 +06:00 committed by GitHub
commit c51d4cd89b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 177 additions and 147 deletions

View File

@ -33,7 +33,7 @@ repositories {
kotlin {
jvm()
js (IR) {
js(IR) {
browser()
nodejs()
}
@ -47,30 +47,6 @@ kotlin {
implementation libs.kotlin
}
}
commonTest {
dependencies {
implementation libs.kotlin.test.common
implementation libs.kotlin.test.annotations.common
}
}
jvmTest {
dependencies {
implementation libs.kotlin.test.junit
}
}
jsTest {
dependencies {
implementation libs.kotlin.test.js
implementation libs.kotlin.test.junit
}
}
androidTest {
dependencies {
implementation libs.kotlin.test.junit
implementation libs.android.test.junit
implementation libs.android.test.espresso.core
}
}
}
}

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

@ -1,66 +1,73 @@
@file:Suppress("NOTHING_TO_INLINE")
package dev.inmo.kslog.common
inline fun Logger.log(level: LogLevel, tag: String? = null, e: Throwable? = null, messageBuilder: () -> String) = log(level, messageBuilder(), tag, 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.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.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 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,57 @@
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>
): KSLog = KSLog (defaultTag) { l, _, _, _ ->
l in levels
}
fun KSLog(
defaultTag: String,
minLoggingLevel: LogLevel = LogLevel.VERBOSE
): KSLog = KSLog (defaultTag) { l, _, _, _ ->
minLoggingLevel.ordinal <= l.ordinal
}

View File

@ -1,40 +0,0 @@
package dev.inmo.kslog.common
enum class LogLevel {
VERBOSE,
INFO,
WARNING,
ERROR,
ASSERT,
DEBUG
}
interface Logger {
fun log (level: LogLevel, message: String, tag: String? = null, throwable: Throwable? = null)
companion object : Logger {
var DEFAULT: Logger = Logger("app")
override fun log(level: LogLevel, message: String, tag: String?, throwable: Throwable?) = DEFAULT.log(level, message, tag, 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 log(level: LogLevel, message: String, tag: 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,28 +1,30 @@
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)
val args = e ?.let {
arrayOf(text, e)
} ?: arrayOf(text)
when (l) {
LogLevel.VERBOSE,
LogLevel.INFO -> console.info(text, e)
LogLevel.WARNING -> console.warn(text, e)
LogLevel.INFO -> console.info(*args)
LogLevel.WARNING -> console.warn(*args)
LogLevel.ERROR,
LogLevel.ASSERT -> console.error(text, e)
LogLevel.DEBUG -> console.log(text, e)
LogLevel.ASSERT -> console.error(*args)
LogLevel.DEBUG -> console.log(*args)
}
}
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,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) }
)
}

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)