1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
|
# Logging system
#
# Author: Renaud Gaujoux
# Creation: 25 Apr 2012
###############################################################################
#' @include utils.R options.R
NULL
#' Logging Feature
#'
#' \code{lverbose} returns/sets the current verbosity level.
#'
#' @param val logical/numeric value that sets the verbosity level.
#' @param global logical that indicates if the verbose level of
#' all log handlers should be set to \var{val}.
#'
#' @return the old verbose level
#' @export
#' @rdname logging
lverbose <- local({
.val <- as.integer(FALSE)
function(val, global = FALSE){
if( missing(val) ) return( max(getOption('pkg_verbose_level', 0L), .val) )
oval <- .val
.val <<- max(as.integer(val), 0)
# set value globally
if( global ){
options(pkg_verbose_level = val)
}
invisible(oval)
}
})
#' @describeIn logging tells if all verbose messages are silenced.
#' @export
lsilent <- function(){
l <- lverbose()
is.na(l) || l == 0L
}
#' @describeIn logging tells if verbosity is on, i.e. at level greater than 0.
#' @export
is.verbose <- function(){
l <- lverbose()
!is.na(l) && l >= 0L
}
#' @describeIn logging prints out a message (on sdtout) if the verbosity level is greater than a
#' given value.
#'
#' @param ... parts of a character message that are concatenated and passed to
#' the current logger's write function.
#' @param level verbosity level threshold (numeric value) above which the
#' message should be printed out.
#' This threshold is compared with the current verbosity level as returned by
#' \code{lverbose}.
#' @param appendLF logical indicating if an endline character should be appended
#' at the end of the message.
#' @param sep separation character, used when concatenating all arguments in \code{...}.
#' @param force logical that indicates if one should output messages or return a non
#' null logger, even if the verbose mode is not high enough.
#'
#' @export
lmessage <- function(level, ..., appendLF=TRUE, sep='', force=FALSE){
getLogger(force=force)$lmessage(level, ..., appendLF=appendLF, sep=sep, force=force)
}
#' @describeIn logging prints out a log message (at level 1) using the current logger,
#' typically on stdout.
#' It is a shortcut for \code{lmessage(1L, ...)}.
#'
#' @export
vmessage <- function(...){
lmessage(1L, ...)
}
#' @describeIn logging directly appends some message to the current log line.
#' @export
log_append <- function(...){
if( !is.null(w <- getLogger(new=FALSE)$write) ) w(...)
}
# return a list of data about a given frame (i.e. caller function)
callInfo <- function(n=-1L){
if( n<0L ) n <- n-1
f <- sys.function(n)
e <- sys.frame(n)
sysn <- if( n<0L ) sys.nframe()-n else n
list(fd=digest(f), env=capture.output(e), nframe=sysn)
}
log_lastLF <- sVariable(list(Inf, TRUE))
.LOG_OPTIONS <- setupPackageOptions(NAME='logger', RESET=TRUE,
# should a the next logger be indented
autoindent = TRUE
)
logger.options <- .LOG_OPTIONS$options
logger.getOption <- .LOG_OPTIONS$getOption
getLogger <- local({
# store info for top call
.topCall <- NULL
# list of logger objects
.lastLogger <- list()
function(..., type='STDOUT', new=TRUE, force=FALSE){
# return NULL logger if not in verbose mode
if( !force && !lverbose() ) return( new_logger('NULL') )
# top caller
call1 <- sys.call(1)
topCall <- callInfo(1L)
caller <- callInfo(-1L)
# reset all static variables
if( !identical(.topCall, topCall) ){
.topCall <<- topCall
.lastLogger <<- list()
}
## build logger object
if( !length(.lastLogger) ){ # new stored for future calls
logger <- new_logger(type, ...)
logger$caller(caller)
.lastLogger[[as.character(caller$nframe)]] <<- logger
}else{ # increment indentation
autonew <- missing(new)
if( new ){ # get new logger object
last <- getLogger(new=FALSE)
if( !autonew || (!is.null(last$caller()) && last$caller()$nframe < caller$nframe) ){
# instanciate a new logger object only if the current caller is lower in the stack
logger <- new_logger(type, ...)
logger$caller(caller)
# auto indent if last logger is higher in the stack
if( !logger.getOption('autoindent') ){
# reset autoindent option on.exit
on.exit( logger.options(autoindent=TRUE), add=TRUE)
logger$nindent(last$nindent(), add=FALSE)
} else if( last$caller()$nframe < caller$nframe ){
logger$nindent(last$nindent() + 1L, add=FALSE)
}
.lastLogger[[as.character(caller$nframe)]] <<- logger
}else logger <- last
# add initial new line if the last log message was from a higher logger
logger$breaknext(log_lastLF()[[1L]] < caller$nframe)
}else{
# get logger for exact frame number
if( is.null(logger <- .lastLogger[[as.character(caller$nframe)]]) ){
# get first higher logger
i <- which(as.numeric(names(.lastLogger)) <= caller$nframe)
# str(.lastLogger)
if( length(i) ) logger <- .lastLogger[[tail(i, 1L)]]
else logger <- .lastLogger[[1L]]
}
}
}
# return logger
logger
}
})
new_logger <- function(type, ..., nindent=0L, indent=' '){
.data <- list(
lastLF = TRUE
, breaknext = FALSE
, cindent = indent
, nindent = nindent
, indent = rep(indent, nindent)
, caller = NULL
)
# init logger object
.logger <- list()
.logger$data <- function(value){
if( missing(value) ) .data
else .data <<- value
}
# init type-specific slots
f <- match.fun(paste('new_logger', type, sep=''))
.logger <- f(.logger, ...)
# get last used appendLF value
.logger$lastLF <- function(){ .data$lastLF }
# get initial new line flag
.logger$breaknext <- function(val){
if( missing(val) ) .data$breaknext
else .data$breaknext <<- val
}
# get caller data
.logger$caller <- function(val){
if( missing(val) ) .data$caller
else .data$caller <<- val
}
# get/set indentation
.logger$indent <- function(val){
if( missing(val) ) .data$indent
else{
if( is.numeric(val) ) return( .logger$nindent(val) )
old <- .data$indent
.data$indent <<- val
old
}
}
# get/set number of indentation
.logger$nindent <- function(val, add=TRUE){
if( missing(val) ) .data$nindent
else{
old <- .data$nindent
.data$nindent <<- val + if( add ) .data$nindent else 0L
if( .data$nindent >= 0L )
.data$indent <<- paste(rep(.data$cindent, .data$nindent), collapse='')
old
}
}
# new message with auto-indentation and breakline
.logger$message <- function(..., appendLF=TRUE, sep=''){
if( is.null(.logger$write) ) return()
msg <- if( .data$breaknext || isTRUE(logger.getOption('breaknext')) ){
logger.options(breaknext=NULL)
.data$breaknext <<- FALSE
"\n"
}
# add indentation if on a new line
lastLFframe <- log_lastLF()
callerFrame <- .logger$caller()$nframe
if( .data$lastLF || lastLFframe[[2L]] || (is.finite(lastLFframe[[1L]]) && lastLFframe[[1L]] >= callerFrame) )
msg <- paste(msg, .data$indent, sep='')
msg <- paste(msg, paste(..., sep='', collapse=sep), if( appendLF ) "\n", sep='')
# store in global variable the frame number if it the line is left open
log_lastLF(list(if( !appendLF ) callerFrame else Inf, appendLF))
.data$lastLF <<- appendLF
# call logger write function
.logger$write(msg)
}
# log message with specific level
.logger$lmessage <- function(level, ..., force=FALSE){
if( force || lverbose() >= level ) .logger$message(...)
}
# show info about the logger
.logger$show <- function(){
cat("<Type: ", class(.logger)[1L], ">\n", sep='')
.logger$info()
}
# wrap into a logger object
structure(.logger, class=c(paste('logger', type, sep=''), 'logger'))
}
# NULL logger
new_loggerNULL <- function(.logger, ...){
# write to log
.logger$write <- NULL
# special info
.logger$info <- function(){
cat("Output: NULL\n")
}
# return logger object
.logger
}
# Logger that writes on the standard output
new_loggerSTDOUT <- function(.logger, ...){
# append to log
.logger$write <- function(...){
cat(..., sep='')
}
.logger$info <- function(){
cat("Output: stdout\n")
}
# return logger object
.logger
}
# Logger that writes on the standard error
new_loggerSTDERR <- function(.logger, ...){
# append to log
.logger$write <- function(...){
message(..., appendLF=FALSE)
}
.logger$info <- function(){
cat("Output: stderr\n")
}
# return logger object
.logger
}
#' General Log Formating
#'
#' Generate a formatted diagnostic message.
#' This function is a shortcut for \code{message(sprintf(...))}.
#'
#' @inheritParams base::sprintf
#' @inheritParams base::message
#'
#' @return Returns nothing. Used for their side effects of printing messages/warnings.
#' @seealso \code{\link{sprintf}}, \code{\link{message}}
#' @export
#' @examples
#'
#' messagef("Hello %s number %i", 'world', 4)
#'
messagef <- function(fmt, ..., domain = NULL, appendLF = TRUE) message(sprintf(fmt, ...), domain = domain, appendLF = appendLF)
#' @describeIn messagef throws a simple note as an immediate warning.
#' It is a shorcut for `warning(..., immediate. = TRUE, call. = FALSE)`.
#'
#' @inheritParams base::warning
#' @export
wnote <- function(..., immediate. = TRUE){
warning(..., immediate. = immediate., call. = FALSE)
}
#' Simple Text Iteration Counter
#'
#' @param n number of total steps
#' @param i0 starting step
#' @param title character string to use as title
#' @param extra character vector providing extra text to add at each step
#' @param verbose logical that toggles the counter
#'
#' @return A function that can be used to increment progress.
#' @export
#' @examples
#'
#' progress <- iterCount(LETTERS)
#' res <- sapply(LETTERS, function(x){
#' Sys.sleep(.1)
#' progress()
#' })
#' # terminate counter
#' i_end <- progress(NULL)
#' i_end
#'
iterCount <- function(n = 100, i0 = 0L, title = 'Iterations', extra = NULL, verbose = TRUE){
if( !verbose ) return( function(...) NULL )
if( length(n) > 1L || is.character(n) ){
extra <- extra %||% n
n <- length(n)
}
# setup
size_i <- nchar(as.character(n))
if( nzchar(title) ) title <- paste0(title, ': ')
.msg_fmt <- sprintf("%s%%%ii/%i", title, size_i, n)
inc_msg <- function(i, addon = extra[i]){
if( !length(addon) || is.na(addon) ) sprintf(.msg_fmt, i, '')
else paste0(sprintf(.msg_fmt, i, addon))
}
.cat_msg <- function(msg){
cat(msg)
flush.console()
nchar(msg)
}
if( !is.null(extra) ){
.msg_fmt <- paste0(.msg_fmt, " (%s)")
.msg_size <- inc_msg(n, extra[which.max(nchar(extra))])
}else .msg_size <- inc_msg(n)
.msg_size <- nchar(.msg_size)
.back <- paste0(rep("\b", .msg_size), collapse ='')
.i <- i0
.last_msg_size <- .cat_msg(inc_msg(i0))
# increment function
function(i, appendLF = i == n){
if( missing(i) ) i <- .i
else if( is.null(i) ){
if( .i < n ) cat("\n")
i_stop <- .i
.i <<- i0
return(invisible(i_stop))
}
.i <<- i+1
msg <- paste0(substr(.back, 1L, .last_msg_size), msg0 <- inc_msg(.i))
.cat_msg(msg)
.last_msg_size <<- nchar(msg0)
if( appendLF ) cat("\n")
invisible(.i)
}
}
|