Skip to content

Commit

Permalink
fix daroczig#157 scoping issues in helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
daroczig committed Jun 17, 2024
1 parent 2ff43d0 commit 8bddb71
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 17 deletions.
45 changes: 29 additions & 16 deletions R/helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -91,20 +91,25 @@ log_eval <- function(expr, level = TRACE, multiline = FALSE) {
#' log_layout(layout_blank)
#' log_separator(ERROR, separator = '!', width = 80)
#' @seealso \code{\link{log_with_separator}}
log_separator <- function(level = INFO,
log_separator <- function(level = INFO,
namespace = NA_character_,
separator = '=',
width = 80,
.topcall = sys.call()) {
.logcall = sys.call(),
.topcall = sys.call(-1),
.topenv = parent.frame()) {

stopifnot(length(separator) == 1, nchar(separator) == 1)

base_info_chars <- nchar(catch_base_log(level, namespace, .topcall = .topcall))
base_info_chars <- nchar(catch_base_log(level, namespace, .topcall = .topcall, .topenv = .topenv))

log_level(
paste(rep(separator, max(0, width - base_info_chars)), collapse = ''),
level = level,
namespace = namespace)
namespace = namespace,
.logcall = .logcall,
.topcall = .topcall,
.topenv = .topenv)
}


Expand Down Expand Up @@ -132,14 +137,16 @@ log_separator <- function(level = INFO,
#' @seealso \code{\link{log_separator}}
log_with_separator <- function(..., level = INFO, namespace = NA_character_, separator = '=', width = 80) {

base_info_chars <- nchar(catch_base_log(level, namespace, .topcall = sys.call()))
base_info_chars <- nchar(catch_base_log(level, namespace, .topcall = sys.call(-1)))

log_separator(
level = level,
level = level,
separator = separator,
width = width,
width = width,
namespace = namespace,
.topcall = call('log_separator')
.logcall = sys.call(),
.topcall = sys.call(-1),
.topenv = parent.frame()
)

message <- do.call(eval(log_formatter()), list(...))
Expand All @@ -151,14 +158,16 @@ log_with_separator <- function(..., level = INFO, namespace = NA_character_, sep
' ', separator)
})

log_level(skip_formatter(message), level = level, namespace = namespace)
log_level(skip_formatter(message), level = level, namespace = namespace, .topenv = parent.frame())

log_separator(
level = level,
level = level,
separator = separator,
width = width,
width = width,
namespace = namespace,
.topcall = call('log_separator')
.logcall = sys.call(),
.topcall = sys.call(-1),
.topenv = parent.frame()
)
}

Expand Down Expand Up @@ -191,10 +200,14 @@ log_tictoc <- function(..., level = INFO, namespace = NA_character_) {
toc <- Sys.time()
tictoc <- difftime(toc, tic)

log_level(paste(ns, 'timer',
ifelse(round(tictoc, 2) == 0, 'tic', 'toc'),
round(tictoc, 2), attr(tictoc, 'units') , '-- '),
..., level = level, namespace = namespace)
log_level(
paste(ns, 'timer',
ifelse(round(tictoc, 2) == 0, 'tic', 'toc'),
round(tictoc, 2), attr(tictoc, 'units') , '-- '),
..., level = level, namespace = namespace,
.logcall = sys.call(),
.topcall = sys.call(-1),
.topenv = parent.frame())

}
tictocs <- new.env()
Expand Down
8 changes: 7 additions & 1 deletion man/log_separator.Rd

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

0 comments on commit 8bddb71

Please sign in to comment.