This commit is contained in:
Horis
2024-08-16 17:31:29 +08:00
parent d68404c6a4
commit deef0a69be
3 changed files with 43 additions and 20 deletions

View File

@@ -52,13 +52,13 @@ class App : Application() {
override fun onCreate() {
super.onCreate()
CrashHandler(this)
LogUtils.d("App", "onCreate")
LogUtils.logDeviceInfo()
if (isDebuggable) {
ThreadUtils.setThreadAssertsDisabledForTesting(true)
}
oldConfig = Configuration(resources.configuration)
CrashHandler(this)
//预下载Cronet so
Cronet.preDownload()
createNotificationChannels()

View File

@@ -34,6 +34,22 @@ object AppLog {
}
}
@Synchronized
fun putNotSave(message: String?, throwable: Throwable? = null, toast: Boolean = false) {
message ?: return
if (toast) {
appCtx.toastOnUi(message)
}
if (mLogs.size > 100) {
mLogs.removeLastOrNull()
}
mLogs.add(0, Triple(System.currentTimeMillis(), message, throwable))
if (BuildConfig.DEBUG) {
val stackTrace = Thread.currentThread().stackTrace
Log.e(stackTrace[3].className, message, throwable)
}
}
@Synchronized
fun clear() {
mLogs.clear()

View File

@@ -7,6 +7,7 @@ import android.os.Build
import android.webkit.WebSettings
import io.legado.app.BuildConfig
import io.legado.app.constant.AppConst
import io.legado.app.constant.AppLog
import io.legado.app.help.config.AppConfig
import splitties.init.appCtx
import java.text.SimpleDateFormat
@@ -47,28 +48,34 @@ object LogUtils {
}
private val fileHandler by lazy {
val root = appCtx.externalCacheDir ?: return@lazy null
val logFolder = FileUtils.createFolderIfNotExist(root, "logs")
val expiredTime = System.currentTimeMillis() - 7.days.inWholeMilliseconds
logFolder.listFiles()?.forEach {
if (it.lastModified() < expiredTime) {
it.delete()
}
}
val date = getCurrentDateStr(TIME_PATTERN)
val logPath = FileUtils.getPath(root = logFolder, "appLog-$date.txt")
AsyncFileHandler(logPath).apply {
formatter = object : java.util.logging.Formatter() {
override fun format(record: LogRecord): String {
// 设置文件输出格式
return (getCurrentDateStr(TIME_PATTERN) + ": " + record.message + "\n")
try {
val root = appCtx.externalCacheDir ?: return@lazy null
val logFolder = FileUtils.createFolderIfNotExist(root, "logs")
val expiredTime = System.currentTimeMillis() - 7.days.inWholeMilliseconds
logFolder.listFiles()?.forEach {
if (it.lastModified() < expiredTime) {
it.delete()
}
}
level = if (AppConfig.recordLog) {
Level.INFO
} else {
Level.OFF
val date = getCurrentDateStr(TIME_PATTERN)
val logPath = FileUtils.getPath(root = logFolder, "appLog-$date.txt")
AsyncFileHandler(logPath).apply {
formatter = object : java.util.logging.Formatter() {
override fun format(record: LogRecord): String {
// 设置文件输出格式
return (getCurrentDateStr(TIME_PATTERN) + ": " + record.message + "\n")
}
}
level = if (AppConfig.recordLog) {
Level.INFO
} else {
Level.OFF
}
}
} catch (e: Exception) {
e.printStackTrace()
AppLog.putNotSave("创建fileHandler出错\n$e", e)
return@lazy null
}
}