R/log.R

Defines functions read_log logger

Documented in logger

# READ/WRITE LOG

#' Log
#'
#' Logs the output of any function.
#' @param f A [`function`].
#' @param file A [`character`] string naming the file to print the output
#'  of `f`.
#' @return
#'  `logger()` is a decorator function called for its side-effect. Returns
#'  the output of `f`.
#' @author N. Frerebeau
#' @keywords internal
logger <- function(f, file) {
  wrapper <- function(...) {
    # Before execution

    # Do nothing
    res <- f(...)

    # After execution
    cat(res, file = file, sep = "", append = TRUE)

    return(res)
  }
  return(wrapper)
}

read_log <- function(file, silent = TRUE, ...) {
  if (file.exists(file)) {
    tweet_log <- try(
      expr = utils::read.table(file, header = FALSE, sep = " ", ...),
      silent = silent
    )
    if (!inherits(tweet_log, "try-error")) {
      return(tweet_log)
    }
  }
  return(NULL)
}
nfrerebeau/twitterbot documentation built on Jan. 30, 2023, 1:12 p.m.