Skip to content

Commit cd89a6f

Browse files
authored
Merge branch 'main' into issue110-location-log
2 parents 889fc9a + 5f15e68 commit cd89a6f

File tree

7 files changed

+123
-0
lines changed

7 files changed

+123
-0
lines changed

NAMESPACE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ export(layout_simple)
5454
export(layout_syslognet)
5555
export(log_appender)
5656
export(log_debug)
57+
export(log_elapsed)
58+
export(log_elapsed_start)
5759
export(log_error)
5860
export(log_errors)
5961
export(log_eval)

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# logger (development version)
22

33
* File and line location of the log call is now available to the layouts (#110, @thomasp85)
4+
* Added `log_elapsed()` to show cumulative elapsed running time (@thomasp85)
45
* New `formatter_cli()` allows you to use the syntax from the cli package to create log messages (#210, @thomasp85)
56

67
# logger 0.4.0 (2024-10-19)

R/helpers.R

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,64 @@ log_tictoc <- function(..., level = INFO, namespace = NA_character_) {
227227
}
228228
tictocs <- new.env(parent = emptyenv())
229229

230+
#' Log cumulative running time
231+
#'
232+
#' This function is working like [log_tictoc()] but differs in that it continues
233+
#' to count up rather than resetting the timer at every call. You can set the
234+
#' start time using `log_elapsed_start()`, but if that hasn't been called it
235+
#' will show the time since the R session started.
236+
#'
237+
#' @inheritParams log_tictoc
238+
#'
239+
#' @export
240+
#'
241+
#' @examples
242+
#' log_elapsed_start()
243+
#' Sys.sleep(0.4)
244+
#' log_elapsed("Tast 1")
245+
#' Sys.sleep(0.2)
246+
#' log_elapsed("Task 2")
247+
#'
248+
log_elapsed <- function(..., level = INFO, namespace = NA_character_) {
249+
ns <- fallback_namespace(namespace)
250+
251+
start <- get0(ns, envir = elapsed, ifnotfound = 0)
252+
253+
time_elapsed <- as.difftime(proc.time()["elapsed"] - start, units = "secs")
254+
255+
log_level(
256+
paste(
257+
ns, "timer",
258+
round(time_elapsed, 2), attr(time_elapsed, "units"), "elapsed -- "
259+
),
260+
...,
261+
level = level, namespace = namespace,
262+
.logcall = sys.call(),
263+
.topcall = sys.call(-1),
264+
.topenv = parent.frame()
265+
)
266+
}
267+
#' @rdname log_elapsed
268+
#' @param quiet Should starting the time emit a log message
269+
#' @export
270+
log_elapsed_start <- function(level = INFO, namespace = NA_character_, quiet = FALSE) {
271+
ns <- fallback_namespace(namespace)
272+
273+
assign(ns, proc.time()["elapsed"], envir = elapsed)
274+
275+
if (!quiet) {
276+
log_level(
277+
paste(
278+
"starting", ns, "timer"
279+
),
280+
level = level, namespace = namespace,
281+
.logcall = sys.call(),
282+
.topcall = sys.call(-1),
283+
.topenv = parent.frame()
284+
)
285+
}
286+
}
287+
elapsed <- new.env(parent = emptyenv())
230288

231289
#' Logs the error message to console before failing
232290
#' @param expression call

R/utils.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,3 +142,4 @@ needs_stdout <- function() {
142142

143143
# allow mocking
144144
Sys.time <- NULL # nolint
145+
proc.time <- NULL # nolint

_pkgdown.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ reference:
2323
- log_eval
2424
- log_failure
2525
- log_tictoc
26+
- log_elapsed
2627
- log_separator
2728
- log_with_separator
2829
- with_log_threshold

man/log_elapsed.Rd

Lines changed: 34 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-helpers.R

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,32 @@ test_that("tictoc", {
1717
expect_output(log_tictoc(), "timer toc 1 mins")
1818
})
1919

20+
test_that("elapsed", {
21+
local_test_logger()
22+
23+
proc_null <- proc.time() * 0
24+
25+
with_mocked_bindings(
26+
expect_output(log_elapsed(), "timer 2 secs elapsed"),
27+
proc.time = function() proc_null + 2
28+
)
29+
30+
with_mocked_bindings(
31+
expect_output(log_elapsed_start(), "starting global timer"),
32+
proc.time = function() proc_null + 1
33+
)
34+
35+
with_mocked_bindings(
36+
expect_output(log_elapsed(), "timer 1 secs elapsed"),
37+
proc.time = function() proc_null + 2
38+
)
39+
40+
with_mocked_bindings(
41+
expect_silent(log_elapsed_start(quiet = TRUE)),
42+
proc.time = function() proc_null
43+
)
44+
})
45+
2046
test_that("log with separator", {
2147
local_test_logger()
2248
expect_output(log_with_separator(42), "===")

0 commit comments

Comments
 (0)