A new package made it to CRAN today: spdl
. It provides a very simple interface to spdlog with the key focus being that provides the same interface from both R and C++. I had brought spdlog to R a little while ago via RcppSpdlog, but its use was generally limited to C++ code where we would write something like (taken from one of the examples included in the package)
::info("Welcome to spdlog!"); spdlog
I then provided C-level exports from the package accessible in other package but with that the signature only became longer as it also included the package name, here RcppSpdlog. So I experimented a little with that and added another ‘more compactly named’ namespace spdl
around it. So then it becomes
::info("Info message with values {} and {}", 42, 1.23); // C++ code spdl
which now also shows the power of the included fmt library. And I got quite used to that … and now wanted to same from R! But to create a new namespace, we need a new package. So … we made one. Now from R too:
::info("Info message with values {} and {}", 42L, 1.23) # R code spdl
We layered a very simple wrapper for fmt
over this. All R argument are passed to format()
and we simply send a vector of strings over the C interface to the RcppSpdlog package where the templated formatter of fmt is invoked – but for character values. You can equally well format the string locally first. Everything works: from paste()
to sprintf()
to any of the existing string interpolators all of which are nice. But none works just like fmt and I wanted to have the same formatter in both languages, and like how fmt works.
By tradition, the NEWS entry for this release follows.
Changes in spld version 0.0.1 (2022-11-21)
- Initial release of R and C++ loggers using
spdl::*
namespace
More detailed information is on the spdl page.
If you like this or other open-source work I do, you can sponsor me at GitHub.
This post by Dirk Eddelbuettel originated on his Thinking inside the box blog. Please report excessive re-aggregation in third-party for-profit settings.