Skip to content

Commit

Permalink
read logs based on length instead of null
Browse files Browse the repository at this point in the history
  • Loading branch information
qimiko committed Jun 25, 2024
1 parent 6b70d8c commit d345f4c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
15 changes: 11 additions & 4 deletions app/src/main/java/com/geode/launcher/log/LogLine.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand All @@ -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,
Expand Down
12 changes: 9 additions & 3 deletions app/src/main/java/com/geode/launcher/log/LogViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down

0 comments on commit d345f4c

Please sign in to comment.