TraceUtils updates, new helper for runnables

Modify TraceUtils' traceSection to use traceBegin instead of
beginSection because beginSection will crash at run-time if the string
is longer than 127 chars.

Also, guard against StringBuilder GC garbage while tracing.

Test: manual
Bug: 257075630
Change-Id: I74cb5687a206f5d7ab8eb570cf24715d7e671df1
This commit is contained in:
Peter Kalauskas
2022-11-21 16:53:34 -08:00
parent 8c52651070
commit 46917e5336

View File

@@ -22,11 +22,22 @@ import android.os.Trace
* Run a block within a [Trace] section.
* Calls [Trace.beginSection] before and [Trace.endSection] after the passed block.
*/
inline fun <T> traceSection(tag: String, block: () -> T): T {
Trace.beginSection(tag)
try {
return block()
} finally {
Trace.endSection()
inline fun <T> traceSection(tag: String, block: () -> T): T =
if (Trace.isTagEnabled(Trace.TRACE_TAG_APP)) {
Trace.traceBegin(Trace.TRACE_TAG_APP, tag)
try {
block()
} finally {
Trace.traceEnd(Trace.TRACE_TAG_APP)
}
} else {
block()
}
class TraceUtils {
companion object {
inline fun traceRunnable(tag: String, crossinline block: () -> Unit): Runnable {
return Runnable { traceSection(tag) { block() } }
}
}
}
}