添加log and crash 保存本地,分享。

This commit is contained in:
2025-11-11 10:48:20 +08:00
parent 0aca66ce84
commit 8d983bfa99
20 changed files with 231 additions and 130 deletions

View File

@@ -1,14 +1,10 @@
package com.xscm.modulemain
import android.content.Context
import com.blankj.utilcode.util.CrashUtils
import com.blankj.utilcode.util.LogUtils
import com.opensource.svgaplayer.utils.log.SVGALogger
import com.scwang.smartrefresh.layout.SmartRefreshLayout
import com.scwang.smartrefresh.layout.api.DefaultRefreshFooterCreator
import com.scwang.smartrefresh.layout.api.DefaultRefreshHeaderCreator
import com.scwang.smartrefresh.layout.api.RefreshFooter
import com.scwang.smartrefresh.layout.api.RefreshHeader
import com.scwang.smartrefresh.layout.api.RefreshLayout
import com.scwang.smartrefresh.layout.footer.ClassicsFooter
import com.scwang.smartrefresh.layout.header.ClassicsHeader
import com.xscm.modulemain.widget.WheatLayoutSingManager
@@ -17,17 +13,67 @@ import com.xscm.moduleutil.base.CommonAppContext
open class Application : CommonAppContext() {
var APP_CONENT = ""
var LOGUTILS_SAVE_PATH = ""
var CRASHUTILS_SAVE_PATH = ""
// 单例实例
companion object {
@Volatile
private lateinit var instance: Application
// 全局获取 Application 实例
fun getInstance(): Application {
return instance
}
// 获取 Application Context避免内存泄漏
fun getContext(): Context {
return instance.applicationContext
}
}
override fun onCreate() {
super.onCreate()
LOGUTILS_SAVE_PATH = getExternalFilesDir("APP_CONTENT/APP_LOG")?.absolutePath.toString()
CRASHUTILS_SAVE_PATH = getExternalFilesDir("APP_CONTENT/APP_CRASH")?.absolutePath.toString()
getExternalFilesDir("")
APP_CONENT = getExternalFilesDir("APP_CONTENT")?.absolutePath.toString()
// 初始化单例实例
instance = this
// 初始化并预绘制视图 二卡八列
WheatLayoutSingManager.init(this)
WheatLayoutSingManager.getInstance().setWheatData( null)
WheatLayoutSingManager.getInstance().setWheatData(null)
// 默认情况下SVGA 内部不会输出任何 log所以需要手动设置为 true
SVGALogger.setLogEnabled(false)
LogUtils.getConfig().isLogSwitch = true
initLogUtils()
initCrashUtils()
initSmartRefreshLayout()
}
private fun initLogUtils() {
LogUtils.getConfig()
.setLogSwitch(true) // 全局开关
.setLog2FileSwitch(true) // 必须设为 true
.setDir(LOGUTILS_SAVE_PATH) // 设置有效路径
.setFileFilter(LogUtils.V); // 允许所有级别
}
private fun initCrashUtils() {
CrashUtils.init(CRASHUTILS_SAVE_PATH, object : CrashUtils.OnCrashListener {
override fun onCrash(crashInfo: CrashUtils.CrashInfo?) {
LogUtils.e("crash", crashInfo.toString())
}
})
}
private fun initSmartRefreshLayout() {
//设置全局的Header构建器
SmartRefreshLayout.setDefaultRefreshHeaderCreator { context, _ ->
val header = ClassicsHeader(context)
@@ -49,4 +95,9 @@ open class Application : CommonAppContext() {
}
}
fun getAppContent(): String {
return APP_CONENT
}
}