1 /* 2 * Copyright (C) 2022 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 @file:OptIn(ExperimentalTime::class) 18 19 package com.android.settingslib.spa.framework.compose 20 21 import android.util.Log 22 import androidx.compose.runtime.Composable 23 import androidx.compose.runtime.remember 24 import kotlin.time.ExperimentalTime 25 import kotlin.time.TimeSource 26 27 const val ENABLE_MEASURE_TIME = false 28 29 interface TimeMeasurer { lognull30 fun log(msg: String) {} logFirstnull31 fun logFirst(msg: String) {} 32 33 companion object { 34 private object EmptyTimeMeasurer : TimeMeasurer 35 36 @Composable <lambda>null37 fun rememberTimeMeasurer(tag: String): TimeMeasurer = remember { 38 if (ENABLE_MEASURE_TIME) TimeMeasurerImpl(tag) else EmptyTimeMeasurer 39 } 40 } 41 } 42 43 private class TimeMeasurerImpl(private val tag: String) : TimeMeasurer { 44 private val mark = TimeSource.Monotonic.markNow() 45 private val msgLogged = mutableSetOf<String>() 46 lognull47 override fun log(msg: String) { 48 Log.d(tag, "Timer $msg: ${mark.elapsedNow()}") 49 } 50 logFirstnull51 override fun logFirst(msg: String) { 52 if (msgLogged.add(msg)) { 53 Log.d(tag, "Timer $msg: ${mark.elapsedNow()}") 54 } 55 } 56 } 57