Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 4f57fc60 authored by Jernej Virag's avatar Jernej Virag Committed by Android Build Cherrypicker Worker
Browse files

Add ability to trace Flow values as counters

This works mostly only for Numbers flows and it traces values as a track.

Bug: 354934316
Flag: EXEMPT logging only
Test: recorded a trace
(cherry picked from https://googleplex-android-review.googlesource.com/q/commit:00983de642384ed5dff1f1bea9f6e23a3ec34d40)
Merged-In: Iefe7b5fe12054ca5cac97dad2919124bfa0498f1
Change-Id: Iefe7b5fe12054ca5cac97dad2919124bfa0498f1
parent 1e80b3b7
Loading
Loading
Loading
Loading
+14 −0
Original line number Diff line number Diff line
@@ -46,6 +46,20 @@ object FlowTracing {
        return baseFlow.onEach { stateLogger.log(valueToString(it)) }
    }

    /** Records value of a given numeric flow as a counter track in traces. */
    fun <T : Number> Flow<T>.traceAsCounter(
        counterName: String,
        traceEmissionCount: Boolean = false,
        valueToInt: (T) -> Int = { it.toInt() }
    ): Flow<T> {
        val baseFlow = if (traceEmissionCount) traceEmissionCount(counterName) else this
        return baseFlow.onEach {
            if (isEnabled()) {
                traceCounter(counterName, valueToInt(it))
            }
        }
    }

    /** Adds a counter track to monitor emissions from a specific flow.] */
    fun <T> Flow<T>.traceEmissionCount(flowName: String, uniqueSuffix: Boolean = false): Flow<T> {
        val trackName by lazy {