Horis 2 лет назад
Родитель
Сommit
24052ddf08

+ 1 - 1
app/src/main/java/io/legado/app/App.kt

@@ -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()

+ 16 - 0
app/src/main/java/io/legado/app/constant/AppLog.kt

@@ -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()

+ 26 - 19
app/src/main/java/io/legado/app/utils/LogUtils.kt

@@ -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
         }
     }