TraceLog/sample-jvm/src/main/kotlin/Main.kt

73 lines
2.2 KiB
Kotlin

package dev.msfjarvis.tracelog.sample
import com.github.ajalt.mordant.rendering.TextColors.*
import com.github.ajalt.mordant.terminal.Terminal
import dev.msfjarvis.tracelog.runtime.annotations.DebugLog
private val messages = arrayListOf<String>()
private const val MATCH_ARROW = "arrow"
private const val MATCH_IDENTIFIER = "identifier"
private const val MATCH_PARAMS = "params"
private const val MATCH_TIME = "time"
private const val MATCH_RETURN = "retval"
private val ENTER_REGEX =
"(?<$MATCH_ARROW>⇢) (?<$MATCH_IDENTIFIER>[a-zA-Z].*)\\((?<$MATCH_PARAMS>.*)\\)".toRegex()
private val EXIT_REGEX =
"(?<$MATCH_ARROW>⇠) (?<$MATCH_IDENTIFIER>[a-zA-Z].*) \\[(?<$MATCH_TIME>.*)] = (?<$MATCH_RETURN>.*)"
.toRegex()
private fun MatchResult.getMatch(groupName: String): String {
return groups[groupName]!!.value
}
public fun main() {
debuggableFunction("First parameter")
val t = Terminal()
messages.forEach { msg ->
var matches = ENTER_REGEX.find(msg)
if (matches != null) {
val params =
matches
.getMatch(MATCH_PARAMS)
.split(", ")
.map {
val split = it.split("=")
(split[0] to split[1])
}
.joinToString(", ") { (name, value) -> "${brightYellow(name)}=${brightBlue(value)}" }
t.println(
"""
${red(matches.getMatch(MATCH_ARROW))} ${green(matches.getMatch(MATCH_IDENTIFIER))}($params)
"""
.trimIndent()
)
} else {
matches = EXIT_REGEX.matchEntire(msg)
if (matches != null) {
t.println(
"""
${red(matches.getMatch(MATCH_ARROW))} ${green(matches.getMatch(MATCH_IDENTIFIER))} [${blue(matches.getMatch(
MATCH_TIME))}] = ${brightWhite(matches.getMatch(MATCH_RETURN))}
"""
.trimIndent()
)
}
}
}
}
/** Custom implementation of a logger function for use by the compiler plugin. */
@Suppress("Unused") // Used by the generated bytecode
public fun recordMessage(message: Any?) {
messages += message.toString()
}
/** An example of an annotated method which will be transformed by the compiler plugin. */
@DebugLog
@Suppress("UNUSED_PARAMETER")
public fun debuggableFunction(p0: String, p1: String = "Bar"): String {
return "Debugging is cool!"
}