R/create_log.R

Defines functions create_log

Documented in create_log

# Define the function to create the .log file
#' create_log_file
#' @param outfile   String specifying the name of the to-be-created file, *without* extension
#' @param ...       Not used
#' @keywords internal
#'
#' @returns Nothing is returned, intead a text file with the suffix .log is created.
create_log <- function(outfile, ...) {
  if (missing(outfile)) {
    stop("You must specify a name for the output file(s) via the outfile argument.")
  } else {
    logfile <- paste0(outfile, ".log")
  }

  # open the log file for writing
  log_con <- file(logfile)


  # write header to the log file
  cat("### plmmr log file ###\n", file = logfile)
  cat("Logging to", logfile, "\n", file = logfile, append = TRUE)
  cat("Host:", get_hostname(), "\n", file = logfile, append = TRUE)
  cat("Current working directory:", getwd(), "\n", file = logfile, append = TRUE)
  cat("Start log at:", format(Sys.time(), "%Y-%m-%d %H:%M:%S\n"),
      file = logfile, append = TRUE)

  # get the name of the calling function
  calling_function <- deparse(sys.call(-1))
  cat("Call:", trimws(calling_function), "\n", file = logfile, append = TRUE)

  # close the log file
  close(log_con)

  # return logfile name
  return(logfile)
}

Try the plmmr package in your browser

Any scripts or data that you put into this service are public.

plmmr documentation built on April 4, 2025, 12:19 a.m.