poissonconsulting / hmstimer

An R package to track elapsed clock time using a `hms::hms()` scalar.
https://poissonconsulting.github.io/hmstimer/
Other
2 stars 0 forks source link

local_tmr() #12

Closed krlmlr closed 1 month ago

krlmlr commented 2 years ago

New function that wraps tmr_timer(start = TRUE) and shows the elapsed time when started and on exit (via withr::defer()). Useful for adding trace messages for debugging, in combination with #11.

joethorley commented 2 years ago

Very interesting - do you want to spend sometime on this together when we are working this fall or do you want us to take a stab before?

krlmlr commented 2 years ago

Either way works for me. For reference, here's my own local_timer() that also shows the caller's location.

local_timer <- function(title = NULL, frame = rlang::caller_env()) {
  caller <- sys.call()
  srcref <- utils::getSrcref(caller)
  file <- utils::getSrcFilename(caller)
  file_line <- paste0(file, ":", srcref[[1]])

  if (is.null(title)) {
    title <- file_line
  } else {
    title <- paste0(file_line, ": ", title)
  }

  tmr <- hmstimer::tmr_timer(start = TRUE)
  message(title)
  withr::defer(message(title, ": ", format(hmstimer::tmr_elapsed(tmr))), frame)

  tmr
}