From d345f4ccf881b37c34a859d3d108525cce2fbd8e Mon Sep 17 00:00:00 2001 From: qimiko <25387744+qimiko@users.noreply.github.com> Date: Mon, 24 Jun 2024 20:28:37 -0700 Subject: [PATCH] read logs based on length instead of null --- .../main/java/com/geode/launcher/log/LogLine.kt | 15 +++++++++++---- .../java/com/geode/launcher/log/LogViewModel.kt | 12 +++++++++--- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/com/geode/launcher/log/LogLine.kt b/app/src/main/java/com/geode/launcher/log/LogLine.kt index 7243e646..ffb30636 100644 --- a/app/src/main/java/com/geode/launcher/log/LogLine.kt +++ b/app/src/main/java/com/geode/launcher/log/LogLine.kt @@ -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, diff --git a/app/src/main/java/com/geode/launcher/log/LogViewModel.kt b/app/src/main/java/com/geode/launcher/log/LogViewModel.kt index 3c26d67b..1fd2874e 100644 --- a/app/src/main/java/com/geode/launcher/log/LogViewModel.kt +++ b/app/src/main/java/com/geode/launcher/log/LogViewModel.kt @@ -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