diff options
author | Nicolo' Mazzucato <nicomazz@google.com> | 2023-11-03 10:20:14 +0000 |
---|---|---|
committer | Nicolo' Mazzucato <nicomazz@google.com> | 2023-11-03 15:25:36 +0000 |
commit | 2b9763d5c751666bb22d93ab2ccd7caa940d57d4 (patch) | |
tree | dda490d70ef035e577343ae7b4ae40ee73337b4a | |
parent | 1865f112029c8834d67730da0e1e6e076b18d06e (diff) | |
download | systemui-2b9763d5c751666bb22d93ab2ccd7caa940d57d4.tar.gz |
Add Utilities to trace flow values
Flag: None
Bug: 308865451
Bug: 308789093
Test: Perfetto trace
Change-Id: I87b5d97198f1ee7e7d6983481f9cccb0634a2880
-rw-r--r-- | tracinglib/src/com/android/app/tracing/FlowTracing.kt | 33 | ||||
-rw-r--r-- | tracinglib/src/com/android/app/tracing/TraceStateLogger.kt | 7 |
2 files changed, 39 insertions, 1 deletions
diff --git a/tracinglib/src/com/android/app/tracing/FlowTracing.kt b/tracinglib/src/com/android/app/tracing/FlowTracing.kt new file mode 100644 index 0000000..d6fe63e --- /dev/null +++ b/tracinglib/src/com/android/app/tracing/FlowTracing.kt @@ -0,0 +1,33 @@ +/* + * Copyright (C) 2023 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.android.app.tracing + +import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.onEach + +/** Utilities to trace Flows */ +object FlowTracing { + + /** Logs each flow element to a trace. */ + inline fun <T> Flow<T>.traceEach( + flowName: String, + logcat: Boolean = false, + crossinline valueToString: (T) -> String = { it.toString() } + ): Flow<T> { + val stateLogger = TraceStateLogger(flowName, logcat = logcat) + return onEach { stateLogger.log(valueToString(it)) } + } +} diff --git a/tracinglib/src/com/android/app/tracing/TraceStateLogger.kt b/tracinglib/src/com/android/app/tracing/TraceStateLogger.kt index cca22e9..02c8737 100644 --- a/tracinglib/src/com/android/app/tracing/TraceStateLogger.kt +++ b/tracinglib/src/com/android/app/tracing/TraceStateLogger.kt @@ -17,6 +17,7 @@ package com.android.app.tracing import android.os.Trace +import android.util.Log /** * Utility class used to log state changes easily in a track with a custom name. @@ -37,7 +38,8 @@ import android.os.Trace class TraceStateLogger( private val trackName: String, private val logOnlyIfDifferent: Boolean = true, - private val instantEvent: Boolean = true + private val instantEvent: Boolean = true, + private val logcat: Boolean = false, ) { private var previousValue: String? = null @@ -50,6 +52,9 @@ class TraceStateLogger( if (logOnlyIfDifferent && previousValue == newValue) return Trace.asyncTraceForTrackEnd(Trace.TRACE_TAG_APP, trackName, 0) Trace.asyncTraceForTrackBegin(Trace.TRACE_TAG_APP, trackName, newValue, 0) + if (logcat) { + Log.d(trackName, "newValue: $newValue") + } previousValue = newValue } } |