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
+20 -7

No files matched your search

@@ -5,6 +5,7 @@ import kotlinx.datetime.Instant
import kotlinx.datetime.TimeZone
import kotlinx.datetime.toJavaInstant
import kotlinx.datetime.toLocalDateTime
import okio.Buffer
import okio.BufferedSource
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 entryVersion = headerSizeToVersion(headerSize)
@@ -177,11 +178,17 @@ data class LogLine(
val processInformation = ProcessInformation(pid, tid, uid)
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 tag = source.readCString()
val message = source.readCString()
val tag = packetBuffer.readCString()
val message = packetBuffer.readUtf8()
return LogLine(
process = processInformation,
@@ -9,7 +9,9 @@ import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import kotlinx.coroutines.yield
import okio.Buffer
import okio.buffer
import okio.source
import java.io.EOFException
@@ -61,7 +63,9 @@ class LogViewModel: ViewModel() {
else "logcat -B -d"
val logProcess = try {
Runtime.getRuntime().exec(logCommand)
withContext(Dispatchers.IO) {
Runtime.getRuntime().exec(logCommand)
}
} catch (ioe: IOException) {
ioe.printStackTrace()
logLines += LogLine.showException(ioe)
@@ -71,13 +75,15 @@ class LogViewModel: ViewModel() {
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 {
coroutineScope {
// this runs until the stream is exhausted
while (true) {
val line = LogLine.fromBufferedSource(logSource)
val line = LogLine.fromBufferedSource(logBuffer)
if (line.priority >= logLevel) {
logLines += line