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

This commit is contained in:
Steve Johnson 2023-11-02 01:52:39 +08:00
parent 00b2c816a4
commit ad471f3837
3 changed files with 22 additions and 8 deletions

View File

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

View File

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

View File

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