1 package android.platform.uiautomator_helpers 2 3 import android.os.Trace 4 import android.util.Log 5 6 /** Tracing utils until androidx tracing library is updated in the tree. */ 7 object TracingUtils { 8 9 // from frameworks/base/core/java/android/os/Trace.java MAX_SECTION_NAME_LEN. 10 private const val MAX_TRACE_NAME_LEN = 127 11 private const val TAG = "TracingUtils" 12 tracenull13 inline fun <T> trace(sectionName: String, block: () -> T): T { 14 Trace.beginSection(sectionName.shortenedIfNeeded()) 15 try { 16 return block() 17 } finally { 18 Trace.endSection() 19 } 20 } 21 22 /** Shortens the section name if it's too long. */ beginSectionSafenull23 fun beginSectionSafe(sectionName: String) { 24 Trace.beginSection(sectionName.shortenedIfNeeded()) 25 } 26 27 /** Shorten the length of a string to make it less than the limit for atraces. */ shortenedIfNeedednull28 fun String.shortenedIfNeeded(): String = 29 if (length > MAX_TRACE_NAME_LEN) { 30 Log.w(TAG, "Section name too long: \"$this\" (len=$length, max=$MAX_TRACE_NAME_LEN)") 31 substring(0, MAX_TRACE_NAME_LEN) 32 } else this 33 } 34