R/count_errors_logs.R

Defines functions count_errors_log

Documented in count_errors_log

#' Count errors in a log file
#'
#' Reports occcurrences of common errors in log files generated by
#' \code{run_optim()}.
#'
#' @param path_to_log path to a log file to search the errors in.
#' The log file can be a \code{.txt} or \code{.log} file.
#'
#' @return a list with the indexes of trees that returned each error.
#' Also prints the nb of occurrences of each error to the console
#'
#' @author Théo Pannetier
#' @export
#'
count_errors_log <- function(path_to_log){
  if (!is.character(path_to_log) |
      !tools::file_ext(path_to_log) %in% c("txt", "log")) {
    stop("Wrong input file format. Use '.txt' or '.log' files only.")
  }
  no_conv_error <-
    "Optimization has not converged. Try again with different initial values."
  subplex_error <- "Error : in 'subplex': missing value where TRUE/FALSE needed"
  error_templates <- c(no_conv_error, subplex_error)

  # Load text file as a list of text lines
  log_file <- readLines(path_to_log)

  # Initiate counters
  counts <- rep(0, length(error_templates)) # nb occurrences of each error
  mc <- 0 # tree index
  mcs_error_list <- list(
    "no_conv" = c(),
    "subplex" = c()
  ) # store the indexes of trees that return each error


  for(i in seq_along(log_file)){

    # Keep track of the tree index
    if(log_file[[i]] == paste("Running ML for tree", mc + 1, "")){
      mc <- mc + 1
    }
    # Count error line if a match is found to specified error messages
    for(j in seq_along(error_templates)){
      if(log_file[[i]] == error_templates[j]){
        counts[j] <- counts[j] + 1
        mcs_error_list[[j]] <- c(mcs_error_list[[j]], mc)
      }
    }
  }
  # Print the nb
  cat(path_to_log, "\t", counts, "\n")
  return(mcs_error_list)
}
TheoPannetier/DDvTDtools documentation built on Oct. 22, 2020, 2:31 p.m.