refactor: merge LoggerFactory to YukiLog

This commit is contained in:
2023-09-23 22:48:35 +08:00
parent 623c55f6fd
commit de4b50944e
9 changed files with 139 additions and 91 deletions

View File

@@ -30,8 +30,7 @@ package com.highcapable.yukireflection.finder.base
import com.highcapable.yukireflection.YukiReflection
import com.highcapable.yukireflection.annotation.YukiPrivateApi
import com.highcapable.yukireflection.log.yLoggerE
import com.highcapable.yukireflection.log.yLoggerI
import com.highcapable.yukireflection.utils.debug.YukiLog
/**
* 这是 [Class] 查找类功能的基本类实现
@@ -64,7 +63,7 @@ abstract class ClassBaseFinder internal constructor(internal open val loaderSet:
* @param msg 调试日志内容
*/
internal fun onDebuggingMsg(msg: String) {
if (YukiReflection.Configs.isDebug) yLoggerI(msg)
if (YukiReflection.Configs.isDebug) YukiLog.debug(msg)
}
/**
@@ -75,7 +74,7 @@ abstract class ClassBaseFinder internal constructor(internal open val loaderSet:
if (isShutErrorPrinting) return
/** 判断是否为 [LOADERSET_IS_NULL] */
if (throwable?.message == LOADERSET_IS_NULL) return
yLoggerE(msg = "NoClassDefFound happend in [$loaderSet]", e = throwable)
YukiLog.error(msg = "NoClassDefFound happend in [$loaderSet]", e = throwable)
}
@YukiPrivateApi

View File

@@ -31,8 +31,7 @@
package com.highcapable.yukireflection.finder.base
import com.highcapable.yukireflection.YukiReflection
import com.highcapable.yukireflection.log.yLoggerE
import com.highcapable.yukireflection.log.yLoggerI
import com.highcapable.yukireflection.utils.debug.YukiLog
import com.highcapable.yukireflection.utils.factory.await
import java.lang.reflect.Constructor
import java.lang.reflect.Field
@@ -115,7 +114,7 @@ abstract class MemberBaseFinder internal constructor(
/** 存在日志时输出日志 */
internal fun printLogIfExist() {
if (loggingContent != null) yLoggerE(
if (loggingContent != null) YukiLog.error(
msg = "NoSuch$tag happend in [$classSet] ${loggingContent?.first}",
e = loggingContent?.second
)
@@ -128,6 +127,6 @@ abstract class MemberBaseFinder internal constructor(
* @param msg 调试日志内容
*/
internal fun onDebuggingMsg(msg: String) {
if (YukiReflection.Configs.isDebug) yLoggerI(msg)
if (YukiReflection.Configs.isDebug) YukiLog.debug(msg)
}
}

View File

@@ -48,7 +48,7 @@ import com.highcapable.yukireflection.finder.classes.rules.result.MemberRulesRes
import com.highcapable.yukireflection.finder.tools.ReflectionTool
import com.highcapable.yukireflection.finder.type.factory.ModifierConditions
import com.highcapable.yukireflection.finder.type.factory.NameConditions
import com.highcapable.yukireflection.log.yLoggerW
import com.highcapable.yukireflection.utils.debug.YukiLog
import com.highcapable.yukireflection.utils.factory.await
import com.highcapable.yukireflection.utils.factory.runBlocking
import com.highcapable.yukireflection.utils.factory.toStackTrace
@@ -93,7 +93,7 @@ class DexClassFinder @PublishedApi internal constructor(
?.let { "${CACHE_FILE_NAME}_${versionName ?: it.versionName}_${versionCode ?: runCatching { it.longVersionCode }.getOrNull() ?: it.versionCode}" }
?: "${CACHE_FILE_NAME}_unknown",
Context.MODE_PRIVATE)
}.onFailure { yLoggerW(msg = "Failed to read app's SharedPreferences when using DexClassFinder\n${it.toStackTrace()}") }.getOrNull()
}.onFailure { YukiLog.warn(msg = "Failed to read app's SharedPreferences when using DexClassFinder\n${it.toStackTrace()}") }.getOrNull()
/**
* 清除当前 [DexClassFinder] 的 [Class] 缓存
@@ -104,7 +104,7 @@ class DexClassFinder @PublishedApi internal constructor(
* @param versionCode 版本号 - 默认空
*/
fun clearCache(context: Context, versionName: String? = null, versionCode: Long? = null) =
context.currentSp(versionName, versionCode)?.edit()?.clear()?.apply() ?: yLoggerW(msg = "Failed to clear DexClassFinder's cache")
context.currentSp(versionName, versionCode)?.edit()?.clear()?.apply() ?: YukiLog.warn(msg = "Failed to clear DexClassFinder's cache")
}
@PublishedApi
@@ -454,7 +454,7 @@ class DexClassFinder @PublishedApi internal constructor(
takeIf { it.isNotEmpty() }?.forEach { names.add(it.name) }
context?.also {
if (it.packageName == "android") error("Cannot create classes cache for \"android\", please remove \"name\" param")
it.currentSp()?.edit()?.apply { putStringSet(name, names) }?.apply() ?: yLoggerW(msg = "Failed to use caching in DexClassFinder")
it.currentSp()?.edit()?.apply { putStringSet(name, names) }?.apply() ?: YukiLog.warn(msg = "Failed to use caching in DexClassFinder")
}
}
}

View File

@@ -41,9 +41,9 @@ import com.highcapable.yukireflection.finder.type.factory.ConstructorConditions
import com.highcapable.yukireflection.finder.type.factory.CountConditions
import com.highcapable.yukireflection.finder.type.factory.ModifierConditions
import com.highcapable.yukireflection.finder.type.factory.ObjectsConditions
import com.highcapable.yukireflection.log.yLoggerW
import com.highcapable.yukireflection.type.defined.UndefinedType
import com.highcapable.yukireflection.type.defined.VagueType
import com.highcapable.yukireflection.utils.debug.YukiLog
import com.highcapable.yukireflection.utils.factory.runBlocking
import java.lang.reflect.Constructor
@@ -319,7 +319,7 @@ class ConstructorFinder @PublishedApi internal constructor(@PublishedApi overrid
)
remedyPlans.clear()
}
} else yLoggerW(msg = "RemedyPlan is empty, forgot it?")
} else YukiLog.warn(msg = "RemedyPlan is empty, forgot it?")
}
/**

View File

@@ -43,7 +43,7 @@ import com.highcapable.yukireflection.finder.type.factory.FieldConditions
import com.highcapable.yukireflection.finder.type.factory.ModifierConditions
import com.highcapable.yukireflection.finder.type.factory.NameConditions
import com.highcapable.yukireflection.finder.type.factory.ObjectConditions
import com.highcapable.yukireflection.log.yLoggerW
import com.highcapable.yukireflection.utils.debug.YukiLog
import com.highcapable.yukireflection.utils.factory.runBlocking
import java.lang.reflect.Field
@@ -277,7 +277,7 @@ class FieldFinder @PublishedApi internal constructor(@PublishedApi override val
)
remedyPlans.clear()
}
} else yLoggerW(msg = "RemedyPlan is empty, forgot it?")
} else YukiLog.warn(msg = "RemedyPlan is empty, forgot it?")
}
/**

View File

@@ -43,9 +43,9 @@ import com.highcapable.yukireflection.finder.type.factory.ModifierConditions
import com.highcapable.yukireflection.finder.type.factory.NameConditions
import com.highcapable.yukireflection.finder.type.factory.ObjectConditions
import com.highcapable.yukireflection.finder.type.factory.ObjectsConditions
import com.highcapable.yukireflection.log.yLoggerW
import com.highcapable.yukireflection.type.defined.UndefinedType
import com.highcapable.yukireflection.type.defined.VagueType
import com.highcapable.yukireflection.utils.debug.YukiLog
import com.highcapable.yukireflection.utils.factory.runBlocking
import java.lang.reflect.Method
@@ -411,7 +411,7 @@ class MethodFinder @PublishedApi internal constructor(@PublishedApi override val
)
remedyPlans.clear()
}
} else yLoggerW(msg = "RemedyPlan is empty, forgot it?")
} else YukiLog.warn(msg = "RemedyPlan is empty, forgot it?")
}
/**

View File

@@ -43,13 +43,13 @@ import com.highcapable.yukireflection.finder.members.data.ConstructorRulesData
import com.highcapable.yukireflection.finder.members.data.FieldRulesData
import com.highcapable.yukireflection.finder.members.data.MemberRulesData
import com.highcapable.yukireflection.finder.members.data.MethodRulesData
import com.highcapable.yukireflection.log.yLoggerW
import com.highcapable.yukireflection.type.defined.UndefinedType
import com.highcapable.yukireflection.type.defined.VagueType
import com.highcapable.yukireflection.type.java.DalvikBaseDexClassLoader
import com.highcapable.yukireflection.type.java.NoClassDefFoundErrorClass
import com.highcapable.yukireflection.type.java.NoSuchFieldErrorClass
import com.highcapable.yukireflection.type.java.NoSuchMethodErrorClass
import com.highcapable.yukireflection.utils.debug.YukiLog
import com.highcapable.yukireflection.utils.factory.conditions
import com.highcapable.yukireflection.utils.factory.findLastIndex
import com.highcapable.yukireflection.utils.factory.lastIndex
@@ -204,7 +204,7 @@ internal object ReflectionTool {
fun MemberRulesData.exists(vararg type: Any?): Boolean {
if (type.isEmpty()) return true
for (i in type.indices) if (type[i] == UndefinedType) {
yLoggerW(msg = "$objectName type[$i] mistake, it will be ignored in current conditions")
YukiLog.warn(msg = "$objectName type[$i] mistake, it will be ignored in current conditions")
return false
}
return true
@@ -668,7 +668,7 @@ internal object ReflectionTool {
addAll(declaredConstructors.toList())
}.asSequence()
}.onFailure {
yLoggerW(msg = "Failed to get the declared Members in [$this] because got an exception\n$it")
YukiLog.warn(msg = "Failed to get the declared Members in [$this] because got an exception\n$it")
}.getOrNull()
/**
@@ -677,7 +677,7 @@ internal object ReflectionTool {
*/
private val Class<*>.existFields
get() = runCatching { declaredFields.asSequence() }.onFailure {
yLoggerW(msg = "Failed to get the declared Fields in [$this] because got an exception\n$it")
YukiLog.warn(msg = "Failed to get the declared Fields in [$this] because got an exception\n$it")
}.getOrNull()
/**
@@ -686,7 +686,7 @@ internal object ReflectionTool {
*/
private val Class<*>.existMethods
get() = runCatching { declaredMethods.asSequence() }.onFailure {
yLoggerW(msg = "Failed to get the declared Methods in [$this] because got an exception\n$it")
YukiLog.warn(msg = "Failed to get the declared Methods in [$this] because got an exception\n$it")
}.getOrNull()
/**
@@ -695,7 +695,7 @@ internal object ReflectionTool {
*/
private val Class<*>.existConstructors
get() = runCatching { declaredConstructors.asSequence() }.onFailure {
yLoggerW(msg = "Failed to get the declared Constructors in [$this] because got an exception\n$it")
YukiLog.warn(msg = "Failed to get the declared Constructors in [$this] because got an exception\n$it")
}.getOrNull()
/**

View File

@@ -1,67 +0,0 @@
/*
* YukiReflection - An efficient Reflection API for Java and Android built in Kotlin.
* Copyright (C) 2019-2023 HighCapable
* https://github.com/fankes/YukiReflection
*
* MIT License
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* This file is created by fankes on 2022/2/3.
* This file is modified by fankes on 2023/1/21.
*/
@file:Suppress("unused", "MemberVisibilityCanBePrivate")
package com.highcapable.yukireflection.log
import android.util.Log
import com.highcapable.yukireflection.YukiReflection
/**
* 向控制台打印日志 - D
* @param msg 日志打印的内容
*/
internal fun yLoggerD(msg: String) {
if (YukiReflection.Configs.isAllowPrintingLogs) Log.d(YukiReflection.Configs.debugTag, msg)
}
/**
* 向控制台打印日志 - I
* @param msg 日志打印的内容
*/
internal fun yLoggerI(msg: String) {
if (YukiReflection.Configs.isAllowPrintingLogs) Log.i(YukiReflection.Configs.debugTag, msg)
}
/**
* 向控制台打印日志 - W
* @param msg 日志打印的内容
*/
internal fun yLoggerW(msg: String) {
if (YukiReflection.Configs.isAllowPrintingLogs) Log.w(YukiReflection.Configs.debugTag, msg)
}
/**
* 向控制台打印日志 - E
* @param msg 日志打印的内容
* @param e 可填入异常堆栈信息 - 将自动完整打印到控制台
*/
internal fun yLoggerE(msg: String, e: Throwable? = null) {
if (YukiReflection.Configs.isAllowPrintingLogs) Log.e(YukiReflection.Configs.debugTag, msg, e)
}

View File

@@ -0,0 +1,117 @@
/*
* YukiReflection - An efficient Reflection API for Java and Android built in Kotlin.
* Copyright (C) 2019-2023 HighCapable
* https://github.com/fankes/YukiReflection
*
* MIT License
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* This file is created by fankes on 2023/9/23.
*/
package com.highcapable.yukireflection.utils.debug
import android.util.Log
import com.highcapable.yukireflection.YukiReflection
import com.highcapable.yukireflection.factory.hasClass
import com.highcapable.yukireflection.utils.factory.toStackTrace
/**
* 全局 Log 管理类
*/
internal object YukiLog {
/**
* 是否为 Android 模式
* @return [Boolean]
*/
private val isAndroidMode get() = "android.util.Log".hasClass()
/**
* 是否启用
* @return [Boolean]
*/
private val isEnable get() = YukiReflection.Configs.isEnableLogs
/**
* Log 标签
* @return [String]
*/
private val tag get() = YukiReflection.Configs.debugTag
/**
* 打印 Debug 级别 Log
* @param msg 消息内容
* @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)
}
/**
* 打印 Info 级别 Log
* @param msg 消息内容
* @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")
}
/**
* 打印 Warn 级别 Log
* @param msg 消息内容
* @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")
}
/**
* 打印 Error 级别 Log
* @param msg 消息内容
* @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")
}
/**
* 打印 Log
* @param msg 消息内容
* @param e 异常堆栈 - 默认空
* @param color 颜色代码 - 默认无颜色
*/
private fun log(msg: String, e: Throwable? = null, color: String = "") {
/**
* 打印 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.toStackTrace()) }
}
}