Skip to content

Commit d345f4c

Browse files
committedJun 25, 2024
read logs based on length instead of null
1 parent 6b70d8c commit d345f4c

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed
 

‎app/src/main/java/com/geode/launcher/log/LogLine.kt

+11-4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import kotlinx.datetime.Instant
55
import kotlinx.datetime.TimeZone
66
import kotlinx.datetime.toJavaInstant
77
import kotlinx.datetime.toLocalDateTime
8+
import okio.Buffer
89
import okio.BufferedSource
910
import java.io.IOException
1011

@@ -161,7 +162,7 @@ data class LogLine(
161162
};
162163
*/
163164

164-
/* val payloadLength = */ source.readShortLe()
165+
val payloadLength = source.readShortLe().toLong()
165166
val headerSize = source.readShortLe().toUShort()
166167

167168
val entryVersion = headerSizeToVersion(headerSize)
@@ -177,11 +178,17 @@ data class LogLine(
177178
val processInformation = ProcessInformation(pid, tid, uid)
178179
val time = Instant.fromEpochSeconds(sec, nSec)
179180

180-
val priorityByte = source.readByte()
181+
// the payload is split into three parts
182+
// initial priority byte -> null terminated tag -> non null terminated message
183+
184+
val packetBuffer = Buffer()
185+
source.readFully(packetBuffer, payloadLength)
186+
187+
val priorityByte = packetBuffer.readByte()
181188
val priority = LogPriority.fromByte(priorityByte)
182189

183-
val tag = source.readCString()
184-
val message = source.readCString()
190+
val tag = packetBuffer.readCString()
191+
val message = packetBuffer.readUtf8()
185192

186193
return LogLine(
187194
process = processInformation,

‎app/src/main/java/com/geode/launcher/log/LogViewModel.kt

+9-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ import kotlinx.coroutines.coroutineScope
99
import kotlinx.coroutines.flow.MutableStateFlow
1010
import kotlinx.coroutines.flow.asStateFlow
1111
import kotlinx.coroutines.launch
12+
import kotlinx.coroutines.withContext
1213
import kotlinx.coroutines.yield
14+
import okio.Buffer
1315
import okio.buffer
1416
import okio.source
1517
import java.io.EOFException
@@ -61,7 +63,9 @@ class LogViewModel: ViewModel() {
6163
else "logcat -B -d"
6264

6365
val logProcess = try {
64-
Runtime.getRuntime().exec(logCommand)
66+
withContext(Dispatchers.IO) {
67+
Runtime.getRuntime().exec(logCommand)
68+
}
6569
} catch (ioe: IOException) {
6670
ioe.printStackTrace()
6771
logLines += LogLine.showException(ioe)
@@ -71,13 +75,15 @@ class LogViewModel: ViewModel() {
7175
return logLines
7276
}
7377

74-
val logSource = logProcess.inputStream.source().buffer()
78+
// read entire log into a buffer so no logs are added to the buffer during processing
79+
val logBuffer = Buffer()
80+
logProcess.inputStream.source().buffer().readAll(logBuffer)
7581

7682
try {
7783
coroutineScope {
7884
// this runs until the stream is exhausted
7985
while (true) {
80-
val line = LogLine.fromBufferedSource(logSource)
86+
val line = LogLine.fromBufferedSource(logBuffer)
8187

8288
if (line.priority >= logLevel) {
8389
logLines += line

0 commit comments

Comments
 (0)