[core] Add UtilsLogger in mirai-core-utils module to bridge logging,

also added delegations in mirai-core and mirai-core-api.
This commit is contained in:
Him188 2023-04-22 12:34:21 +01:00
parent 26ea98db6a
commit 36d90c57fe
3 changed files with 249 additions and 0 deletions

View File

@ -0,0 +1,47 @@
/*
* Copyright 2019-2023 Mamoe Technologies and contributors.
*
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
*
* https://github.com/mamoe/mirai/blob/dev/LICENSE
*/
package net.mamoe.mirai.utils
internal fun MiraiLogger.asUtilsLogger(): UtilsLogger = MiraiUtilsLogger(this)
internal class MiraiUtilsLogger(
private val miraiLogger: MiraiLogger,
) : UtilsLogger {
override val isVerboseEnabled: Boolean
get() = miraiLogger.isVerboseEnabled
override val isDebugEnabled: Boolean
get() = miraiLogger.isDebugEnabled
override val isInfoEnabled: Boolean
get() = miraiLogger.isInfoEnabled
override val isWarningEnabled: Boolean
get() = miraiLogger.isWarningEnabled
override val isErrorEnabled: Boolean
get() = miraiLogger.isErrorEnabled
override fun verbose(message: String?, e: Throwable?) {
miraiLogger.verbose(message, e)
}
override fun debug(message: String?, e: Throwable?) {
miraiLogger.debug(message, e)
}
override fun info(message: String?, e: Throwable?) {
miraiLogger.info(message, e)
}
override fun warning(message: String?, e: Throwable?) {
miraiLogger.warning(message, e)
}
override fun error(message: String?, e: Throwable?) {
miraiLogger.error(message, e)
}
}

View File

@ -0,0 +1,184 @@
/*
* Copyright 2019-2023 Mamoe Technologies and contributors.
*
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
*
* https://github.com/mamoe/mirai/blob/dev/LICENSE
*/
package net.mamoe.mirai.utils
/**
* Mirror of `MiraiLogger`, to be used in utils module.
*/
public interface UtilsLogger {
/**
* VERBOSE 级别的日志启用时返回 `true`.
*/
public val isVerboseEnabled: Boolean
/**
* DEBUG 级别的日志启用时返回 `true`
*/
public val isDebugEnabled: Boolean
/**
* INFO 级别的日志启用时返回 `true`
*/
public val isInfoEnabled: Boolean
/**
* WARNING 级别的日志启用时返回 `true`
*/
public val isWarningEnabled: Boolean
/**
* ERROR 级别的日志启用时返回 `true`
*/
public val isErrorEnabled: Boolean
/**
* 记录一个 `verbose` 级别的日志.
* 无关紧要的, 经常大量输出的日志应使用它.
*/
public fun verbose(message: String?, e: Throwable? = null)
/**
* 记录一个 _调试_ 级别的日志.
*/
public fun debug(message: String?, e: Throwable? = null)
/**
* 记录一个 _信息_ 级别的日志.
*/
public fun info(message: String?, e: Throwable? = null)
/**
* 记录一个 _警告_ 级别的日志.
*/
public fun warning(message: String?, e: Throwable? = null)
/**
* 记录一个 _错误_ 级别的日志.
*/
public fun error(message: String?, e: Throwable? = null)
public companion object {
@OptIn(TestOnly::class)
private val noop: UtilsLogger by lazy {
SimpleUtilsLogger().apply {
isDebugEnabled = false
isErrorEnabled = false
isInfoEnabled = false
isWarningEnabled = false
isVerboseEnabled = false
}
}
public fun noop(): UtilsLogger = noop
}
}
public fun UtilsLogger.info(e: Throwable?) {
info(null, e)
}
public fun UtilsLogger.error(e: Throwable?) {
error(null, e)
}
public fun UtilsLogger.warning(e: Throwable?) {
warning(null, e)
}
public fun UtilsLogger.debug(e: Throwable?) {
debug(null, e)
}
public fun UtilsLogger.verbose(e: Throwable?) {
verbose(null, e)
}
public inline fun UtilsLogger.verbose(message: () -> String) {
if (isVerboseEnabled) verbose(message())
}
public inline fun UtilsLogger.verbose(message: () -> String, e: Throwable?) {
if (isVerboseEnabled) verbose(message(), e)
}
public inline fun UtilsLogger.debug(message: () -> String?) {
if (isDebugEnabled) debug(message())
}
public inline fun UtilsLogger.debug(message: () -> String?, e: Throwable?) {
if (isDebugEnabled) debug(message(), e)
}
public inline fun UtilsLogger.info(message: () -> String?) {
if (isInfoEnabled) info(message())
}
public inline fun UtilsLogger.info(message: () -> String?, e: Throwable?) {
if (isInfoEnabled) info(message(), e)
}
public inline fun UtilsLogger.warning(message: () -> String?) {
if (isWarningEnabled) warning(message())
}
public inline fun UtilsLogger.warning(message: () -> String?, e: Throwable?) {
if (isWarningEnabled) warning(message(), e)
}
public inline fun UtilsLogger.error(message: () -> String?) {
if (isErrorEnabled) error(message())
}
public inline fun UtilsLogger.error(message: () -> String?, e: Throwable?) {
if (isErrorEnabled) error(message(), e)
}
@TestOnly
public open class SimpleUtilsLogger
@TestOnly
public constructor() : UtilsLogger {
override var isVerboseEnabled: Boolean = true
override var isDebugEnabled: Boolean = true
override var isInfoEnabled: Boolean = true
override var isWarningEnabled: Boolean = true
override var isErrorEnabled: Boolean = true
override fun verbose(message: String?, e: Throwable?) {
if (!isVerboseEnabled) return
println("[V] $message")
e?.printStackTrace()
}
override fun debug(message: String?, e: Throwable?) {
if (!isDebugEnabled) return
println("[D] $message")
e?.printStackTrace()
}
override fun info(message: String?, e: Throwable?) {
if (!isInfoEnabled) return
println("[I] $message")
e?.printStackTrace()
}
override fun warning(message: String?, e: Throwable?) {
if (!isWarningEnabled) return
println("[W] $message")
e?.printStackTrace()
}
override fun error(message: String?, e: Throwable?) {
if (!isErrorEnabled) return
println("[E] $message")
e?.printStackTrace()
}
}

View File

@ -0,0 +1,18 @@
/*
* Copyright 2019-2023 Mamoe Technologies and contributors.
*
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
*
* https://github.com/mamoe/mirai/blob/dev/LICENSE
*/
@file:Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE")
package net.mamoe.mirai.internal.utils
import net.mamoe.mirai.utils.MiraiLogger
import net.mamoe.mirai.utils.UtilsLogger
import net.mamoe.mirai.utils.asUtilsLogger as asUtilsLogger1
internal fun MiraiLogger.asUtilsLogger(): UtilsLogger = asUtilsLogger1()