Skip to content

Commit

Permalink
Merge pull request #193 from hadley/run-examples
Browse files Browse the repository at this point in the history
Run more examples
  • Loading branch information
daroczig authored Sep 10, 2024
2 parents dac2ce2 + 878ffc2 commit e05f1a8
Show file tree
Hide file tree
Showing 35 changed files with 175 additions and 250 deletions.
12 changes: 6 additions & 6 deletions R/appenders.R
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ attr(appender_stdout, "generator") <- quote(appender_stdout())
#' @export
#' @return function taking `lines` argument
#' @family `log_appenders`
#' @examples \dontrun{
#' @examples
#' \dontshow{old <- logger:::namespaces_set()}
#' ## ##########################################################################
#' ## simple example logging to a file
#' t <- tempfile()
Expand All @@ -70,6 +71,8 @@ attr(appender_stdout, "generator") <- quote(appender_stdout())
#' ## define the file logger with log rotation enabled
#' log_appender(appender_file(f, max_lines = 3, max_files = 5L))
#'
#' ## enable internal logging to see what's actually happening in the logrotate steps
#' log_threshold(TRACE, namespace = ".logger")
#' ## log 25 messages
#' for (i in 1:25) log_info(i)
#'
Expand All @@ -79,10 +82,7 @@ attr(appender_stdout, "generator") <- quote(appender_stdout())
#' cat(readLines(t), sep = "\n")
#' })
#'
#' ## enable internal logging to see what's actually happening in the logrotate steps
#' log_threshold(TRACE, namespace = ".logger")
#' ## run the above commands again
#' }
#' \dontshow{logger:::namespaces_set(old)}
appender_file <- function(file, append = TRUE, max_lines = Inf, max_bytes = Inf, max_files = 1L) { # nolint
force(file)
force(append)
Expand Down Expand Up @@ -168,7 +168,7 @@ appender_tee <- function(file, append = TRUE, max_lines = Inf, max_bytes = Inf,
force(max_files)
structure(
function(lines) {
appender_console(lines)
if (needs_stdout()) appender_stdout(lines) else appender_console(lines)
appender_file(file, append, max_lines, max_bytes, max_files)(lines)
},
generator = deparse(match.call())
Expand Down
18 changes: 10 additions & 8 deletions R/formatters.R
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ attr(formatter_glue_safe, "generator") <- quote(formatter_glue_safe())
#' @return character vector
#' @family `log_formatters`
#' @export
#' @examples \dontrun{
#' @examples
#' formatter_glue_or_sprintf("{a} + {b} = %s", a = 2, b = 3, 5)
#' formatter_glue_or_sprintf("{pi} * {2} = %s", pi * 2)
#' formatter_glue_or_sprintf("{pi} * {2} = {pi*2}")
Expand All @@ -120,7 +120,6 @@ attr(formatter_glue_safe, "generator") <- quote(formatter_glue_safe())
#' formatter_glue_or_sprintf("Hi {c('foo', 'bar')}, did you know that 2*4=%s", 2 * 4)
#' formatter_glue_or_sprintf("Hi %s, did you know that 2*4={2*4}", c("foo", "bar"))
#' 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(),
Expand Down Expand Up @@ -182,12 +181,13 @@ attr(formatter_glue_or_sprintf, "generator") <- quote(formatter_glue_or_sprintf(
#' @export
#' @note This functionality depends on the \pkg{jsonlite} package.
#' @family `log_formatters`
#' @examples \dontrun{
#' @examples
#' \dontshow{old <- logger:::namespaces_set()}
#' log_formatter(formatter_json)
#' log_layout(layout_json_parser())
#' log_info(everything = 42)
#' log_info(mtcars = mtcars, species = iris$Species)
#' }
#' \dontshow{logger:::namespaces_set(old)}
formatter_json <- function(...,
.logcall = sys.call(),
.topcall = sys.call(-1),
Expand Down Expand Up @@ -235,15 +235,16 @@ skip_formatter <- function(message, ...) {
#' @return character vector
#' @export
#' @family `log_formatters`
#' @examples \dontrun{
#' @examples
#' \dontshow{old <- logger:::namespaces_set()}
#' log_formatter(formatter_logging)
#' log_info("42")
#' log_info(42)
#' log_info(4 + 2)
#' log_info("foo %s", "bar")
#' log_info("vector %s", 1:3)
#' log_info(12, 1 + 1, 2 * 2)
#' }
#' \dontshow{logger:::namespaces_set(old)}
formatter_logging <- function(...,
.logcall = sys.call(),
.topcall = sys.call(-1),
Expand All @@ -270,15 +271,16 @@ attr(formatter_logging, "generator") <- quote(formatter_logging())
#' @note This functionality depends on the \pkg{pander} package.
#' @export
#' @family `log_formatters`
#' @examples \dontrun{
#' @examples
#' \dontshow{old <- logger:::namespaces_set()}
#' log_formatter(formatter_pander)
#' log_info("42")
#' log_info(42)
#' log_info(4 + 2)
#' log_info(head(iris))
#' log_info(head(iris), style = "simple")
#' log_info(lm(hp ~ wt, mtcars))
#' }
#' \dontshow{logger:::namespaces_set(old)}
formatter_pander <- function(x,
...,
.logcall = sys.call(),
Expand Down
19 changes: 11 additions & 8 deletions R/helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
#' its result on a single line separated by `=>`, while setting to
#' `TRUE` will log the expression and the result in separate
#' sections reserving line-breaks and rendering the printed results
#' @examples \dontrun{
#' @examples
#' \dontshow{old <- logger:::namespaces_set()}
#' log_eval(pi * 2, level = INFO)
#'
#' ## lowering the log level threshold so that we don't have to set a higher level in log_eval
Expand Down Expand Up @@ -36,7 +37,7 @@
#'
#' ## doing something computationally intensive
#' log_eval(system.time(for (i in 1:100) mad(runif(1000))), multiline = TRUE)
#' }
#' \dontshow{logger:::namespaces_set(old)}
#' @importFrom utils capture.output
#' @export
log_eval <- function(expr, level = TRACE, multiline = FALSE) {
Expand Down Expand Up @@ -84,6 +85,7 @@ log_eval <- function(expr, level = TRACE, multiline = FALSE) {
#' @param width max width of message -- longer text will be wrapped into multiple lines
#' @export
#' @examples
#' \dontshow{old <- logger:::namespaces_set()}
#' log_separator()
#' log_separator(ERROR, separator = "!", width = 60)
#' log_separator(ERROR, separator = "!", width = 100)
Expand All @@ -92,6 +94,7 @@ log_eval <- function(expr, level = TRACE, multiline = FALSE) {
#' log_separator(ERROR, separator = "!", width = 100)
#' log_layout(layout_blank)
#' log_separator(ERROR, separator = "!", width = 80)
#' \dontshow{logger:::namespaces_set(old)}
#' @seealso [log_with_separator()]
log_separator <- function(level = INFO,
namespace = NA_character_,
Expand Down Expand Up @@ -120,6 +123,7 @@ log_separator <- function(level = INFO,
#' @inheritParams log_separator
#' @export
#' @examples
#' \dontshow{old <- logger:::namespaces_set()}
#' log_with_separator("An important message")
#' log_with_separator("Some critical KPI down!!!", separator = "$")
#' log_with_separator("This message is worth a {1e3} words")
Expand All @@ -140,6 +144,7 @@ log_separator <- function(level = INFO,
#' logger <- layout_glue_generator(format = "{node}/{pid}/{namespace}/{fn} {time} {level}: {msg}")
#' log_layout(logger)
#' log_with_separator("Boo!", level = FATAL, width = 120)
#' \dontshow{logger:::namespaces_set(old)}
#' @seealso [log_separator()]
log_with_separator <- function(...,
level = INFO,
Expand Down Expand Up @@ -187,15 +192,14 @@ log_with_separator <- function(...,
#' @param level see [log_levels()]
#' @param namespace x
#' @export
#' @examples \dontrun{
#' @examples
#' log_tictoc("warming up")
#' Sys.sleep(0.1)
#' log_tictoc("running")
#' Sys.sleep(0.1)
#' log_tictoc("running")
#' Sys.sleep(runif(1))
#' log_tictoc("and running")
#' }
#' @author Thanks to Neal Fultz for the idea and original implementation!
log_tictoc <- function(..., level = INFO, namespace = NA_character_) {
ns <- fallback_namespace(namespace)
Expand All @@ -221,16 +225,15 @@ log_tictoc <- function(..., level = INFO, namespace = NA_character_) {
.topenv = parent.frame()
)
}
tictocs <- new.env()
tictocs <- new.env(parent = emptyenv())


#' Logs the error message to console before failing
#' @param expression call
#' @export
#' @examples \dontrun{
#' @examples
#' log_failure("foobar")
#' log_failure(foobar)
#' }
#' try(log_failure(foobar))
log_failure <- function(expression) {
tryCatch(expression, error = function(e) {
log_error(e$message)
Expand Down
22 changes: 15 additions & 7 deletions R/layouts.R
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,16 @@ get_logger_meta_variables <- function(log_level = NULL,
#' that is returned when calling [log_layout()] for the currently
#' used layout
#' @export
#' @examples \dontrun{
#' @examples
#' \dontshow{old <- logger:::namespaces_set()}
#' example_layout <- layout_glue_generator(
#' format = "{node}/{pid}/{ns}/{ans}/{topenv}/{fn} {time} {level}: {msg}"
#' )
#' example_layout(INFO, "try {runif(1)}")
#'
#' log_layout(example_layout)
#' log_info("try {runif(1)}")
#' }
#' \dontshow{logger:::namespaces_set(old)}
#' @seealso See example calls from [layout_glue()] and [layout_glue_colors()].
#' @family `log_layouts`
layout_glue_generator <- function(format = '{level} [{format(time, "%Y-%m-%d %H:%M:%S")}] {msg}') {
Expand Down Expand Up @@ -159,14 +160,17 @@ attr(layout_simple, "generator") <- quote(layout_simple())
#' @return character vector
#' @export
#' @family `log_layouts`
#' @examples \dontrun{
#' @examples
#' \dontshow{old <- logger:::namespaces_set()}
#' log_layout(layout_logging)
#' log_info(42)
#' log_info(42, namespace = "everything")
#'
#' \dontrun{
#' devtools::load_all(system.file("demo-packages/logger-tester-package", package = "logger"))
#' logger_tester_function(INFO, 42)
#' }
#' \dontshow{logger:::namespaces_set(old)}
layout_logging <- function(level,
msg,
namespace = NA_character_,
Expand Down Expand Up @@ -241,11 +245,12 @@ attr(layout_glue_colors, "generator") <- quote(layout_glue_colors())
#' @export
#' @note This functionality depends on the \pkg{jsonlite} package.
#' @family `log_layouts`
#' @examples \dontrun{
#' @examples
#' \dontshow{old <- logger:::namespaces_set()}
#' log_layout(layout_json())
#' log_info(42)
#' log_info("ok {1:3} + {1:3} = {2*(1:3)}")
#' }
#' \dontshow{logger:::namespaces_set(old)}
layout_json <- function(fields = default_fields()) {
force(fields)

Expand Down Expand Up @@ -282,14 +287,17 @@ layout_json <- function(fields = default_fields()) {
#' @export
#' @note This functionality depends on the \pkg{jsonlite} package.
#' @family `log_layouts`
#' @examples \dontrun{
#' @examples
#' \dontshow{old <- logger:::namespaces_set()}
#' log_formatter(formatter_json)
#' log_info(everything = 42)
#'
#' log_layout(layout_json_parser())
#' log_info(everything = 42)
#'
#' log_layout(layout_json_parser(fields = c("time", "node")))
#' log_info(cars = row.names(mtcars), species = unique(iris$Species))
#' }
#' \dontshow{logger:::namespaces_set(old)}
layout_json_parser <- function(fields = default_fields()) {
force(fields)

Expand Down
Loading

0 comments on commit e05f1a8

Please sign in to comment.