r-simmer / simmer

Discrete-Event Simulation for R
https://r-simmer.org
GNU General Public License v2.0
220 stars 42 forks source link

Caller misidentification #286

Closed Enchufa2 closed 2 years ago

Enchufa2 commented 2 years ago

Caller identification for error printing is made here:

https://github.com/r-simmer/simmer/blob/ca6632e5e28223872dcc86dda9f0c21b65fbe59b/R/utils.R#L42-L44

So, for instance, we have this for R 3.6.x as well as 4.0.x:

trajectory() %>% log_(1)
#> Error: log_: 'message' is not a valid character or function

But then, for R 4.1.x:

trajectory() %>% log_(1)
#> Error: check_args: 'message' is not a valid character or function

and R 4.2.x:

trajectory() %>% log_(1)
#> Error: stop: 'message' is not a valid character or function

which means that we cannot rely on a fixed stack position. It's better to perform a (limited) search, e.g.,

get_caller <- function(max.depth=10) {
  for (i in seq_len(max.depth)) {
    fun <- as.character(sys.call(-i-1)[[1]])
    if (grepl("\\.(simmer|trajectory)$", fun))
      return(strsplit(fun, ".", fixed=TRUE)[[1]][1])
  }
  return("")
}