read logs based on length instead of null

This commit is contained in:
qimiko committed 2024-06-24 20:28:37 -07:00
1 parent 6b70d8c6d0
commit d345f4ccf8
2 files changed
+19 -6

No files matched your search

@@ -5,6 +5,7 @@ import kotlinx.datetime.Instant
import kotlinx.datetime.TimeZone import kotlinx.datetime.TimeZone
import kotlinx.datetime.toJavaInstant import kotlinx.datetime.toJavaInstant
import kotlinx.datetime.toLocalDateTime import kotlinx.datetime.toLocalDateTime
import okio.Buffer
import okio.BufferedSource import okio.BufferedSource
import java.io.IOException import java.io.IOException
@@ -161,7 +162,7 @@ data class LogLine(
}; };
*/ */
/* val payloadLength = */ source.readShortLe() val payloadLength = source.readShortLe().toLong()
val headerSize = source.readShortLe().toUShort() val headerSize = source.readShortLe().toUShort()
val entryVersion = headerSizeToVersion(headerSize) val entryVersion = headerSizeToVersion(headerSize)
@@ -177,11 +178,17 @@ data class LogLine(
val processInformation = ProcessInformation(pid, tid, uid) val processInformation = ProcessInformation(pid, tid, uid)
val time = Instant.fromEpochSeconds(sec, nSec) val time = Instant.fromEpochSeconds(sec, nSec)
val priorityByte = source.readByte() // the payload is split into three parts
// initial priority byte -> null terminated tag -> non null terminated message
val packetBuffer = Buffer()
source.readFully(packetBuffer, payloadLength)
val priorityByte = packetBuffer.readByte()
val priority = LogPriority.fromByte(priorityByte) val priority = LogPriority.fromByte(priorityByte)
val tag = source.readCString() val tag = packetBuffer.readCString()
val message = source.readCString() val message = packetBuffer.readUtf8()
return LogLine( return LogLine(
process = processInformation, process = processInformation,
@@ -9,7 +9,9 @@ import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import kotlinx.coroutines.yield import kotlinx.coroutines.yield
import okio.Buffer
import okio.buffer import okio.buffer
import okio.source import okio.source
import java.io.EOFException import java.io.EOFException
@@ -61,7 +63,9 @@ class LogViewModel: ViewModel() {
else "logcat -B -d" else "logcat -B -d"
val logProcess = try { val logProcess = try {
withContext(Dispatchers.IO) {
Runtime.getRuntime().exec(logCommand) Runtime.getRuntime().exec(logCommand)
}
} catch (ioe: IOException) { } catch (ioe: IOException) {
ioe.printStackTrace() ioe.printStackTrace()
logLines += LogLine.showException(ioe) logLines += LogLine.showException(ioe)
@@ -71,13 +75,15 @@ class LogViewModel: ViewModel() {
return logLines return logLines
} }
val logSource = logProcess.inputStream.source().buffer() // read entire log into a buffer so no logs are added to the buffer during processing
val logBuffer = Buffer()
logProcess.inputStream.source().buffer().readAll(logBuffer)
try { try {
coroutineScope { coroutineScope {
// this runs until the stream is exhausted // this runs until the stream is exhausted
while (true) { while (true) {
val line = LogLine.fromBufferedSource(logSource) val line = LogLine.fromBufferedSource(logBuffer)
if (line.priority >= logLevel) { if (line.priority >= logLevel) {
logLines += line logLines += line