From 29eafce92568ec5cca27bc89c8840e0fddf97cb3 Mon Sep 17 00:00:00 2001 From: Hadley Wickham Date: Wed, 7 Aug 2024 16:59:57 -0500 Subject: [PATCH] Re-style --- R/formatters.R | 31 +++++++++++++++---------------- R/layouts.R | 38 +++++++++++++++++++++----------------- R/logger.R | 2 -- R/try.R | 23 +++++++++++------------ R/utils.R | 1 - man/formatter_json.Rd | 2 ++ man/formatter_logging.Rd | 7 +++++-- man/layout_glue_colors.Rd | 20 ++++++++++---------- 8 files changed, 64 insertions(+), 60 deletions(-) diff --git a/R/formatters.R b/R/formatters.R index c8c9a184..92b400ff 100644 --- a/R/formatters.R +++ b/R/formatters.R @@ -10,7 +10,7 @@ #' and [skip_formatter()] for marking a string not to apply the #' formatter on it. formatter_paste <- function(..., - .logcall = sys.call(), .topcall = sys.call(-1), .topenv = parent.frame()) { + .logcall = sys.call(), .topcall = sys.call(-1), .topenv = parent.frame()) { paste(...) } attr(formatter_paste, "generator") <- quote(formatter_paste()) @@ -28,7 +28,7 @@ attr(formatter_paste, "generator") <- quote(formatter_paste()) #' and [skip_formatter()] for marking a string not to apply the #' formatter on it. formatter_sprintf <- function(fmt, ..., - .logcall = sys.call(), .topcall = sys.call(-1), .topenv = parent.frame()) { + .logcall = sys.call(), .topcall = sys.call(-1), .topenv = parent.frame()) { sprintf(fmt, ...) } attr(formatter_sprintf, "generator") <- quote(formatter_sprintf()) @@ -49,12 +49,12 @@ attr(formatter_sprintf, "generator") <- quote(formatter_sprintf()) #' formatter on it. #' @importFrom utils str formatter_glue <- function(..., .logcall = sys.call(), .topcall = sys.call(-1), .topenv = parent.frame()) { - fail_on_missing_package('glue') + fail_on_missing_package("glue") - withCallingHandlers( - glue::glue(..., .envir = .topenv), - error = function(e) { - args <- paste0(capture.output(str(...)), collapse = '\n') + withCallingHandlers( + glue::glue(..., .envir = .topenv), + error = function(e) { + args <- paste0(capture.output(str(...)), collapse = "\n") stop(paste0( "`glue` failed in `formatter_glue` on:\n\n", @@ -83,7 +83,7 @@ attr(formatter_glue, "generator") <- quote(formatter_glue()) #' formatter on it. #' @importFrom utils str formatter_glue_safe <- function(..., - .logcall = sys.call(), .topcall = sys.call(-1), .topenv = parent.frame()) { + .logcall = sys.call(), .topcall = sys.call(-1), .topenv = parent.frame()) { fail_on_missing_package("glue") as.character( tryCatch( @@ -136,8 +136,8 @@ attr(formatter_glue_safe, "generator") <- quote(formatter_glue_safe()) #' formatter_glue_or_sprintf("Hi %s, did you know that 2*4=%s", c("foo", "bar"), 2 * 4) #' } formatter_glue_or_sprintf <- function(msg, ..., - .logcall = sys.call(), .topcall = sys.call(-1), - .topenv = parent.frame()) { + .logcall = sys.call(), .topcall = sys.call(-1), + .topenv = parent.frame()) { params <- list(...) ## params without a name are potential sprintf params @@ -208,8 +208,8 @@ attr(formatter_glue_or_sprintf, "generator") <- quote(formatter_glue_or_sprintf( #' } #' @seealso This is a [log_formatter()] potentially to be used with [layout_json_parser()], for alternatives, see [formatter_paste()], [formatter_sprintf()], [formatter_glue()], [formatter_glue_safe()], [formatter_glue_or_sprintf()], [formatter_logging()], [formatter_pander()] and [skip_formatter()] for marking a string not to apply the formatter on it. formatter_json <- function(..., .logcall = sys.call(), .topcall = sys.call(-1), .topenv = parent.frame()) { - fail_on_missing_package('jsonlite') - eval(as.character(jsonlite::toJSON(list(...), auto_unbox = TRUE)), envir = .topenv) + fail_on_missing_package("jsonlite") + eval(as.character(jsonlite::toJSON(list(...), auto_unbox = TRUE)), envir = .topenv) } attr(formatter_json, "generator") <- quote(formatter_json()) @@ -270,9 +270,8 @@ skip_formatter <- function(message, ...) { #' @export #' @seealso This is a [log_formatter()], for alternatives, see [formatter_paste()], [formatter_glue()], [formatter_glue_safe()], [formatter_glue_or_sprintf()], [formatter_json()], [formatter_pander()] and [skip_formatter()] for marking a string not to apply the formatter on it. formatter_logging <- function(..., .logcall = sys.call(), .topcall = sys.call(-1), .topenv = parent.frame()) { - - params <- list(...) - .logcall <- substitute(.logcall) + params <- list(...) + .logcall <- substitute(.logcall) if (is.character(params[[1]])) { return(do.call(sprintf, params, envir = .topenv)) @@ -306,7 +305,7 @@ attr(formatter_logging, "generator") <- quote(formatter_logging()) #' log_info(lm(hp ~ wt, mtcars)) #' } formatter_pander <- function(x, ..., - .logcall = sys.call(), .topcall = sys.call(-1), .topenv = parent.frame()) { + .logcall = sys.call(), .topcall = sys.call(-1), .topenv = parent.frame()) { fail_on_missing_package("pander") pander::pander_return(x, ...) } diff --git a/R/layouts.R b/R/layouts.R index b70c9e2d..ab426566 100644 --- a/R/layouts.R +++ b/R/layouts.R @@ -120,7 +120,7 @@ layout_glue_generator <- function(format = '{level} [{format(time, "%Y-%m-%d %H: #' [layout_simple()], [layout_glue_colors()], [layout_json()], or #' generator functions such as [layout_glue_generator()] layout_blank <- function(level, msg, namespace = NA_character_, - .logcall = sys.call(), .topcall = sys.call(-1), .topenv = parent.frame()) { + .logcall = sys.call(), .topcall = sys.call(-1), .topenv = parent.frame()) { msg } attr(layout_blank, "generator") <- quote(layout_blank()) @@ -136,7 +136,7 @@ attr(layout_blank, "generator") <- quote(layout_blank()) #' [layout_json()], [layout_json_parser()], or generator functions #' such as [layout_glue_generator()] layout_simple <- function(level, msg, namespace = NA_character_, - .logcall = sys.call(), .topcall = sys.call(-1), .topenv = parent.frame()) { + .logcall = sys.call(), .topcall = sys.call(-1), .topenv = parent.frame()) { paste0(attr(level, "level"), " [", format(Sys.time(), "%Y-%m-%d %H:%M:%S"), "] ", msg) } attr(layout_simple, "generator") <- quote(layout_simple()) @@ -159,7 +159,7 @@ attr(layout_simple, "generator") <- quote(layout_simple()) #' logger_tester_function(INFO, 42) #' } layout_logging <- function(level, msg, namespace = NA_character_, - .logcall = sys.call(), .topcall = sys.call(-1), .topenv = parent.frame()) { + .logcall = sys.call(), .topcall = sys.call(-1), .topenv = parent.frame()) { meta <- get_logger_meta_variables( log_level = level, namespace = namespace, .logcall = .logcall, .topcall = .topcall, .topenv = .topenv @@ -205,16 +205,16 @@ attr(layout_glue, "generator") <- quote(layout_glue()) #' such as [layout_glue_generator()] #' @note This functionality depends on the \pkg{crayon} package. #' @examplesIf requireNamespace("crayon") -#' log_layout(layout_glue_colors) -#' log_threshold(TRACE) -#' log_info("Starting the script...") -#' log_debug("This is the second line") -#' log_trace("That is being placed right after the first one.") -#' log_warn("Some errors might come!") -#' log_error("This is a problem") -#' log_debug("Getting an error is usually bad") -#' log_error("This is another problem") -#' log_fatal("The last problem.") +#' log_layout(layout_glue_colors) +#' log_threshold(TRACE) +#' log_info("Starting the script...") +#' log_debug("This is the second line") +#' log_trace("That is being placed right after the first one.") +#' log_warn("Some errors might come!") +#' log_error("This is a problem") +#' log_debug("Getting an error is usually bad") +#' log_error("This is another problem") +#' log_fatal("The last problem.") layout_glue_colors <- layout_glue_generator( format = paste( "{crayon::bold(colorize_by_log_level(level, levelr))}", @@ -239,8 +239,10 @@ attr(layout_glue_colors, "generator") <- quote(layout_glue_colors()) #' log_info(42) #' log_info("ok {1:3} + {1:3} = {2*(1:3)}") #' } -layout_json <- function(fields = c("time", "level", "ns", "ans", "topenv", "fn", "node", "arch", - "os_name", "os_release", "os_version", "pid", "user", "msg")) { +layout_json <- function(fields = c( + "time", "level", "ns", "ans", "topenv", "fn", "node", "arch", + "os_name", "os_release", "os_version", "pid", "user", "msg" + )) { force(fields) structure(function(level, msg, namespace = NA_character_, @@ -275,8 +277,10 @@ layout_json <- function(fields = c("time", "level", "ns", "ans", "topenv", "fn", #' log_layout(layout_json_parser(fields = c("time", "node"))) #' log_info(cars = row.names(mtcars), species = unique(iris$Species)) #' } -layout_json_parser <- function(fields = c("time", "level", "ns", "ans", "topenv", "fn", "node", "arch", - "os_name", "os_release", "os_version", "pid", "user")) { +layout_json_parser <- function(fields = c( + "time", "level", "ns", "ans", "topenv", "fn", "node", "arch", + "os_name", "os_release", "os_version", "pid", "user" + )) { force(fields) structure(function(level, msg, namespace = NA_character_, diff --git a/R/logger.R b/R/logger.R index 894494cf..5ab66dea 100644 --- a/R/logger.R +++ b/R/logger.R @@ -109,7 +109,6 @@ fallback_namespace <- function(namespace) { #' If `value` is not `NULL`, will return the previously set value. #' @noRd log_config_setter <- function(name, value, namespace = "global", index = 1) { - if (length(namespace) > 1) { for (ns in namespace) { log_config_setter(name, value, ns, index) @@ -141,7 +140,6 @@ log_config_setter <- function(name, value, namespace = "global", index = 1) { configs[[min(index, length(config) + 1)]] <- config assign(namespace, configs, envir = namespaces) invisible(old) - } diff --git a/R/try.R b/R/try.R index 527e51ed..79eaaea0 100644 --- a/R/try.R +++ b/R/try.R @@ -15,7 +15,6 @@ #' FunDoesNotExist(1:10) %except% MEAN(1:10) %except% mean(1:10) #' FunDoesNotExist(1:10) %except% (MEAN(1:10) %except% mean(1:10)) `%except%` <- function(try, except) { - ## Need to capture these in the evaluation frame of `%except%` but only want ## to do the work if there's an error delayedAssign("call", sys.call(-1)) @@ -24,15 +23,15 @@ delayedAssign("try_text", deparse(substitute(try))) tryCatch(try, - error = function(e) { - log_level( - WARN, - paste0("Running '", except_text, "' as '", try_text, "' failed: '", e$message, "'"), - namespace = "except", - .topcall = call, - .topenv = env - ) - except - }) - + error = function(e) { + log_level( + WARN, + paste0("Running '", except_text, "' as '", try_text, "' failed: '", e$message, "'"), + namespace = "except", + .topcall = call, + .topenv = env + ) + except + } + ) } diff --git a/R/utils.R b/R/utils.R index 8f6944ef..23faa0fc 100644 --- a/R/utils.R +++ b/R/utils.R @@ -95,7 +95,6 @@ catch_base_log <- function(level, namespace, .topcall = sys.call(-1), .topenv = ), type = "message" ) - } in_pkgdown <- function() { diff --git a/man/formatter_json.Rd b/man/formatter_json.Rd index d0f4c102..65b2260a 100644 --- a/man/formatter_json.Rd +++ b/man/formatter_json.Rd @@ -51,4 +51,6 @@ This is a \code{\link[=log_formatter]{log_formatter()}} potentially to be used w \code{\link[=formatter_logging]{formatter_logging()}}, \code{\link[=formatter_pander]{formatter_pander()}} and \code{\link[=skip_formatter]{skip_formatter()}} for marking a string not to apply the formatter on it. + +This is a \code{\link[=log_formatter]{log_formatter()}} potentially to be used with \code{\link[=layout_json_parser]{layout_json_parser()}}, for alternatives, see \code{\link[=formatter_paste]{formatter_paste()}}, \code{\link[=formatter_sprintf]{formatter_sprintf()}}, \code{\link[=formatter_glue]{formatter_glue()}}, \code{\link[=formatter_glue_safe]{formatter_glue_safe()}}, \code{\link[=formatter_glue_or_sprintf]{formatter_glue_or_sprintf()}}, \code{\link[=formatter_logging]{formatter_logging()}}, \code{\link[=formatter_pander]{formatter_pander()}} and \code{\link[=skip_formatter]{skip_formatter()}} for marking a string not to apply the formatter on it. } diff --git a/man/formatter_logging.Rd b/man/formatter_logging.Rd index 712996ed..c207b5d7 100644 --- a/man/formatter_logging.Rd +++ b/man/formatter_logging.Rd @@ -12,8 +12,7 @@ formatter_logging( ) } \arguments{ -\item{...}{string and further params passed to \code{sprintf} or R -expressions to be evaluated} +\item{...}{string and further params passed to \code{sprintf} or R expressions to be evaluated} \item{.logcall}{the logging call being evaluated (useful in formatters and layouts when you want to have access to the raw, @@ -28,6 +27,8 @@ where the formatter function will be evaluated and that is used to look up the \code{namespace} as well via \code{logger:::top_env_name}} } \value{ +character vector + character vector } \description{ @@ -54,4 +55,6 @@ This is a \code{\link[=log_formatter]{log_formatter()}}, for alternatives, see \code{\link[=formatter_glue_safe]{formatter_glue_safe()}}, \code{\link[=formatter_glue_or_sprintf]{formatter_glue_or_sprintf()}}, \code{\link[=formatter_json]{formatter_json()}}, \code{\link[=formatter_pander]{formatter_pander()}} and \code{\link[=skip_formatter]{skip_formatter()}} for marking a string not to apply the formatter on it. + +This is a \code{\link[=log_formatter]{log_formatter()}}, for alternatives, see \code{\link[=formatter_paste]{formatter_paste()}}, \code{\link[=formatter_glue]{formatter_glue()}}, \code{\link[=formatter_glue_safe]{formatter_glue_safe()}}, \code{\link[=formatter_glue_or_sprintf]{formatter_glue_or_sprintf()}}, \code{\link[=formatter_json]{formatter_json()}}, \code{\link[=formatter_pander]{formatter_pander()}} and \code{\link[=skip_formatter]{skip_formatter()}} for marking a string not to apply the formatter on it. } diff --git a/man/layout_glue_colors.Rd b/man/layout_glue_colors.Rd index 9e9e00fc..b9192d04 100644 --- a/man/layout_glue_colors.Rd +++ b/man/layout_glue_colors.Rd @@ -49,16 +49,16 @@ This functionality depends on the \pkg{crayon} package. } \examples{ \dontshow{if (requireNamespace("crayon")) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} - log_layout(layout_glue_colors) - log_threshold(TRACE) - log_info("Starting the script...") - log_debug("This is the second line") - log_trace("That is being placed right after the first one.") - log_warn("Some errors might come!") - log_error("This is a problem") - log_debug("Getting an error is usually bad") - log_error("This is another problem") - log_fatal("The last problem.") +log_layout(layout_glue_colors) +log_threshold(TRACE) +log_info("Starting the script...") +log_debug("This is the second line") +log_trace("That is being placed right after the first one.") +log_warn("Some errors might come!") +log_error("This is a problem") +log_debug("Getting an error is usually bad") +log_error("This is another problem") +log_fatal("The last problem.") \dontshow{\}) # examplesIf} } \seealso{