R/log.R

Defines functions log_msg

Documented in log_msg

#' Function to log a msg to a file
#' 
#' @param msg Message to log.
#' @param dir A directory to write the report to.
#' @param file file to log to.
#' 
#' @author Andreas Scharmueller, \email{andschar@@proton.me}
#' 
#' @export
#' 
#' @examples
#' \donttest{
#'   # connection to database required
#'   log_msg('Test')
#' }
#' 
log_msg = function(msg = 'script run.',
                   dir = NULL,
                   file = 'script.log') {
  # checks
  if (is.null(dir)) {
    dir = getwd()
  }
  script = try(basename(sys.frame(1)$ofile), silent = TRUE)
  if (inherits(script, 'try-error')) {
    out = paste(paste(Sys.time(), 'Current script run: ', sep = ' '),
                msg,
                sep = '\t')
  } else {
    out = paste(paste(Sys.time(), script, 'run: ', sep = ' '),
                msg,
                sep = '\t')
  }
  write(out, file.path(dir, file), append = TRUE)
  message(out)
}
andschar/andmisc documentation built on Jan. 19, 2025, 10:46 a.m.