From 194fc4425c1f2c7ea5c268fc86e01f72a1e60de1 Mon Sep 17 00:00:00 2001 From: fankesyooni Date: Wed, 27 Sep 2023 19:32:09 +0800 Subject: [PATCH] refactor: optimize YukiLog --- .../yukireflection/utils/debug/YukiLog.kt | 56 +++++++++++-------- 1 file changed, 33 insertions(+), 23 deletions(-) diff --git a/yukireflection-core/src/main/java/com/highcapable/yukireflection/utils/debug/YukiLog.kt b/yukireflection-core/src/main/java/com/highcapable/yukireflection/utils/debug/YukiLog.kt index 7b4343c..3f1b3cd 100644 --- a/yukireflection-core/src/main/java/com/highcapable/yukireflection/utils/debug/YukiLog.kt +++ b/yukireflection-core/src/main/java/com/highcapable/yukireflection/utils/debug/YukiLog.kt @@ -25,6 +25,8 @@ * * This file is created by fankes on 2023/9/23. */ +@file:Suppress("unused") + package com.highcapable.yukireflection.utils.debug import android.util.Log @@ -37,12 +39,6 @@ import com.highcapable.yukireflection.utils.factory.dumpToString */ internal object YukiLog { - /** - * 是否为 Android 模式 - * @return [Boolean] - */ - private val isAndroidMode get() = "android.util.Log".hasClass() - /** * 是否启用 * @return [Boolean] @@ -61,9 +57,7 @@ internal object YukiLog { * @param e 异常堆栈 - 默认空 */ internal fun debug(msg: String, e: Throwable? = null) { - if (isEnable.not()) return - if (isAndroidMode) Log.d(tag, msg, e) - else log(msg = "[$tag] $msg", e) + if (isEnable) log(Type.DEBUG, msg, e) } /** @@ -72,9 +66,7 @@ internal object YukiLog { * @param e 异常堆栈 - 默认空 */ internal fun info(msg: String, e: Throwable? = null) { - if (isEnable.not()) return - if (isAndroidMode) Log.i(tag, msg, e) - else log(msg = "[$tag] $msg", e, color = "38;5;10") + if (isEnable) log(Type.INFO, msg, e) } /** @@ -83,9 +75,7 @@ internal object YukiLog { * @param e 异常堆栈 - 默认空 */ internal fun warn(msg: String, e: Throwable? = null) { - if (isEnable.not()) return - if (isAndroidMode) Log.w(tag, msg, e) - else log(msg = "[$tag] $msg", e, color = "33") + if (isEnable) log(Type.WARN, msg, e) } /** @@ -94,24 +84,44 @@ internal object YukiLog { * @param e 异常堆栈 - 默认空 */ internal fun error(msg: String, e: Throwable? = null) { - if (isEnable.not()) return - if (isAndroidMode) Log.e(tag, msg, e) - else log(msg = "[$tag] $msg", e, color = "31") + if (isEnable) log(Type.ERROR, msg, e) } /** * 打印 Log + * @param type 类型 * @param msg 消息内容 * @param e 异常堆栈 - 默认空 - * @param color 颜色代码 - 默认无颜色 */ - private fun log(msg: String, e: Throwable? = null, color: String = "") { + private fun log(type: Type, msg: String, e: Throwable? = null) { + val isAndroid = "android.util.Log".hasClass() + val mixedContent = "[$tag][${type.alias}] $msg" + /** * 打印 Log * @param msg 消息内容 */ - fun innerLog(msg: String) = if (color.isBlank()) println(msg) else println("\u001B[${color}m$msg\u001B[0m") - innerLog(msg) - e?.also { innerLog(it.dumpToString()) } + fun innerLog(msg: String) { + when { + isAndroid -> when (type) { + Type.DEBUG -> Log.d(tag, msg, e) + Type.INFO -> Log.i(tag, msg, e) + Type.WARN -> Log.w(tag, msg, e) + Type.ERROR -> Log.e(tag, msg, e) + } + type.color.isBlank() -> println(mixedContent) + else -> println("\u001B[${type.color}m$mixedContent\u001B[0m") + } + }; innerLog(msg) + if (isAndroid.not()) e?.also { innerLog(it.dumpToString()) } + } + + /** + * Log 类型定义类 + * @param alias 类型别名 + * @param color 颜色代码 - 默认无颜色 + */ + private enum class Type(val alias: String, val color: String = "") { + DEBUG("D"), INFO("I", "38;5;10"), WARN("W", "33"), ERROR("E", "31") } } \ No newline at end of file