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

Commit 00983de6 authored by Jernej Virag's avatar Jernej Virag
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
Change-Id: Iefe7b5fe12054ca5cac97dad2919124bfa0498f1
parent 338b49e1
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 {