Skip to content

Instantly share code, notes, and snippets.

@ntlv
Created March 4, 2022 09:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ntlv/6dc73de3c02ec702842200eb257e0c0c to your computer and use it in GitHub Desktop.
Save ntlv/6dc73de3c02ec702842200eb257e0c0c to your computer and use it in GitHub Desktop.
/*
* Copyright 2020 Google LLC
*
* 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.
*/
@file:Suppress("NOTHING_TO_INLINE")
package app.tivi.common.compose
import android.util.Log
import androidx.compose.runtime.Composable
import androidx.compose.runtime.SideEffect
import androidx.compose.runtime.remember
class Ref(var value: Int)
const val EnableDebugCompositionLogs = false
/**
* An effect which logs the number compositions at the invoked point of the slot table.
* Thanks to [objcode](https://github.com/objcode) for this code.
*
* This is an inline function to act as like a C-style #include to the host composable function.
* That way we track it's compositions, not this function's compositions.
*
* @param tag Log tag used for [Log.d]
*/
@Composable
inline fun LogCompositions(tag: String) {
if (EnableDebugCompositionLogs && BuildConfig.DEBUG) {
val ref = remember { Ref(0) }
SideEffect { ref.value++ }
Log.d(tag, "Compositions: ${ref.value}")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment