Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid very long log lines and resulting OOM #1073

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions app/src/main/kotlin/at/bitfire/davdroid/log/PlainTextFormatter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

package at.bitfire.davdroid.log

import com.google.common.base.Ascii
import java.io.PrintWriter
import java.io.StringWriter
import java.text.SimpleDateFormat
Expand Down Expand Up @@ -42,6 +43,11 @@ class PlainTextFormatter(
lineSeparator = System.lineSeparator()
)

/**
* Maximum length of a log line (estimate).
*/
const val MAX_LENGTH = 10000

fun shortClassName(className: String) = className
.replace(Regex("^at\\.bitfire\\.(dav|cert4an|dav4an|ical4an|vcard4an)droid\\."), ".")
.replace(Regex("\\$.*$"), "")
Expand Down Expand Up @@ -72,7 +78,7 @@ class PlainTextFormatter(
}
}

builder.append(r.message)
builder.append(truncate(r.message))

if (withException && r.thrown != null) {
val indentedStackTrace = stackTrace(r.thrown)
Expand All @@ -83,7 +89,7 @@ class PlainTextFormatter(

r.parameters?.let {
for ((idx, param) in it.withIndex())
builder.append("\n\tPARAMETER #").append(idx).append(" = ").append(param)
builder.append("\n\tPARAMETER #").append(idx).append(" = ").append(truncate(param.toString()))
}

if (lineSeparator != null)
Expand All @@ -92,4 +98,7 @@ class PlainTextFormatter(
return builder.toString()
}

private fun truncate(s: String) =
Ascii.truncate(s, MAX_LENGTH, "[…]")

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright © All Contributors. See LICENSE and AUTHORS in the root directory for details.
*/

package at.bitfire.davdroid.log

import org.junit.Test
import java.util.logging.Level
import java.util.logging.LogRecord

class PlainTextFormatterTest {

@Test
fun test_format_TruncatesMessage() {
val formatter = PlainTextFormatter.DEFAULT
val result = formatter.format(LogRecord(Level.INFO, "a".repeat(50000)))
// PlainTextFormatter.MAX_LENGTH is 10,000, so the message should be truncated to 10,000 + something
assert(result.length <= 10100)
}

}
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ androidx-work = "2.9.1"
bitfire-cert4android = "f1cc9b9ca3"
bitfire-dav4jvm = "fbd95a5f5a"
bitfire-ical4android = "b75f33972a"
bitfire-vcard4android = "505848a85a"
bitfire-vcard4android = "5439c1f63c"
compose-accompanist = "0.36.0"
compose-bom = "2024.09.03"
dnsjava = "3.6.0"
Expand Down
Loading