tl: A tiny logger using spdlite via rspdlite
About
The rspdlite packages provides the tiny, fast, capable logger spdlite, implemented as a small C++20 header-only library, for use by R. It is related to its larger and more featureful cousing spdlog which we provide via RcppSpdlog -- and the wrapping frontend spdl which polishes and unifies some user-interface aspects for the discerning R user and/or package developer.
tl does the same for rspdlite: It offers an entirely optional interface
consistently using namespaced calling convention for both R and C++. In other words, one can added
debugging informations such as tl::info("Entering section foo") the same way in both R and C++
(with an added semicolon, or course, and by adding a header for declarations).
Examples
We can revisit the examples from the rspdlite package:
From C++
We can use the same C++ example from the spdlite docs, but now accessing via the tl
namespace:
tl::critical("-- level to warn"); tl::set_level("warn"); tl::error("Some error message with arg: {}", 1); tl::warn("Easy padding in numbers like {:08d}", 12); tl::critical("Support for int: {0:d}; hex: {0:x}; oct: {0:o}; bin: {0:b}", 42); tl::info("Support for floats {:03.2f}", 1.23456); tl::info("Positional args are {1} {0}..", "too", "supported"); tl::info("{:<30}", "left aligned");
The default logging level is 'info' so all these message would appear by default but as we alter the logging level dynamically to 'warn' fewer messages appear.
From R
Following the nice user experience offered by spdl, we similarly provide access via the 'package name colon colon' patter from R:
tl::critical("-- level to debug") tl::set_level("debug") tl::info("Some more at info") tl::error("Some error message with arg: {}", 1) tl::error("Some error message with more args: {} and {}", 1, "abc") tl::log_critical("-- level to error and calling example1 and example2") tl::set_level("error")
As for its cousin R packages, the format string here 'resembles' the full C++ format string, but remains simpler. As we convert each argument directly to a character (then passed on to the C++ layer) we do not offer the extra formatting options available directly from C++. We have not found this to be an issue.
Package tlexample
The package tlexample offers a complete example of integrated logging from both C++ and
R. After installing rspdlite and tl (along with their one dependency Rcpp)
one can run the provided demo. So for example in an r2u container (such as rocker/r2u
where we rely on the GitHub installation helper also pulling in dependencies Rcpp,
rspdlite and this package tl as binaries)
root$ installGithub.r eddelbuettel/tlexample
root$ Rscript -e 'demo(ex, package="tlexample")'or alternatively install Rcpp and these package from source or as binary from my r-universe. This tlexample package is very lightweight and therefore a good playground to experiment with logging via tl, rspdlite and spdlite.
Package RcppNLoptExample
The package RcppNLoptExample package (also on [CRAN][cran]) provides a complete example from the documentation of the underlying NLopt library, bundled in a small yet complete package illustrating use of NLopt from R. This offers a vessel for a good illustration of adding tl from both the C++ and R, along with different logging levels.
Author
Dirk Eddelbuettel
License
tl is released under the GNU GPL, version 2 or later, just like R itself.