Skip to content

Commit

Permalink
chore: Improve Log Output
Browse files Browse the repository at this point in the history
Fixing the crash when logging "Vehicle". Instead of outputting one
big log chunk outputting multiple smaller ones.

Signed-off-by: Andre Weber <andre.weber3@etas.com>
  • Loading branch information
wba2hi committed Dec 6, 2023
1 parent fac8d85 commit 480f17f
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -247,8 +247,14 @@ class KuksaDataBrokerActivity : ComponentActivity() {
return
}

result?.entriesList?.withIndex()?.forEach {
val dataEntry = it.value
val index = it.index

outputViewModel.appendOutput(result.toString())
val text = dataEntry.toString().substringAfter("\n")
val withTimestamp = index == 0
outputViewModel.appendOutput(text, withTimestamp)
}
}

override fun onError(error: Throwable) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ fun DataBrokerOutput(viewModel: OutputViewModel, modifier: Modifier = Modifier)
.fillMaxHeight()
.fillMaxWidth()
.padding(start = DefaultElementPadding, end = DefaultElementPadding),
text = outputElement,
text = "\n" + outputElement,
fontSize = 14.sp,
textAlign = TextAlign.Start,
onTextLayout = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,35 +39,23 @@ class OutputViewModel : ViewModel() {
var output: List<String> by mutableStateOf(listOf())
private set

fun appendOutput(text: String) {
fun appendOutput(text: String, withTimestamp: Boolean = true) {
viewModelScope.launch {
withContext(Dispatchers.Main) {
val sanitizedText = sanitizeString(text)

val emptyLines = if (logEntries.isEmpty()) "\n" else "\n\n"
val dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss:SSS")
val date = LocalDateTime.now().format(dateFormatter)
logEntries += "$emptyLines- $date\n $sanitizedText"
var msg = ""
if (withTimestamp) {
msg += "$date\n"
}
msg += text
logEntries += msg

output = logEntries.toList()
}
}
}

// fixes a crash when outputting VssPath(Vehicle). The ScrollBar can't handle input with more than 3971 line breaks
private fun sanitizeString(text: String): String {
var sanitizedText = text
val isTextTooLong = sanitizedText.length >= MAX_LENGTH_LOG_ENTRY
if (isTextTooLong) {
sanitizedText = sanitizedText.substring(0, MAX_LENGTH_LOG_ENTRY) + ""
sanitizedText += System.lineSeparator()
sanitizedText += System.lineSeparator()
sanitizedText += "Text is too long and was truncated"
}

return sanitizedText
}

fun clear() {
viewModelScope.launch {
withContext(Dispatchers.Main) {
Expand All @@ -77,8 +65,4 @@ class OutputViewModel : ViewModel() {
}
}
}

private companion object {
private const val MAX_LENGTH_LOG_ENTRY = 90_000
}
}

0 comments on commit 480f17f

Please sign in to comment.