Skip to content

Commit 03019b9

Browse files
hadleycderv
andauthored
Add some logging (#185)
feat: Add configurable debug logging for Quarto vignettes - Add internal quarto_log() function for debug file logging - Enable via R_QUARTO_LOG_DEBUG=TRUE or quarto.log.debug=TRUE option - Configure log file via R_QUARTO_LOG_FILE or quarto.log.file option - Default log file: ./quarto-r-debug.log - Auto-enable for GitHub Actions debug mode and QUARTO_LOG_LEVEL=DEBUG - Add comprehensive tests and documentation * Bump version --------- Co-authored-by: Christophe Dervieux <[email protected]>
1 parent 36d18b0 commit 03019b9

File tree

8 files changed

+447
-5
lines changed

8 files changed

+447
-5
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: quarto
22
Title: R Interface to 'Quarto' Markdown Publishing System
3-
Version: 1.4.4.9013
3+
Version: 1.4.4.9014
44
Authors@R: c(
55
person("JJ", "Allaire", , "[email protected]", role = "aut",
66
comment = c(ORCID = "0000-0003-0174-9868")),

NEWS.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# quarto (development version)
22

3+
- Added debugging logic for quarto vignette engine to help diagnose issues with Quarto vignettes in **pkgdown** and other context (thanks, @hadley, #185).
4+
Set `quarto.log.debug = TRUE` to enable debugging messages (or `R_QUARTO_LOG_DEBUG = TRUE` environment variable).
5+
Set `quarto.log.file` to change the file path to write to (or `R_QUARTO_LOG_FILE` environment variable). Default will be `./quarto-r-debug.log`
6+
Debug mode will be on automatically when debugging Github Actions workflows, or when Quarto CLI's environment variable `QUARTO_LOG_LEVEL` is set to `DEBUG`.
7+
38
- Added a `new_blog_post()` function (thanks, @topeto, #22).
49

510
- Make `quarto_render(as_job = TRUE)` wrapable (thanks, @salim-b, #105).

R/quarto-args.R

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@ cli_arg_profile <- function(profile, ...) {
55

66
is_quiet <- function(quiet) {
77
# in CI, follow debug mode
8-
if (
9-
identical(Sys.getenv("ACTIONS_RUNNER_DEBUG", ""), "true") ||
10-
identical(Sys.getenv("ACTIONS_STEP_DEBUG", ""), "true")
11-
) {
8+
if (in_ci_with_debug()) {
129
return(FALSE)
1310
}
1411
# these option takes precedence

R/r-logging.R

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
in_debug_mode <- function() {
2+
in_ci_with_debug() || quarto_log_level("DEBUG") || is_quarto_r_debug()
3+
}
4+
5+
in_ci_with_debug <- function() {
6+
# check for GitHub Actions debug mode
7+
# https://docs.github.com/en/actions/learn-github-actions/environment-variables#default-environment-variables
8+
gha_debug <- Sys.getenv("ACTIONS_RUNNER_DEBUG", "") == "true" ||
9+
Sys.getenv("ACTIONS_STEP_DEBUG", "") == "true"
10+
return(gha_debug)
11+
}
12+
13+
# Return the current log level for Quarto
14+
# unless level is provided, in which case
15+
# it checks if the current log level matches the provided level.
16+
quarto_log_level <- function(level = NULL) {
17+
log_level <- Sys.getenv("QUARTO_LOG_LEVEL", NA)
18+
if (is.null(level)) {
19+
return(log_level)
20+
}
21+
return(identical(tolower(log_level), tolower(level)))
22+
}
23+
24+
#' @importFrom xfun env_option
25+
is_quarto_r_debug <- function() {
26+
# check option first, then environment variable
27+
# to allow setting in the R session
28+
# env var R_QUARTO_LOG_DEBUG
29+
isTRUE(as.logical(xfun::env_option('quarto.log.debug', FALSE)))
30+
}
31+
32+
# Get the configured log file path
33+
# Uses xfun::env_option to check for log file configuration
34+
# with option 'quarto.log.file' and env var 'R_QUARTO_LOG_FILE'
35+
#' @importFrom xfun env_option
36+
get_log_file <- function() {
37+
xfun::env_option('quarto.log.file', default = "./quarto-r-debug.log")
38+
}
39+
40+
#' Log debug information to a configurable file
41+
#'
42+
#' This function logs messages to a file only when in debug mode to help diagnose
43+
#' issues with Quarto vignettes in **pkgdown** and other contexts.
44+
#'
45+
#' Debug mode will be enabled automatically when debugging Github Actions workflows,
46+
#' or when Quarto CLI's environment variable `QUARTO_LOG_LEVEL` is set to `DEBUG`.
47+
#'
48+
#' @param ... Messages to log (will be concatenated)
49+
#' @param file Path to log file. If NULL, uses `get_log_file()` to determine the file.
50+
#' Default will be `./quarto-r-debug.log` if no configuration is found.
51+
#' @param append Logical. Should the messages be appended to the file? Default TRUE.
52+
#' @param timestamp Logical. Should a timestamp be added? Default TRUE.
53+
#' @param prefix Character. Prefix to add before each log entry. Default "DEBUG: ".
54+
#'
55+
#' @return Invisibly returns TRUE if logging occurred, FALSE otherwise
56+
#' @keywords internal
57+
#'
58+
#' @section Configuration:
59+
#'
60+
#' **Enable debugging messages:**
61+
#' - Set `quarto.log.debug = TRUE` (or `R_QUARTO_LOG_DEBUG = TRUE` environment variable)
62+
#'
63+
#' **Change log file path:**
64+
#' - Set `quarto.log.file` to change the file path (or `R_QUARTO_LOG_FILE` environment variable)
65+
#' - Default will be `./quarto-r-debug.log`
66+
#'
67+
#' **Automatic debug mode:**
68+
#' - Debug mode will be on automatically when debugging Github Actions workflows
69+
#' - When Quarto CLI's environment variable `QUARTO_LOG_LEVEL` is set to `DEBUG`
70+
#'
71+
#' @examples
72+
#' \dontrun{
73+
#' # Set log file via environment variable
74+
#' Sys.setenv(R_QUARTO_LOG_FILE = "~/quarto-debug.log")
75+
#'
76+
#' # Or via option
77+
#' options(quarto.log.file = "~/quarto-debug.log")
78+
#'
79+
#' # Enable debug mode
80+
#' options(quarto.log.debug = TRUE)
81+
#'
82+
#' # Log some information
83+
#' quarto_log("Starting process")
84+
#' quarto_log("R_LIBS:", Sys.getenv("R_LIBS"))
85+
#' quarto_log(".libPaths():", paste0(.libPaths(), collapse = ":"))
86+
#' }
87+
quarto_log <- function(
88+
...,
89+
file = NULL,
90+
append = TRUE,
91+
timestamp = TRUE,
92+
prefix = "DEBUG: "
93+
) {
94+
if (!in_debug_mode()) {
95+
return(invisible(FALSE))
96+
}
97+
98+
if (is.null(file)) {
99+
file <- get_log_file()
100+
}
101+
102+
# get_log_file() now returns the default, so no need for additional fallback
103+
104+
# Construct the message
105+
msg_parts <- list(...)
106+
msg <- paste(msg_parts, collapse = "")
107+
108+
# Add prefix if provided
109+
if (!is.null(prefix) && nchar(prefix) > 0) {
110+
msg <- paste0(prefix, msg)
111+
}
112+
113+
# Add timestamp if requested
114+
if (timestamp) {
115+
ts <- format(Sys.time(), "[%Y-%m-%d %H:%M:%S] ")
116+
msg <- paste0(ts, msg)
117+
}
118+
119+
# Ensure message ends with newline
120+
if (!grepl("\n$", msg)) {
121+
msg <- paste0(msg, "\n")
122+
}
123+
124+
# Write to file
125+
tryCatch(
126+
{
127+
cat(msg, file = file, append = append)
128+
return(invisible(TRUE))
129+
},
130+
error = function(e) {
131+
# If we can't write to the file, fail silently in debug logging
132+
return(invisible(FALSE))
133+
}
134+
)
135+
}

R/utils-vignettes.R

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ vweave_quarto <- function(format) {
3333
}
3434
return(vweave_empty(file))
3535
}
36+
37+
# Log debug information using the new configurable logging function
38+
quarto_log("R_LIBS: ", Sys.getenv("R_LIBS"))
39+
quarto_log(".libPaths(): ", paste0(.libPaths(), collapse = ":"))
40+
quarto_log("Packages: ", paste0(dir(.libPaths()[1]), collapse = ","))
41+
3642
quarto_render(file, ..., output_format = format, metadata = meta)
3743
}
3844
}

man/quarto_log.Rd

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

tests/testthat/helper.R

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,3 +285,22 @@ install_dev_package <- function(.local_envir = parent.frame()) {
285285
)
286286
}
287287
}
288+
289+
local_clean_state <- function(env = parent.frame()) {
290+
withr::local_envvar(
291+
# gha debug env variables
292+
ACTIONS_RUNNER_DEBUG = NA,
293+
ACTIONS_STEP_DEBUG = NA,
294+
# quarto R env variables
295+
R_QUARTO_LOG_DEBUG = NA,
296+
R_QUARTO_LOG_FILE = NA,
297+
# quarto CLI env variables
298+
QUARTO_LOG_LEVEL = NA,
299+
.local_envir = env
300+
)
301+
withr::local_options(
302+
quarto.log.debug = NULL,
303+
quarto.log.file = NULL,
304+
.local_envir = env
305+
)
306+
}

0 commit comments

Comments
 (0)