jmbarbone / mark

R package with a set of general use functions for easier working
https://jmbarbone.github.io/mark/
Other
6 stars 1 forks source link

add logging function #164

Closed jmbarbone closed 1 year ago

jmbarbone commented 1 year ago

Not sure if this is the right home for it. Maybe {scribe}?

Maybe echo()?

log_expressions <- function(exprs, to = stdout(), msg = TRUE) {
  env <- environment()
  exprs <- as.list(substitute(exprs))[-1]
  time <- function() paste0("[", format(Sys.time(), tz = "UTC"), "] ")

  cat0 <- function(..., sep = "") {
    cat(..., sep = sep, file = to)
  }

  catln <- function(...) {
    cat0(..., "\n")
  }

  # TODO add functions for other controls
  msg <- if (isTRUE(msg)) {
    function(x) cat0(paste0(time(), "[MSG] #> ", conditionMessage(x))),
  } else {
    function(x) invisible()
  }

  op <- options(width = max(getOption("width") - 37, 30))
  on.exit(options(op))

  for (exp in exprs) {
    cat0(time(), "[EXP] ")
    dep <- deparse1(exp)
    catln(dep)

    res <- tryCatch(
      eval(as.expression(exp), envir = env),
      error = function(e) {
        catln(paste0(time(), "[ERR] #> ", conditionMessage(e)))
        stop("Error in ", dep, "\n  ", conditionMessage(e), call. = FALSE)
      },
      warning = function(e) {
        catln(paste0(time(), "[WRN] #> ", conditionMessage(e)))
        tryInvokeRestart("muffleWarning")
      },
      message = function(e) {
        msg(e)
        tryInvokeRestart("muffleMessage")
      }
    )

    if (is.null(res)) {
      utils::flush.console()
    } else {
      catln(paste0(time(), "[OUT] #> ", utils::capture.output(res), collapse = "\n"))
    }
  }

  invisible(res)
}

try(log_expressions({
  1 + 1
  Sys.sleep(2)
  head(mtcars)
  message(1)
  warning(2)
  stop(3)
}))
#> [2022-12-06 17:18:01] [EXP] 1 + 1
#> [2022-12-06 17:18:01] [OUT] #> [1] 2
#> [2022-12-06 17:18:01] [EXP] Sys.sleep(2)
#> [2022-12-06 17:18:03] [EXP] head(mtcars)
#> [2022-12-06 17:18:03] [OUT] #>                    mpg cyl disp  hp drat
#> [2022-12-06 17:18:03] [OUT] #> Mazda RX4         21.0   6  160 110 3.90
#> [2022-12-06 17:18:03] [OUT] #> Mazda RX4 Wag     21.0   6  160 110 3.90
#> [2022-12-06 17:18:03] [OUT] #> Datsun 710        22.8   4  108  93 3.85
#> [2022-12-06 17:18:03] [OUT] #> Hornet 4 Drive    21.4   6  258 110 3.08
#> [2022-12-06 17:18:03] [OUT] #> Hornet Sportabout 18.7   8  360 175 3.15
#> [2022-12-06 17:18:03] [OUT] #> Valiant           18.1   6  225 105 2.76
#> [2022-12-06 17:18:03] [OUT] #>                      wt  qsec vs am gear
#> [2022-12-06 17:18:03] [OUT] #> Mazda RX4         2.620 16.46  0  1    4
#> [2022-12-06 17:18:03] [OUT] #> Mazda RX4 Wag     2.875 17.02  0  1    4
#> [2022-12-06 17:18:03] [OUT] #> Datsun 710        2.320 18.61  1  1    4
#> [2022-12-06 17:18:03] [OUT] #> Hornet 4 Drive    3.215 19.44  1  0    3
#> [2022-12-06 17:18:03] [OUT] #> Hornet Sportabout 3.440 17.02  0  0    3
#> [2022-12-06 17:18:03] [OUT] #> Valiant           3.460 20.22  1  0    3
#> [2022-12-06 17:18:03] [OUT] #>                   carb
#> [2022-12-06 17:18:03] [OUT] #> Mazda RX4            4
#> [2022-12-06 17:18:03] [OUT] #> Mazda RX4 Wag        4
#> [2022-12-06 17:18:03] [OUT] #> Datsun 710           1
#> [2022-12-06 17:18:03] [OUT] #> Hornet 4 Drive       1
#> [2022-12-06 17:18:03] [OUT] #> Hornet Sportabout    2
#> [2022-12-06 17:18:03] [OUT] #> Valiant              1
#> [2022-12-06 17:18:03] [EXP] message(1)
#> [2022-12-06 17:18:03] [MSG] #> 1
#> [2022-12-06 17:18:03] [EXP] warning(2)
#> [2022-12-06 17:18:03] [WRN] #> 2
#> [2022-12-06 17:18:03] [EXP] stop(3)
#> [2022-12-06 17:18:03] [ERR] #> 3
#> Error : Error in stop(3)
#>   3

Created on 2022-12-06 with reprex v2.0.2

Should also have controls for

jmbarbone commented 1 year ago

option to not repeat timestamp in consecutive lines?

echo(repeat = FALSE, {
  head(mtcars)
})
#> [2022-12-06 17:18:03] [EXP] head(mtcars)
#> [2022-12-06 17:18:03] [OUT] #>                    mpg cyl disp  hp drat
#>                             #> Mazda RX4         21.0   6  160 110 3.90
#>                             #> Mazda RX4 Wag     21.0   6  160 110 3.90
#>                             #> Datsun 710        22.8   4  108  93 3.85
#>                             #> Hornet 4 Drive    21.4   6  258 110 3.08
#>                             #> Hornet Sportabout 18.7   8  360 175 3.15
#>                             #> Valiant           18.1   6  225 105 2.76
#>                             #>                      wt  qsec vs am gear
#>                             #> Mazda RX4         2.620 16.46  0  1    4
#>                             #> Mazda RX4 Wag     2.875 17.02  0  1    4
#>                             #> Datsun 710        2.320 18.61  1  1    4
#>                             #> Hornet 4 Drive    3.215 19.44  1  0    3
#>                             #> Hornet Sportabout 3.440 17.02  0  0    3
#>                             #> Valiant           3.460 20.22  1  0    3
#>                             #>                   carb
#>                             #> Mazda RX4            4
#>                             #> Mazda RX4 Wag        4
#>                             #> Datsun 710           1
#>                             #> Hornet 4 Drive       1
#>                             #> Hornet Sportabout    2
#>                             #> Valiant              1
jmbarbone commented 1 year ago

Closed with #168