Skip to content

Commit

Permalink
mnt: Add set_echo and get_echo
Browse files Browse the repository at this point in the history
  • Loading branch information
MEO265 committed May 8, 2024
1 parent f018bac commit ef14c4a
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 3 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
This was previously (unintentionally) guaranteed by replacing the `NA` with `"__NA__"`.
* A global log level can now be set using `set_log_level()`, which is used by all functions unless otherwise stated.
The log levels are: `"DEBUG"`, `"INFO"`, `"WARN"`, `"ERROR"` and `"NONE"`.
* Add `set_echo()` to control globally whether log messages are echoed to the console.

## Bugfixes
* `read_logs()` now correctly reads empty character values `""`, as in `{"key": ""}`, as such.
Expand Down
22 changes: 22 additions & 0 deletions R/configurations.R
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,25 @@ get_log_level <- function() {
.config$level
}

#' Set echo
#'
#' @param echo Logical. Should log messages be echoed to the console?
#'
#' @return Invisible `NULL`.
#'
#' @export
#'
set_echo <- function(echo = TRUE, confirm = TRUE) {
.config$echo <- echo
if (confirm) base::message("Echo set to ", echo)
invisible(NULL)

Check warning on line 137 in R/configurations.R

View check run for this annotation

Codecov / codecov/patch

R/configurations.R#L135-L137

Added lines #L135 - L137 were not covered by tests
}

#' Get echo
#'
#' @return Logical. Are log messages echoed to the console?
#'
#' @export
get_echo <- function() {
.config$echo

Check warning on line 146 in R/configurations.R

View check run for this annotation

Codecov / codecov/patch

R/configurations.R#L146

Added line #L146 was not covered by tests
}

Check warning on line 147 in R/configurations.R

View workflow job for this annotation

GitHub Actions / lint

file=R/configurations.R,line=147,col=2,[trailing_blank_lines_linter] Missing terminal newline.
2 changes: 1 addition & 1 deletion R/json.R
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ default_ndjson_unsanitizer <- function(string) {
#' so will append new log entries to the log file.
#'
#' @keywords internal
write_ndjson <- function(log_df, logfile = get_logfile(), echo = TRUE, overwrite = FALSE) {
write_ndjson <- function(log_df, logfile = get_logfile(), echo = get_echo(), overwrite = FALSE) {

# logdata will be built into a character vector where each element is a valid
# JSON object, constructed from each row of the log data frame.
Expand Down
2 changes: 1 addition & 1 deletion R/loggit.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ NULL
#' sure = "why not?", like = 2, or = 10, what = "ever")
#'
#' @export
loggit <- function(log_lvl, log_msg, ..., echo = TRUE, custom_log_lvl = FALSE, logfile = get_logfile(),
loggit <- function(log_lvl, log_msg, ..., echo = get_echo(), custom_log_lvl = FALSE, logfile = get_logfile(),
ignore_log_level = FALSE) {
# Try to suggest limited log levels to prevent typos by users
log_lvls <- c("DEBUG", "INFO", "WARN", "ERROR")
Expand Down
2 changes: 1 addition & 1 deletion R/whit_loggit.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#' @return The result of the expression.
#'
#' @export
with_loggit <- function(exp, logfile = get_logfile(), echo = TRUE, log_level = get_log_level()) {
with_loggit <- function(exp, logfile = get_logfile(), echo = get_echo(), log_level = get_log_level()) {
log_level <- convert_lvl_input(log_level)
withCallingHandlers(
exp,
Expand Down
1 change: 1 addition & 0 deletions R/zzz.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.onLoad <- function(libname, pkgname) {
set_echo(confirm = FALSE)
set_log_level(confirm = FALSE, level = 3L)
set_logfile(confirm = FALSE, create = FALSE)
set_timestamp_format(confirm = FALSE)
Expand Down

0 comments on commit ef14c4a

Please sign in to comment.