Skip to content

Commit

Permalink
chore: Truncate Logs
Browse files Browse the repository at this point in the history
  • Loading branch information
wba2hi committed Oct 19, 2023
1 parent 5369dfa commit ab154ad
Showing 1 changed file with 21 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,32 @@ class OutputViewModel : ViewModel() {
fun appendOutput(text: String) {
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 $text"
logEntries += "$emptyLines- $date\n $sanitizedText"

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 @@ -61,4 +77,8 @@ class OutputViewModel : ViewModel() {
}
}
}

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

0 comments on commit ab154ad

Please sign in to comment.