commonMain.trace-consumer.kt Maven / Gradle / Ivy
package kotlinx.html.consumers
import kotlinx.html.*
class TraceConsumer(val downstream: TagConsumer, val println: (String) -> Unit) : TagConsumer by downstream {
private val id = "ID-${currentTimeMillis() % 16384}"
private val path = ArrayList(1024)
override fun onTagStart(tag: Tag) {
downstream.onTagStart(tag)
path.add(tag.tagName)
println("[$id] open ${tag.tagName} path: ${path.joinToString(" > ")}")
}
override fun onTagEnd(tag: Tag) {
downstream.onTagEnd(tag)
path.removeAt(path.lastIndex)
println("[$id] close ${tag.tagName} path: ${path.joinToString(" > ")}")
}
override fun onTagAttributeChange(tag: Tag, attribute: String, value: String?) {
downstream.onTagAttributeChange(tag, attribute, value)
println("[$id] ${tag.tagName}.$attribute changed to $value")
}
override fun finalize(): R {
val v = downstream.finalize()
println("[$id] finalized: ${v.toString()}")
return v
}
}
fun TagConsumer.trace(println: (String) -> Unit): TagConsumer = TraceConsumer(this, println)
//header fun TagConsumer.trace() : TagConsumer
© 2015 - 2025 Weber Informatics LLC | Privacy Policy