All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.squareup.anvil.compiler.codegen.ksp.KspTracer.kt Maven / Gradle / Ivy

Go to download

The core implementation module for Anvil, responsible for hooking into the Kotlin compiler and orchestrating code generation

There is a newer version: 0.4.0
Show newest version
package com.squareup.anvil.compiler.codegen.ksp

import com.google.devtools.ksp.processing.SymbolProcessorEnvironment
import com.squareup.anvil.compiler.OPTION_VERBOSE
import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.contract
import kotlin.time.measureTimedValue

internal interface KspTracer {
  fun log(message: String)
}

internal class KspTracerImpl(
  env: SymbolProcessorEnvironment,
  private val logTag: String,
) : KspTracer {
  private val logger = env.logger
  private val verbose = env.options[OPTION_VERBOSE]?.toBoolean() ?: false

  override fun log(message: String) {
    val messageWithTag = "[Anvil] [$logTag] $message"
    if (verbose) {
      logger.warn(messageWithTag)
    } else {
      logger.info(messageWithTag)
    }
  }
}

@OptIn(ExperimentalContracts::class)
internal inline fun  KspTracer.trace(message: String, block: () -> T): T {
  contract { callsInPlace(block, kotlin.contracts.InvocationKind.EXACTLY_ONCE) }
  val (result, duration) = measureTimedValue(block)
  val durationMs = duration.inWholeMilliseconds
  val prefix = if (durationMs > 200) {
    "[SLOW] "
  } else {
    ""
  }
  log("$prefix$message took ${durationMs}ms")
  return result
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy