Skip to content

Commit baf6c03

Browse files
authored
Merge pull request #225 from thomasp85/issue210-cli-formatter
Add formatter_cli
2 parents d7b4735 + af8e2cb commit baf6c03

14 files changed

+110
-0
lines changed

DESCRIPTION

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Imports:
2222
utils
2323
Suggests:
2424
botor,
25+
cli,
2526
covr,
2627
crayon,
2728
devtools,

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export(colorize_by_log_level)
3232
export(delete_logger_index)
3333
export(deparse_to_one_line)
3434
export(fail_on_missing_package)
35+
export(formatter_cli)
3536
export(formatter_glue)
3637
export(formatter_glue_or_sprintf)
3738
export(formatter_glue_safe)

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ improved documentations, modernized tests, performance speedups.
1313
* `log_appender()`, `log_layout()` and `log_formatter()` now check that you are calling them with a function, and return the previously set value (#170, @hadley)
1414
* new function to return number of log indices (#194, @WurmPeter)
1515
* `appender_async` is now using `mirai` instead of a custom background process and queue system (#214, @hadley @shikokuchuo)
16+
* New `formatter_cli()` allows you to use the syntax from the cli package to create log messages (#210, @thomasp85)
1617

1718
## Fixes
1819

R/formatters.R

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,36 @@ formatter_glue_or_sprintf <- function(msg,
173173
}
174174
attr(formatter_glue_or_sprintf, "generator") <- quote(formatter_glue_or_sprintf())
175175

176+
#' Apply [cli::cli_text()] to format string with cli syntax
177+
#' @param ... passed to [cli::cli_text()] for the text interpolation
178+
#' @inheritParams log_level
179+
#' @return character vector
180+
#' @export
181+
#' @family log_formatters
182+
#' @importFrom utils str
183+
formatter_cli <- function(...,
184+
.logcall = sys.call(),
185+
.topcall = sys.call(-1),
186+
.topenv = parent.frame()) {
187+
fail_on_missing_package("cli")
188+
189+
withCallingHandlers(
190+
cli::cli_fmt(cli::cli_text(..., .envir = .topenv)),
191+
error = function(e) {
192+
args <- paste0(capture.output(str(...)), collapse = "\n")
193+
194+
stop(paste0(
195+
"`cli` failed in `formatter_cli` on:\n\n",
196+
args,
197+
"\n\nRaw error message:\n\n",
198+
conditionMessage(e),
199+
"\n\nPlease consider using another `log_formatter` or ",
200+
"`skip_formatter` on strings with curly braces."
201+
))
202+
}
203+
)
204+
}
205+
attr(formatter_cli, "generator") <- quote(formatter_cli())
176206

177207
#' Transforms all passed R objects into a JSON list
178208
#' @param ... passed to `toJSON` wrapped into a `list`

man/formatter_cli.Rd

Lines changed: 46 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/formatter_glue.Rd

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/formatter_glue_or_sprintf.Rd

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/formatter_glue_safe.Rd

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/formatter_json.Rd

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/formatter_logging.Rd

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)