Skip to content

Commit

Permalink
Re-style
Browse files Browse the repository at this point in the history
  • Loading branch information
hadley committed Aug 7, 2024
1 parent ad2dbb4 commit 29eafce
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 60 deletions.
31 changes: 15 additions & 16 deletions R/formatters.R
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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())
Expand All @@ -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",
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.

Check warning on line 209 in R/formatters.R

View workflow job for this annotation

GitHub Actions / lint

file=R/formatters.R,line=209,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 355 characters.
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())

Expand Down Expand Up @@ -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.

Check warning on line 271 in R/formatters.R

View workflow job for this annotation

GitHub Actions / lint

file=R/formatters.R,line=271,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 278 characters.
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))
Expand Down Expand Up @@ -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, ...)
}
Expand Down
38 changes: 21 additions & 17 deletions R/layouts.R
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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())
Expand All @@ -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
Expand Down Expand Up @@ -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))}",
Expand All @@ -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",

Check warning on line 243 in R/layouts.R

View workflow job for this annotation

GitHub Actions / lint

file=R/layouts.R,line=243,col=26,[indentation_linter] Indentation should be 2 spaces but is 26 spaces.
"os_name", "os_release", "os_version", "pid", "user", "msg"
)) {
force(fields)

structure(function(level, msg, namespace = NA_character_,
Expand Down Expand Up @@ -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",

Check warning on line 281 in R/layouts.R

View workflow job for this annotation

GitHub Actions / lint

file=R/layouts.R,line=281,col=33,[indentation_linter] Indentation should be 2 spaces but is 33 spaces.
"os_name", "os_release", "os_version", "pid", "user"
)) {
force(fields)

structure(function(level, msg, namespace = NA_character_,
Expand Down
2 changes: 0 additions & 2 deletions R/logger.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)

}


Expand Down
23 changes: 11 additions & 12 deletions R/try.R
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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
}
)
}
1 change: 0 additions & 1 deletion R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ catch_base_log <- function(level, namespace, .topcall = sys.call(-1), .topenv =
),
type = "message"
)

}

in_pkgdown <- function() {
Expand Down
2 changes: 2 additions & 0 deletions man/formatter_json.Rd

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

7 changes: 5 additions & 2 deletions man/formatter_logging.Rd

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

20 changes: 10 additions & 10 deletions man/layout_glue_colors.Rd

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

0 comments on commit 29eafce

Please sign in to comment.