Problem
I'm working on a custom package and have run into an interesting edge case in which I need certain code to execute only when it's actually being run by users, rather than when it's triggered during development (e.g. by devtools::check, testthat functions, R CMD CHECK, etc).
Context
I developed custom logging within my package - certain functions, when executed by users, will create a log entry in an external database. I've got this working (with a mix of SQL Server db, and stored procedures). However, the problem is that too much logging occurs during package development. All the various testing/checking/build calls done by an R package developer "pollutes" the database.
So what I need is to be able to modify my logging calls to skip if the call stack is related to devtools/testthat/etc scenarios. How do I do this?
My attempt at troubleshooting
To troubleshoot so far, I created a sample package with these 2 files and explore the console output
zzz.R
:
.onLoad <- function(libname, pkgname) {
myTestFunction()
}
myTestFunction.R
:
myTestFunction= function() {
print(sys.calls())
}
I'll then explore the call stack to see if I can identify reliable keywords that I can detect during my logging, and skip as needed. Here's an example call stack triggered during devtools::check()