Last active
April 10, 2022 11:23
-
-
Save lucapette/3dd7eca10c47de69864bac844b8d0d04 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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