Skip to content

Instantly share code, notes, and snippets.

@lucapette
Last active April 10, 2022 11:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lucapette/3dd7eca10c47de69864bac844b8d0d04 to your computer and use it in GitHub Desktop.
Save lucapette/3dd7eca10c47de69864bac844b8d0d04 to your computer and use it in GitHub Desktop.
package me.lucapette.ext.klogger
import mu.KLogger
import java.util.WeakHashMap
data class TimeDelta(val sinceStart: Long, val sinceLast: Long)
data class TimeLog(val start: Long, var current: Long) {
fun since(): TimeDelta {
val now = System.currentTimeMillis()
return TimeDelta(now - start, now - current)
}
fun update() {
current = System.currentTimeMillis()
}
}
val store = WeakHashMap<String, TimeLog>()
fun KLogger.time(label: String) {
val now = System.currentTimeMillis()
store[label] = TimeLog(now, now)
}
fun KLogger.timeLog(label: String, message: String) {
val timeLog = store[label] ?: return
val delta = timeLog.since()
info { "[$label ${delta.sinceStart} ${delta.sinceLast}] $message" }
timeLog.update()
}
fun KLogger.timeEnd(label: String) {
val timeLog = store[label] ?: return
val delta = timeLog.since()
info { "[$label ${delta.sinceStart} ${delta.sinceLast}]" }
store.remove(label)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment