Trace


public static class Trace


Summary

Public methods

static final void
beginAsyncSection(@NonNull String methodName, int cookie)

Writes a trace message to indicate that a given section of code has begun.

static final void
endAsyncSection(@NonNull String methodName, int cookie)

Writes a trace message to indicate that the current method has ended.

static final void

Enables the app tracing tag in a non-debuggable process.

static final boolean

Checks whether or not tracing is currently enabled.

static final void
setCounter(@NonNull String counterName, int counterValue)

Writes trace message to indicate the value of a given counter.

static final void
@RequiresApi(value = 29)
setCounter(@NonNull String counterName, long counterValue)

Writes trace message to indicate the value of a given counter.

Public methods

beginAsyncSection

Added in 1.0.0
public static final void beginAsyncSection(@NonNull String methodName, int cookie)

Writes a trace message to indicate that a given section of code has begun.

Must be followed by a call to endAsyncSection with the same methodName and cookie. Unlike beginSection and endSection, asynchronous events do not need to be nested. The name and cookie used to begin an event must be used to end it.

The cookie must be unique to any overlapping events. If events don't overlap, you can simply always pass the same integer (e.g. 0). If they do overlap, the cookie is used to disambiguate between overlapping events, like the following scenario:

[==========================]
[=====================================]
[====]

Without unique cookies, these start/stop timestamps could be misinterpreted by the trace display like the following, to show very different ranges:

[=========================================]
[================]
[==========]
Parameters
@NonNull String methodName

The method name to appear in the trace.

int cookie

Unique identifier for distinguishing simultaneous events with the same methodName.

See also
endAsyncSection

endAsyncSection

Added in 1.0.0
public static final void endAsyncSection(@NonNull String methodName, int cookie)

Writes a trace message to indicate that the current method has ended.

Must be called exactly once for each call to beginAsyncSection using the same name and cookie.

Parameters
@NonNull String methodName

The method name to appear in the trace.

int cookie

Unique identifier for distinguishing simultaneous events with the same methodName.

forceEnableAppTracing

Added in 1.1.0
public static final void forceEnableAppTracing()

Enables the app tracing tag in a non-debuggable process.

Beginning in Android 12 (API 31), app tracing - custom tracing performed by app code via this class or android.os.Trace - is always enabled in all apps. Prior to this, app tracing was only enabled in debuggable apps (as well as profileable apps, on API 29/30).

Calling this method enables the app to record custom trace content without debuggable=true on any platform version that supports tracing. Tracing of non-debuggable apps is highly recommended, to ensure accurate performance measurements.

As app tracing is always enabled on Android 12 (API 31) and above, this does nothing after API 31.

isEnabled

Added in 1.0.0
public static final boolean isEnabled()

Checks whether or not tracing is currently enabled.

This is useful to avoid intermediate string creation for trace sections that require formatting. It is not necessary to guard all Trace method calls as they internally already check this. However, it is recommended to use this to prevent creating any temporary objects that would then be passed to those methods to reduce runtime cost when tracing isn't enabled.

Returns
boolean

true if tracing is currently enabled, false otherwise.

setCounter

Added in 1.0.0
public static final void setCounter(@NonNull String counterName, int counterValue)

Writes trace message to indicate the value of a given counter.

Parameters
@NonNull String counterName

The counter name to appear in the trace.

int counterValue

The counter value.

setCounter

Added in 2.0.0-rc01
@RequiresApi(value = 29)
public static final void setCounter(@NonNull String counterName, long counterValue)

Writes trace message to indicate the value of a given counter.

Parameters
@NonNull String counterName

The counter name to appear in the trace.

long counterValue

The counter value.