add option to filter by log level

This commit is contained in:
qimiko committed 2024-01-28 18:48:11 -07:00
1 parent c8e3d7404c
commit b50a5b2b54
6 files changed
+143 -22

No files matched your search

@@ -104,16 +104,20 @@ fun getLogColor(priority: LogPriority): Color {
// i just copied these from my ide tbh
return if (isDark) {
when (priority) {
LogPriority.ERROR -> Color(0xffdf5151)
LogPriority.WARN -> Color(0xffbcb500)
LogPriority.FATAL -> Color(0xffb8b6fc)
LogPriority.ERROR -> Color(0xff_df_51_51)
LogPriority.WARN -> Color(0xff_bc_b5_00)
LogPriority.FATAL -> Color(0xff_b8_b6_fc)
LogPriority.DEBUG -> Color(0xff_aa_aa_aa)
LogPriority.VERBOSE -> Color(0xff_77_77_77)
else -> Color.Unspecified
}
} else {
when (priority) {
LogPriority.ERROR -> Color(0xffdf5151)
LogPriority.WARN -> Color(0xffeab700)
LogPriority.FATAL -> Color(0xffa142f4)
LogPriority.ERROR -> Color(0xff_df_51_51)
LogPriority.WARN -> Color(0xff_ea_b7_00)
LogPriority.FATAL -> Color(0xff_a1_42_f4)
LogPriority.DEBUG -> Color(0xff_77_77_77)
LogPriority.VERBOSE -> Color(0xff_aa_aa_aa)
else -> Color.Unspecified
}
}
@@ -138,6 +142,25 @@ fun copyLogText(context: Context, logMessage: String) {
}
}
@Composable
fun SelectLogLevelDialog(
onDismissRequest: () -> Unit,
onSelect: (LogPriority) -> Unit,
initialValue: LogPriority
) {
SelectDialog(
title = stringResource(R.string.application_logs_level_dialog_name),
onDismissRequest = onDismissRequest,
onSelect = { onSelect(it) },
initialValue = initialValue,
toLabel = {
// i'm lazy
it.name.lowercase().replaceFirstChar { c -> c.uppercaseChar() }
},
optionsCount = LogPriority.FATAL downTo LogPriority.VERBOSE
)
}
@Composable
fun LogCard(logLine: LogLine, modifier: Modifier = Modifier) {
val context = LocalContext.current
@@ -205,6 +228,18 @@ fun ApplicationLogsScreen(
listState.scrollToItem(logLines.size)
}
var showLogLevelSelect by remember { mutableStateOf(false) }
if (showLogLevelSelect) {
SelectLogLevelDialog(
onDismissRequest = { showLogLevelSelect = false },
onSelect = {
logViewModel.logLevel = it
showLogLevelSelect = false
},
initialValue = logViewModel.logLevel
)
}
Scaffold(
modifier = Modifier.nestedScroll(scrollBehavior.nestedScrollConnection),
topBar = {
@@ -238,7 +273,7 @@ fun ApplicationLogsScreen(
contentDescription = stringResource(R.string.application_logs_share)
)
}
IconButton(onClick = { showMoreOptions = !showMoreOptions },) {
IconButton(onClick = { showMoreOptions = !showMoreOptions }) {
Icon(
Icons.Filled.MoreVert,
contentDescription = stringResource(R.string.application_logs_more)
@@ -252,7 +287,7 @@ fun ApplicationLogsScreen(
DropdownMenuItem(
leadingIcon = {
Icon(
painterResource(R.drawable.icon_filter_list),
painterResource(R.drawable.icon_bug_report),
contentDescription = null,
)
},
@@ -272,6 +307,21 @@ fun ApplicationLogsScreen(
}
)
DropdownMenuItem(
leadingIcon = {
Icon(
painterResource(R.drawable.icon_filter_list),
contentDescription = null,
)
},
text = {
Text(stringResource(R.string.application_logs_level))
},
onClick = {
showLogLevelSelect = true
}
)
DropdownMenuItem(
leadingIcon = {
Icon(
@@ -21,7 +21,6 @@ import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
@@ -105,7 +104,7 @@ fun SettingsSelectCard(
},
initialValue = preferenceValue,
toLabel = toLabel,
optionsCount = maxVal
optionsCount = 0..maxVal
)
}
}
@@ -190,15 +189,15 @@ fun StringDialog(
}
@Composable
fun SelectDialog(
fun <T> SelectDialog(
title: String,
onDismissRequest: () -> Unit,
onSelect: (Int) -> Unit,
initialValue: Int,
toLabel: @Composable (Int) -> String,
optionsCount: Int,
onSelect: (T) -> Unit,
initialValue: T,
toLabel: @Composable (T) -> String,
optionsCount: Iterable<T>,
) {
var selectedValue by remember { mutableIntStateOf(initialValue) }
var selectedValue by remember { mutableStateOf(initialValue) }
Dialog(onDismissRequest = onDismissRequest) {
Card(
@@ -218,7 +217,7 @@ fun SelectDialog(
)
// do not give the row or column padding!! it messes up the selection effect
(0..optionsCount).forEach { id ->
optionsCount.forEach { id ->
Row(
verticalAlignment = Alignment.CenterVertically,
modifier = Modifier
@@ -29,9 +29,50 @@ data class ProcessInformation(val processId: Int, val threadId: Int, val process
enum class LogPriority {
UNKNOWN, DEFAULT, VERBOSE, DEBUG, INFO, WARN, ERROR, FATAL, SILENT;
class LogPriorityIterator(
start: LogPriority,
private val end: LogPriority,
private val step: Int
) : Iterator<LogPriority> {
private var hasNext: Boolean = if (step > 0) start <= end else start >= end
private var next = if (hasNext) start else end
override fun hasNext() = hasNext
override fun next(): LogPriority {
val current = next
if (current == end) {
if (!hasNext) {
throw NoSuchElementException()
}
hasNext = false
} else {
next = LogPriority.fromInt(next.toInt() + step)
}
return current
}
}
class LogPriorityProgression(
override val start: LogPriority,
override val endInclusive: LogPriority,
private val step: Int = 1
) : Iterable<LogPriority>, ClosedRange<LogPriority> {
override fun iterator(): Iterator<LogPriority> {
return LogPriorityIterator(start, endInclusive, step)
}
}
infix fun downTo(to: LogPriority) = LogPriorityProgression(this, to, -1)
operator fun rangeTo(to: LogPriority) = LogPriorityProgression(this, to)
companion object {
fun fromByte(byte: Byte): LogPriority {
return when (byte.toInt()) {
fun fromByte(byte: Byte): LogPriority = fromInt(byte.toInt())
fun fromInt(int: Int) = when (int) {
0x1 -> DEFAULT
0x2 -> VERBOSE
0x3 -> DEBUG
@@ -41,10 +82,21 @@ enum class LogPriority {
0x7 -> FATAL
0x8 -> SILENT
else -> UNKNOWN
}
}
}
fun toInt() = when (this) {
DEFAULT -> 0x1
VERBOSE -> 0x2
DEBUG -> 0x3
INFO -> 0x4
WARN -> 0x5
ERROR -> 0x6
FATAL -> 0x7
SILENT -> 0x8
else -> 0x0
}
fun toChar(): Char {
return when (this) {
VERBOSE -> 'V'
@@ -37,6 +37,15 @@ class LogViewModel: ViewModel() {
var filterCrashes = false
private set
// skip debug/verbose lines by default
var logLevel = LogPriority.INFO
set(value) {
field = value
_lineState.clear()
loadLogs()
}
fun toggleCrashBuffer() {
filterCrashes = !filterCrashes
@@ -70,8 +79,7 @@ class LogViewModel: ViewModel() {
while (true) {
val line = LogLine.fromBufferedSource(logSource)
// skip debug/verbose lines
if (line.priority >= LogPriority.INFO) {
if (line.priority >= logLevel) {
logLines += line
}