Преглед изворни кода

fix: adapt to format-broken logs (#165)

Steve Johnson пре 2 година
родитељ
комит
ad471f3837

+ 3 - 3
README.md

@@ -52,11 +52,11 @@ Feature of [Clash.Meta](https://github.com/MetaCubeX/Clash.Meta)
 APP package name is `com.github.metacubex.clash.meta`
 
 - Toggle Clash.Meta service status
-  - Send intent to `com.github.kr328.clash.ExternalControlActivity` with action `com.github.metacubex.clash.meta.action.TOGGLE_CLASH`
+  - Send intent to activity `com.github.kr328.clash.ExternalControlActivity` with action `com.github.metacubex.clash.meta.action.TOGGLE_CLASH`
 - Start Clash.Meta service
-  - Send intent to `com.github.kr328.clash.ExternalControlActivity` with action `com.github.metacubex.clash.meta.action.START_CLASH`
+  - Send intent to activity `com.github.kr328.clash.ExternalControlActivity` with action `com.github.metacubex.clash.meta.action.START_CLASH`
 - Stop Clash.Meta service
-  - Send intent to `com.github.kr328.clash.ExternalControlActivity` with action `com.github.metacubex.clash.meta.action.STOP_CLASH`
+  - Send intent to activity `com.github.kr328.clash.ExternalControlActivity` with action `com.github.metacubex.clash.meta.action.STOP_CLASH`
 - Import a profile
   - URL Scheme `clash://install-config?url=<encoded URI>` or `clashmeta://install-config?url=<encoded URI>`
 

+ 2 - 0
app/src/main/java/com/github/kr328/clash/LogcatActivity.kt

@@ -8,6 +8,7 @@ import android.os.IBinder
 import android.widget.Toast
 import androidx.activity.result.contract.ActivityResultContracts
 import com.github.kr328.clash.common.compat.startForegroundServiceCompat
+import com.github.kr328.clash.common.log.Log
 import com.github.kr328.clash.common.util.fileName
 import com.github.kr328.clash.common.util.intent
 import com.github.kr328.clash.common.util.ticker
@@ -47,6 +48,7 @@ class LogcatActivity : BaseActivity<LogcatDesign>() {
         val messages = try {
             LogcatReader(this, file).readAll()
         } catch (e: Exception) {
+            Log.e("Fail to read log file ${file.fileName}: ${e.message}")
             return showInvalid()
         }
 

+ 17 - 5
app/src/main/java/com/github/kr328/clash/log/LogcatReader.kt

@@ -16,16 +16,28 @@ class LogcatReader(context: Context, file: LogFile) : AutoCloseable {
     }
 
     fun readAll(): List<LogMessage> {
+        var lastTime = Date(0)
         return reader.lineSequence()
             .map { it.trim() }
             .filter { !it.startsWith("#") }
             .map { it.split(":", limit = 3) }
             .map {
-                LogMessage(
-                    time = Date(it[0].toLong()),
-                    level = LogMessage.Level.valueOf(it[1]),
-                    message = it[2]
-                )
+                val time = it[0].toLongOrNull()?.let { Date(it) } ?: lastTime
+                val logMessage = if (it[0].toLongOrNull() != null) {
+                    LogMessage(
+                        time = time,
+                        level = LogMessage.Level.valueOf(it[1]),
+                        message = it[2]
+                    )
+                } else {
+                    LogMessage(
+                        time = time,
+                        level = LogMessage.Level.Warning, // or any default level
+                        message = it.joinToString(":")
+                    )
+                }
+                lastTime = time
+                logMessage
             }
             .toList()
     }