Search code examples
kotlinkotlin-symbol-processing

How to print a log in Kotlin KSP Processor?


In Kotlin KSP, I try to debug the processor

internal class ListedProcessor(
    private val fileGenerator: FileWriter,
) : SymbolProcessor {

    override fun process(resolver: Resolver): List<KSAnnotated> {

        val listedFunctions: Sequence<KSFunctionDeclaration> =
            resolver.findAnnotations(Listed::class)
        val arrayedFunctions: Sequence<KSFunctionDeclaration> =
            resolver.findAnnotations(Arrayed::class)

        return (listedFunctions + arrayedFunctions).filterNot { it.validate() }.toList()
    }
}

I try to use println but don't know where the output goes to. How can I dump out log for Kotlin KSP?


Solution

  • As per the conversation in Kotlin Slack, please use the KSPLogger to perform logging. For example:

    logger.warn(allFiles.toList().toString())
    

    You can get a handle on the KSPLogger through the environment parameter in the create function of your SymbolProcessorProvider:

    class TestProcessorProvider : SymbolProcessorProvider {
        override fun create(environment: SymbolProcessorEnvironment): SymbolProcessor {
            return TestProcessor(environment.codeGenerator, environment.logger)
        }
    }