perf(app): log reader

Signed-off-by: rhunk <101876869+rhunk@users.noreply.github.com>
This commit is contained in:
rhunk
2025-01-10 17:24:18 +01:00
parent 8bb2cb37d6
commit fbeb3d87ec
2 changed files with 31 additions and 23 deletions

View File

@@ -78,18 +78,30 @@ class LogReader(
}
private fun queryLineCount(): Int {
val buffer = ByteArray(1024 * 1024)
synchronized(randomAccessFile) {
randomAccessFile.seek(0)
var lineCount = 0
var lastPointer: Long
var line: String?
var read: Int
var lastPointer: Long = 0
var line: StringBuilder? = null
while (randomAccessFile.also {
lastPointer = it.filePointer
}.readLine().also { line = it } != null) {
if (line?.startsWith('|') == true) {
lineCount++
startLineIndexes.add(lastPointer + 1)
while (randomAccessFile.read(buffer).also { read = it } != -1) {
for (i in 0 until read) {
val char = buffer[i].toInt().toChar()
if (line == null) {
line = StringBuilder()
lastPointer = randomAccessFile.filePointer - read + i
}
line.append(char)
if (char == '\n') {
if (line.startsWith('|')) {
lineCount++
startLineIndexes.add(lastPointer + 1)
}
line = null
}
}
}
@@ -100,7 +112,7 @@ class LogReader(
private fun getLine(index: Int): String? {
if (index <= 0 || index > lineCount) return null
synchronized(randomAccessFile) {
randomAccessFile.seek(startLineIndexes[index])
randomAccessFile.seek(startLineIndexes.getOrNull(index) ?: return null)
return readLogLine()?.toString()
}
}

View File

@@ -156,8 +156,6 @@ class HomeLogs : Routes.Route() {
logReader?.getLogLine(index)
})
}
var expand by remember { mutableStateOf(false) }
logLine?.let { line ->
Box(modifier = Modifier
.fillMaxWidth()
@@ -171,20 +169,18 @@ class HomeLogs : Routes.Route() {
)
)
}
},
onTap = {
expand = !expand
}
)
}) {
Row(
Column(
modifier = Modifier
.padding(4.dp)
.fillMaxWidth()
.defaultMinSize(minHeight = 30.dp),
verticalAlignment = Alignment.CenterVertically
) {
if (!expand) {
Row(
verticalAlignment = Alignment.CenterVertically,
) {
Icon(
imageVector = when (line.logLevel) {
LogLevel.DEBUG -> Icons.Outlined.BugReport
@@ -193,14 +189,15 @@ class HomeLogs : Routes.Route() {
LogLevel.WARN -> Icons.Outlined.Warning
else -> Icons.Outlined.Info
},
modifier = Modifier.size(16.dp),
contentDescription = null,
)
Text(
text = LogChannel.fromChannel(line.tag)?.shortName ?: line.tag,
modifier = Modifier.padding(start = 4.dp),
fontWeight = FontWeight.Light,
fontSize = 10.sp,
fontWeight = FontWeight.Bold,
fontSize = 12.sp,
)
Text(
@@ -212,10 +209,9 @@ class HomeLogs : Routes.Route() {
Text(
text = line.message.trimIndent(),
fontSize = 10.sp,
maxLines = if (expand) Int.MAX_VALUE else 6,
overflow = if (expand) TextOverflow.Visible else TextOverflow.Ellipsis,
softWrap = !expand,
lineHeight = 10.sp,
fontSize = 9.sp,
maxLines = Int.MAX_VALUE,
)
}
}