Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 779
Elye Avatar asked Oct 25 '25 17:10

Elye


1 Answers

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)
    }
}
like image 163
David Rawson Avatar answered Oct 27 '25 12:10

David Rawson