The spdlog library is
a widely-used and very capable header-only C++ library for logging. This
package includes its headers as an R package to permit other R packages
to deploy it via a simple LinkingTo: RcppSpdlog
as
described in Section
1.1.3 of WRE.
A simple first example, following the upstream examples:
edd@rob:~$ Rscript -e 'Rcpp::sourceCpp("inst/examples/exampleOne.cpp")'
R> exampleOne()
[07:45:57.168673] [I] [thread 1500593] Welcome to spdlog!
[07:45:57.168704] [E] [thread 1500593] Some error message with arg: 1
[07:45:57.168707] [W] [thread 1500593] Easy padding in numbers like 00000012
[07:45:57.168710] [C] [thread 1500593] Support for int: 42; hex: 2a; oct: 52; bin: 101010
[07:45:57.168728] [I] [thread 1500593] Support for floats 1.23
[07:45:57.168731] [I] [thread 1500593] Positional args are supported too..
[07:45:57.168734] [I] [thread 1500593] left aligned
[07:45:57.168737] [D] [thread 1500593] This message should be displayed..
edd@rob:~$
This logs the hour, minute, second, microsecond followed by a one-char code for info, error, warning or critical followed by the thread id and the actual loggable message. The code is included in the package.
Many other customizations are possible, see the spdlog wiki.
Note that using spdlog
examples directly may
well trigger warning from R during package checking as
stdout
and/or stderr
may be used. See the
included example function described in the next section which uses a
derived class to pass logging output explicitly to the R input/output
stream as per the R coding requirements, see Section
1.3.1 (and others) of WRE.
As shipped, both spdlog and the embedded fmt use stdout
and
stderr
in ways that may make it non-trivial to fully
replace them with R input/output as required by Section
1.3.1 (and others) of WRE.
However, based on some initial trials and some excellent help from
upstream we have defined a specific sink for R in the header rcpp_sink.h
,
corrected one stderr
use and added one
#define
. That combination now passes as can be seen in
checks of the package RcppSpdlog
and the included function
exampleRsink()
whose complete source code is included in
the package too.
Note that it is deliberately similar in use to the example above. A
new instance of the logger is instantiated as a shared pointer
sp
to a spdlog
object.
We may make additional package features available in the future.
Gabi Melman is the main author of spdlog.
Victor Zverovich is the main author of the embedded fmt library.
Dirk Eddelbuettel is author of this package and the R integration.
spdlog and fmt are under the MIT license.
RcppSpdlog is released under the GNU GPL, version 2 or later, just like R and Rcpp.