From 52d1c981f2b25cc812cb558be0ed2b71edfd0d35 Mon Sep 17 00:00:00 2001 From: Hadley Wickham Date: Mon, 5 Aug 2024 16:18:17 -0500 Subject: [PATCH 1/2] Require R 4.0.0 or later Please let me know if you think this is too aggressive a move for logger, but we're seeing very little usage of R 3.6.0 these days. --- DESCRIPTION | 1 + R/hooks.R | 90 ++++++++++++++++------------------------------------- 2 files changed, 28 insertions(+), 63 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index e112b4aa..f489bdf8 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -15,6 +15,7 @@ RoxygenNote: 7.3.2 License: AGPL-3 Imports: utils +Depends: R (>= 4.0.0) Suggests: glue, pander, diff --git a/R/hooks.R b/R/hooks.R index d41dba9d..83a7b30a 100644 --- a/R/hooks.R +++ b/R/hooks.R @@ -1,12 +1,3 @@ -#' Warn to update R to 4+ -#' @keywords internal -warn_if_globalCallingHandlers_is_not_available <- function() { - log_warn( - 'Using legacy version of global message/warning/error hook, ', - 'please update your R installation to at least 4.0.0 ', - 'to make use of the much more elegant globalCallingHandlers approach.') -} - #' Injects a logger call to standard messages #' @@ -17,23 +8,14 @@ warn_if_globalCallingHandlers_is_not_available <- function() { #' message('hi there') #' } log_messages <- function() { - if (R.Version()$major >= 4) { - if (any(sapply(globalCallingHandlers()[names(globalCallingHandlers()) == 'message'], - attr, which = 'implements') == 'log_messages')) { - warning('Ignoring this call to log_messages as it was registered previously.') - } else { - globalCallingHandlers( - message = structure(function(m) { - logger::log_level(logger::INFO, m$message, .topcall = m$call) - }, implements = 'log_messages')) - } + if (any(sapply(globalCallingHandlers()[names(globalCallingHandlers()) == 'message'], + attr, which = 'implements') == 'log_messages')) { + warning('Ignoring this call to log_messages as it was registered previously.') } else { - warn_if_globalCallingHandlers_is_not_available() - invisible(suppressMessages(trace( - what = 'message', - exit = substitute(logger::log_info(logger::skip_formatter(cond$message))), - print = FALSE, - where = baseenv()))) + globalCallingHandlers( + message = structure(function(m) { + logger::log_level(logger::INFO, m$message, .topcall = m$call) + }, implements = 'log_messages')) } } @@ -48,26 +30,17 @@ log_messages <- function() { #' for (i in 1:5) { Sys.sleep(runif(1)); warning(i) } #' } log_warnings <- function(muffle = getOption('logger_muffle_warnings', FALSE)) { - if (R.Version()$major >= 4) { - if (any(sapply(globalCallingHandlers()[names(globalCallingHandlers()) == 'warning'], - attr, which = 'implements') == 'log_warnings')) { - warning('Ignoring this call to log_warnings as it was registered previously.') - } else { - globalCallingHandlers( - warning = structure(function(m) { - logger::log_level(logger::WARN, m$message, .topcall = m$call) - if (isTRUE(muffle)) { - invokeRestart('muffleWarning') - } - }, implements = 'log_warnings')) - } + if (any(sapply(globalCallingHandlers()[names(globalCallingHandlers()) == 'warning'], + attr, which = 'implements') == 'log_warnings')) { + warning('Ignoring this call to log_warnings as it was registered previously.') } else { - warn_if_globalCallingHandlers_is_not_available() - invisible(suppressMessages(trace( - what = 'warning', - tracer = substitute(logger::log_warn(logger::skip_formatter(paste(list(...), collapse = '')))), - print = FALSE, - where = baseenv()))) + globalCallingHandlers( + warning = structure(function(m) { + logger::log_level(logger::WARN, m$message, .topcall = m$call) + if (isTRUE(muffle)) { + invokeRestart('muffleWarning') + } + }, implements = 'log_warnings')) } } @@ -82,26 +55,17 @@ log_warnings <- function(muffle = getOption('logger_muffle_warnings', FALSE)) { #' stop('foobar') #' } log_errors <- function(muffle = getOption('logger_muffle_errors', FALSE)) { - if (R.Version()$major >= 4) { - if (any(sapply(globalCallingHandlers()[names(globalCallingHandlers()) == 'error'], - attr, which = 'implements') == 'log_errors')) { - warning('Ignoring this call to log_errors as it was registered previously.') - } else { - globalCallingHandlers( - error = structure(function(m) { - logger::log_level(logger::ERROR, m$message, .topcall = m$call) - if (isTRUE(muffle)) { - invokeRestart('abort') - } - }, implements = 'log_errors')) - } + if (any(sapply(globalCallingHandlers()[names(globalCallingHandlers()) == 'error'], + attr, which = 'implements') == 'log_errors')) { + warning('Ignoring this call to log_errors as it was registered previously.') } else { - warn_if_globalCallingHandlers_is_not_available() - invisible(suppressMessages(trace( - what = 'stop', - tracer = substitute(logger::log_error(logger::skip_formatter(paste(list(...), collapse = '')))), - print = FALSE, - where = baseenv()))) + globalCallingHandlers( + error = structure(function(m) { + logger::log_level(logger::ERROR, m$message, .topcall = m$call) + if (isTRUE(muffle)) { + invokeRestart('abort') + } + }, implements = 'log_errors')) } } From cf1aba5fa1ee3f85e632b1282c3562f2069dcc90 Mon Sep 17 00:00:00 2001 From: Hadley Wickham Date: Tue, 6 Aug 2024 07:15:44 -0500 Subject: [PATCH 2/2] Redocument --- ...warn_if_globalCallingHandlers_is_not_available.Rd | 12 ------------ 1 file changed, 12 deletions(-) delete mode 100644 man/warn_if_globalCallingHandlers_is_not_available.Rd diff --git a/man/warn_if_globalCallingHandlers_is_not_available.Rd b/man/warn_if_globalCallingHandlers_is_not_available.Rd deleted file mode 100644 index 97ef5164..00000000 --- a/man/warn_if_globalCallingHandlers_is_not_available.Rd +++ /dev/null @@ -1,12 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/hooks.R -\name{warn_if_globalCallingHandlers_is_not_available} -\alias{warn_if_globalCallingHandlers_is_not_available} -\title{Warn to update R to 4+} -\usage{ -warn_if_globalCallingHandlers_is_not_available() -} -\description{ -Warn to update R to 4+ -} -\keyword{internal}