This commit is contained in:
Horis 2024-04-11 12:53:37 +08:00
parent 2720898214
commit e6d181a5fd
4 changed files with 73 additions and 38 deletions

View File

@ -50,6 +50,7 @@ class App : Application() {
override fun onCreate() { override fun onCreate() {
super.onCreate() super.onCreate()
LogUtils.d("App", "onCreate") LogUtils.d("App", "onCreate")
LogUtils.logDeviceInfo()
oldConfig = Configuration(resources.configuration) oldConfig = Configuration(resources.configuration)
CrashHandler(this) CrashHandler(this)
//预下载Cronet so //预下载Cronet so

View File

@ -30,6 +30,8 @@ import io.legado.app.utils.sendMail
import io.legado.app.utils.sendToClip import io.legado.app.utils.sendToClip
import io.legado.app.utils.showDialogFragment import io.legado.app.utils.showDialogFragment
import io.legado.app.utils.toastOnUi import io.legado.app.utils.toastOnUi
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.launch
import splitties.init.appCtx import splitties.init.appCtx
import java.io.File import java.io.File
@ -125,31 +127,8 @@ class AboutFragment : PreferenceFragmentCompat() {
return@async return@async
} }
val doc = FileDoc.fromUri(Uri.parse(backupPath), true) val doc = FileDoc.fromUri(Uri.parse(backupPath), true)
val files = FileDoc.fromFile(File(appCtx.externalCacheDir, "logs")).list() copyLogs(doc)
if (!files.isNullOrEmpty()) { copyHeapDump(doc)
doc.find("logs")?.delete()
val logsDoc = doc.createFolderIfNotExist("logs")
files.forEach { file ->
file.openInputStream().getOrNull()?.use { input ->
logsDoc.createFileIfNotExist(file.name).openOutputStream().getOrNull()
?.use {
input.copyTo(it)
}
}
}
}
val heapFile = FileDoc.fromFile(File(appCtx.externalCacheDir, "heapDump")).list()
?.firstOrNull()
if (heapFile != null) {
doc.find("heapDump")?.delete()
val heapDumpDoc = doc.createFolderIfNotExist("heapDump")
heapFile.openInputStream().getOrNull()?.use { input ->
heapDumpDoc.createFileIfNotExist(heapFile.name).openOutputStream().getOrNull()
?.use {
input.copyTo(it)
}
}
}
appCtx.toastOnUi("已保存至备份目录") appCtx.toastOnUi("已保存至备份目录")
}.onError { }.onError {
AppLog.put("保存日志出错\n${it.localizedMessage}", it, true) AppLog.put("保存日志出错\n${it.localizedMessage}", it, true)
@ -165,24 +144,48 @@ class AboutFragment : PreferenceFragmentCompat() {
appCtx.toastOnUi("开始创建堆转储") appCtx.toastOnUi("开始创建堆转储")
System.gc() System.gc()
CrashHandler.doHeapDump() CrashHandler.doHeapDump()
val heapFile = FileDoc.fromFile(File(appCtx.externalCacheDir, "heapDump")).list()
?.firstOrNull() ?: let {
appCtx.toastOnUi("未找到堆转储文件")
return@async
}
val doc = FileDoc.fromUri(Uri.parse(backupPath), true) val doc = FileDoc.fromUri(Uri.parse(backupPath), true)
doc.find("heapDump")?.delete() if (!copyHeapDump(doc)) {
val heapDumpDoc = doc.createFolderIfNotExist("heapDump") appCtx.toastOnUi("未找到堆转储文件")
heapFile.openInputStream().getOrNull()?.use { input -> } else {
heapDumpDoc.createFileIfNotExist(heapFile.name).openOutputStream().getOrNull() appCtx.toastOnUi("已保存至备份目录")
?.use {
input.copyTo(it)
}
} }
appCtx.toastOnUi("已保存至备份目录")
}.onError { }.onError {
AppLog.put("保存堆转储失败\n${it.localizedMessage}", it) AppLog.put("保存堆转储失败\n${it.localizedMessage}", it)
} }
} }
private suspend fun copyLogs(doc: FileDoc) = coroutineScope {
val files = FileDoc.fromFile(File(appCtx.externalCacheDir, "logs")).list()
if (files.isNullOrEmpty()) {
return@coroutineScope
}
doc.find("logs")?.delete()
val logsDoc = doc.createFolderIfNotExist("logs")
files.forEach { file ->
launch {
file.openInputStream().getOrNull()?.use { input ->
logsDoc.createFileIfNotExist(file.name).openOutputStream().getOrNull()
?.use {
input.copyTo(it)
}
}
}
}
}
private fun copyHeapDump(doc: FileDoc): Boolean {
val heapFile = FileDoc.fromFile(File(appCtx.externalCacheDir, "heapDump")).list()
?.firstOrNull() ?: return false
doc.find("heapDump")?.delete()
val heapDumpDoc = doc.createFolderIfNotExist("heapDump")
heapFile.openInputStream().getOrNull()?.use { input ->
heapDumpDoc.createFileIfNotExist(heapFile.name).openOutputStream().getOrNull()
?.use {
input.copyTo(it)
}
}
return true
}
} }

View File

@ -174,6 +174,7 @@ class OtherConfigFragment : PreferenceFragment(),
PreferKey.recordLog -> { PreferKey.recordLog -> {
LogUtils.upLevel() LogUtils.upLevel()
LogUtils.logDeviceInfo()
LiveEventBus.config().enableLogger(AppConfig.recordLog) LiveEventBus.config().enableLogger(AppConfig.recordLog)
} }

View File

@ -3,7 +3,10 @@
package io.legado.app.utils package io.legado.app.utils
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.os.Build
import android.webkit.WebSettings
import io.legado.app.BuildConfig import io.legado.app.BuildConfig
import io.legado.app.constant.AppConst
import io.legado.app.help.config.AppConfig import io.legado.app.help.config.AppConfig
import splitties.init.appCtx import splitties.init.appCtx
import java.text.SimpleDateFormat import java.text.SimpleDateFormat
@ -107,6 +110,33 @@ object LogUtils {
val sdf = SimpleDateFormat(pattern) val sdf = SimpleDateFormat(pattern)
return sdf.format(date) return sdf.format(date)
} }
fun logDeviceInfo() {
d("DeviceInfo") {
buildString {
kotlin.runCatching {
//获取系统信息
append("MANUFACTURER=").append(Build.MANUFACTURER).append("\n")
append("BRAND=").append(Build.BRAND).append("\n")
append("MODEL=").append(Build.MODEL).append("\n")
append("SDK_INT=").append(Build.VERSION.SDK_INT).append("\n")
append("RELEASE=").append(Build.VERSION.RELEASE).append("\n")
val userAgent = try {
WebSettings.getDefaultUserAgent(appCtx)
} catch (e: Throwable) {
e.localizedMessage ?: "null"
}
append("WebViewUserAgent=").append(userAgent).append("\n")
//获取app版本信息
AppConst.appInfo.let {
append("versionName=").append(it.versionName).append("\n")
append("versionCode=").append(it.versionCode).append("\n")
}
}
}
}
}
} }
fun Throwable.printOnDebug() { fun Throwable.printOnDebug() {