R/log.R

Defines functions set_log_error set_log_success set_log_info new_log logPredicate

Documented in new_log set_log_error set_log_info set_log_success

#' Log Predicate
#' 
#' Log predicate to bridge the previous log with the new
#' [log::Logger] package.
#' 
#' @noRd 
#' @keywords internal
logPredicate <- function(log){
  \() log
}

#' Logger
#' 
#' Returns a new logger using the `log` package.
#' 
#' @param prefix String to prefix all log messages.
#' @param write Whether to write the log to the `file`.
#' @param file Name of the file to dump the logs to, 
#' only used if `write` is `TRUE`.
#' @param sep Separator between `prefix` and other 
#' flags and messages.
#' 
#' @examples 
#' log <- new_log()
#' log$log("Hello world")
#' 
#' @return An R& of class `log::Logger`.
#' 
#' @export 
new_log <- function(
  prefix = ">", 
  write = FALSE, 
  file = "ambiorix.log", 
  sep = ""
){
  log::Logger$new(
    prefix = prefix,
    write = write,
    file = file,
    sep = sep
  )$
  date()$
  time()
}

#' Customise logs
#' 
#' Customise the internal logs used by Ambiorix.
#' 
#' @param log An object of class `Logger`, see
#' [log::Logger].
#' 
#' @name set_log
#' 
#' @export
set_log_info <- function(log) {
  assert_that(not_missing(log))
  assert_that(is_logger(log))

  .globals$infoLog <- log
}

#' @rdname set_log
#' @export
set_log_success <- function(log) {
  assert_that(not_missing(log))
  assert_that(is_logger(log))

  .globals$successLog <- log
}

#' @rdname set_log
#' @export
set_log_error <- function(log) {
  assert_that(not_missing(log))
  assert_that(is_logger(log))

  .globals$errorLog <- log
}

#' CLI symbols
#' 
#' CLI Symbols for log
#' 
#' @keywords internal
success <- \() {
  cli::col_green(cli::symbol$tick)
}

#' @keywords internal
error <- \() {
  cli::col_red(cli::symbol$cross)
}

#' @keywords internal
info <- \() {
  cli::col_blue(cli::symbol$info)
}

#' @keywords internal
warn <- \() {
  cli::col_yellow(cli::symbol$warning)
}
devOpifex/ambiorix documentation built on Oct. 15, 2024, 10:21 p.m.