Skip to content

Commit

Permalink
Merge pull request #221 from thomasp85/log_elapsed
Browse files Browse the repository at this point in the history
Add log_elapsed()
  • Loading branch information
daroczig authored Jan 27, 2025
2 parents baf6c03 + 3c0d829 commit 5f15e68
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 1 deletion.
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ export(layout_simple)
export(layout_syslognet)
export(log_appender)
export(log_debug)
export(log_elapsed)
export(log_elapsed_start)
export(log_error)
export(log_errors)
export(log_eval)
Expand Down
3 changes: 2 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# logger (development version)

* Added `log_elapsed()` to show cumulative elapsed running time (@thomasp85)
* New `formatter_cli()` allows you to use the syntax from the cli package to create log messages (#210, @thomasp85)

# logger 0.4.0 (2024-10-19)

Expand All @@ -13,7 +15,6 @@ improved documentations, modernized tests, performance speedups.
* `log_appender()`, `log_layout()` and `log_formatter()` now check that you are calling them with a function, and return the previously set value (#170, @hadley)
* new function to return number of log indices (#194, @WurmPeter)
* `appender_async` is now using `mirai` instead of a custom background process and queue system (#214, @hadley @shikokuchuo)
* New `formatter_cli()` allows you to use the syntax from the cli package to create log messages (#210, @thomasp85)

## Fixes

Expand Down
58 changes: 58 additions & 0 deletions R/helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,64 @@ log_tictoc <- function(..., level = INFO, namespace = NA_character_) {
}
tictocs <- new.env(parent = emptyenv())

#' Log cumulative running time
#'
#' This function is working like [log_tictoc()] but differs in that it continues
#' to count up rather than resetting the timer at every call. You can set the
#' start time using `log_elapsed_start()`, but if that hasn't been called it
#' will show the time since the R session started.
#'
#' @inheritParams log_tictoc
#'
#' @export
#'
#' @examples
#' log_elapsed_start()
#' Sys.sleep(0.4)
#' log_elapsed("Tast 1")
#' Sys.sleep(0.2)
#' log_elapsed("Task 2")
#'
log_elapsed <- function(..., level = INFO, namespace = NA_character_) {
ns <- fallback_namespace(namespace)

start <- get0(ns, envir = elapsed, ifnotfound = 0)

time_elapsed <- as.difftime(proc.time()["elapsed"] - start, units = "secs")

log_level(
paste(
ns, "timer",
round(time_elapsed, 2), attr(time_elapsed, "units"), "elapsed -- "
),
...,
level = level, namespace = namespace,
.logcall = sys.call(),
.topcall = sys.call(-1),
.topenv = parent.frame()
)
}
#' @rdname log_elapsed
#' @param quiet Should starting the time emit a log message
#' @export
log_elapsed_start <- function(level = INFO, namespace = NA_character_, quiet = FALSE) {
ns <- fallback_namespace(namespace)

assign(ns, proc.time()["elapsed"], envir = elapsed)

if (!quiet) {
log_level(
paste(
"starting", ns, "timer"
),
level = level, namespace = namespace,
.logcall = sys.call(),
.topcall = sys.call(-1),
.topenv = parent.frame()
)
}
}
elapsed <- new.env(parent = emptyenv())

#' Logs the error message to console before failing
#' @param expression call
Expand Down
1 change: 1 addition & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,4 @@ needs_stdout <- function() {

# allow mocking
Sys.time <- NULL # nolint
proc.time <- NULL # nolint
1 change: 1 addition & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ reference:
- log_eval
- log_failure
- log_tictoc
- log_elapsed
- log_separator
- log_with_separator
- with_log_threshold
Expand Down
34 changes: 34 additions & 0 deletions man/log_elapsed.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions tests/testthat/test-helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,32 @@ test_that("tictoc", {
expect_output(log_tictoc(), "timer toc 1 mins")
})

test_that("elapsed", {
local_test_logger()

proc_null <- proc.time() * 0

with_mocked_bindings(
expect_output(log_elapsed(), "timer 2 secs elapsed"),
proc.time = function() proc_null + 2
)

with_mocked_bindings(
expect_output(log_elapsed_start(), "starting global timer"),
proc.time = function() proc_null + 1
)

with_mocked_bindings(
expect_output(log_elapsed(), "timer 1 secs elapsed"),
proc.time = function() proc_null + 2
)

with_mocked_bindings(
expect_silent(log_elapsed_start(quiet = TRUE)),
proc.time = function() proc_null
)
})

test_that("log with separator", {
local_test_logger()
expect_output(log_with_separator(42), "===")
Expand Down

0 comments on commit 5f15e68

Please sign in to comment.