Skip to content

lisonge/kotlin-loc

Repository files navigation

kotlin-loc

Maven Central License

A Kotlin compilation plugin used to replace specific variables with the current code location, which can solve the problem of Throwable().printStackTrace() being unfriendly to minimization and Kotlin/Native.

package example

import li.songe.loc.Loc

fun main() {
    test1("Hello, World!")
    // [example.ExampleKt.main(Example.kt:9)]: Hello, World!
    test2("Hello, World!")
    // [Example.kt:11]: Hello, World!
    test3("Hello, World!")
    // [13]: Hello, World!
    println(Throwable().stackTraceToString())
    // see the following stack trace (jvm/mingwX64)
}

fun test1(message: String, @Loc loc: String = "") {
    println("[$loc]: $message")
}

fun test2(message: String, @Loc("{fileName}:{lineNumber}") loc: String = "") {
    println("[$loc]: $message")
}

fun test3(message: String, @Loc("{lineNumber}") loc: Int = 0) {
    println("[$loc] $message")
}
trace-jvm
java.lang.Throwable
    at example.ExampleKt.main(Example.kt:11)
    at example.ExampleKt.main(Example.kt)
trace-android(isMinifyEnabled)
java.lang.Throwable
    at a.b.c(b.kt:3)
    at a.b.c(b.kt)
trace-mingwX64
kotlin.Throwable
    at 0   ???                                 7ff6baa398c6       kfun:kotlin.Throwable#<init>(){} + 70
    at 1   ???                                 7ff6baa6700e       kfun:#main(){} + 174
    at 2   ???                                 7ff6baa6726f       Konan_start + 111
    at 3   ???                                 7ff6baa85633       Init_and_run_start + 99
    at 4   ???                                 7ff6baa313b4       __tmainCRTStartup + 564
    at 5   ???                                 7ff6baa3150b       mainCRTStartup + 27
    at 6   ???                                 7ff8148ee8d7       _ZSt25__throw_bad_function_callv + 5803052231
    at 7   ???                                 7ff81548c53c       _ZSt25__throw_bad_function_callv + 5815232812

Usage

# gradle/libs.versions.toml
[versions]
loc = "<version>" # https://github.com/lisonge/kotlin-loc/releases

[libraries]
loc-annotation = { module = "li.songe.loc:loc-annotation", version.ref = "loc" }

[plugins]
loc = { id = "li.songe.loc", version.ref = "loc" }
// build.gradle.kts
plugins {
    alias(libs.plugins.loc) apply false
}
// example/build.gradle.kts
plugins {
    kotlin("multiplatform")
    alias(libs.plugins.loc)
}

kotlin {
    jvm()
    sourceSets {
        commonMain {
            dependencies {
                compileOnly(libs.loc.annotation)
            }
        }
    }
}

// Optional configuration
// loc { }

Configuration

loc {
    // the default configuration
    template = "{className}.{methodName}({fileName}:{lineNumber})"
    // output -> example.ExampleKt.main(Example.kt:6)
}
// use default template
fun test1(message: String, @Loc loc: String = "") {
    println("[$loc] $message")
}
test1("Hello, World!")
// [example.ExampleKt.main(Example.kt:9)]: Hello, World!

// custom string template
fun test2(message: String, @Loc("{fileName}:{lineNumber}") loc: String = "") {
    println("[$loc] $message")
}
test2("Hello, World!")
// [Example.kt:9]: Hello, World!

// custom int template
fun test3(message: String, @Loc("{lineNumber}") loc: Int = 0) {
    println("[$loc] $message")
}
test3("Hello, World!")
// [9]: Hello, World!
template type example
{filePath} string example/src/commonMain/kotlin/example/Example.kt
{fileName} string Example.kt
{className} string example.ExampleKt
{packageName} string example
{methodName} string main
{lineNumber} string int 6
{columnNumber} string int 5
{offsetNumber} string int 18

About

A Kotlin compilation plugin used to replace specific variables with the current code location

Topics

Resources

Stars

3 stars

Watchers

0 watching

Forks

Contributors

Languages