Skip to content

Commit

Permalink
fixed getting loglevel/file from env vars
Browse files Browse the repository at this point in the history
  • Loading branch information
flying-sheep committed Apr 19, 2016
1 parent e1ece75 commit b11f259
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 18 deletions.
18 changes: 9 additions & 9 deletions R/logging.r
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ log_msg_stderror <- function(lvl, msg){
})

log_msg_logfile <- function(lvl, msg) {
if (!is.null(getOption('jupyter.logfile'))) {
cur_logfile <- getOption('jupyter.logfile')
if (.is_changed_logfile(cur_logfile)) {
log_msg_stderror('INFO', sprintf('Logging to %s', cur_logfile))
}
log_con <- file(cur_logfile, open='ab')
writeBin(charToRaw(sprintf('%s %s: %s\n', format(Sys.time()), lvl, msg)), log_con, endian = 'little')
close(log_con)
}
cur_logfile <- getOption('jupyter.logfile')
if (!is.na(cur_logfile)) {
if (.is_changed_logfile(cur_logfile)) {
log_msg_stderror('INFO', sprintf('Logging to %s', cur_logfile))
}
log_con <- file(cur_logfile, open = 'ab')
writeBin(charToRaw(sprintf('%s %s: %s\n', format(Sys.time()), lvl, msg)), log_con, endian = 'little')
close(log_con)
}
}
29 changes: 21 additions & 8 deletions R/options.r
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
getenv_default <- function(varname, default) {
value <- Sys.getenv(varname)
if (identical(value, '')) default else value
}

#' @usage \code{
#' options(jupyter.* = ...)
#' getOption('jupyter.*')
Expand All @@ -11,8 +6,8 @@ getenv_default <- function(varname, default) {
#' @name IRkernel
#' @export
jupyter_option_defaults <- list(
jupyter.log_level = as.integer(getenv_default('JUPYTER_LOG_LEVEL', 1L)),
jupyter.logfile = getenv_default('JUPYTER_LOGFILE', NULL),
jupyter.log_level = 1L,
jupyter.logfile = NA,
jupyter.pager_classes = c(
'help_files_with_topic'),
jupyter.plot_mimetypes = c(
Expand All @@ -21,10 +16,28 @@ jupyter_option_defaults <- list(
'image/svg+xml'),
jupyter.in_kernel = FALSE)

from_env <- list(
JUPYTER_LOG_LEVEL = as.integer,
JUPYTER_LOGFILE = function(f) if (nchar(f) == 0) NA else f)

# converts e.g. jupyter.log_level to JUPYTER_LOG_LEVEL
opt_to_env <- function(nms) gsub('.', '_', toupper(nms), fixed = TRUE)

.onLoad <- function(libname = NULL, pkgname = NULL) {
for (opt_name in names(jupyter_option_defaults)) {
# skip option if it is set to NULL (unset)
if (is.null(getOption(opt_name))) {
do.call(options, jupyter_option_defaults[opt_name]) # single []: preserve name
# prepare `options` call from the default
call_arg <- jupyter_option_defaults[opt_name] # single [] preserve names

# if an env var is set, get value from it.
env_name <- opt_to_env(opt_name)
convert <- from_env[[env_name]]
env_val <- Sys.getenv(env_name, unset = NA)
if (!is.null(convert) && !is.na(env_val))
call_arg[[opt_name]] <- convert(env_val)

do.call(options, call_arg)
}
}
}
2 changes: 1 addition & 1 deletion man/IRkernel.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b11f259

Please sign in to comment.