Movatterモバイル変換


[0]ホーム

URL:


Dirk Eddelbuettel

spdl: Aconsistent C++ and R interface to spdlog

CICRANDependenciesDownloadsLicenseLast Commit

About

TheRcppSpdlogrepository (andCRAN package)provides access to the wonderfulspdlog library along withthefmt library.

This permits use in C++ extensions for R by offering thespdlog (header-only) libraryalong with the (header-only)fmt library (by usingLinkingTo as described inSection1.1.3 of WRE). More recently,RcppSpdlog wasextended so that it provides a set of key functions directly for use byother R functions (as described inSection5.4.3 of WRE).

However, now the use of, say, a debug logging message from C++ lookslike

// in C++spdlog::debug("Text with{} text{} which is{}","auto","expansion",42);

whereas in R, given theRcppSpdlog package andnamespace, it looks like this (if we usesprintf() toassemble the message)

# in RRcppSpdlog::log_debug(sprintf("Text with %s text %s which is %s","auto","expansion", 42L);

and that irked us. Enter this package! By owning thespld namespace (in R) and an easily overlayed namespace inC++ of the same name we can do

// in C++spdl::debug("Text with{} text{} which is{}","auto","expansion",42);

as well as (still usingsprintf())

# in Rspdl::debug(sprintf("Text with %s text %s which is %s","auto","expansion", 42L));

which ismuch better as it avoids context switching. Betterstill, with the internal formatter we can write thesame formatstring as in C++ and not worry about format details:

# in Rspdl::debug("Text with {} text {} which is {}","auto","expansion", 42L);

Details

We use a simple mechanism in which all R arguments are passed throughformat() by default to render strings, and then pass asingle vector of strings argumentthrough the restrictive C languageForeign Function Interface toRcppSpdlog whereit can be passed to the C++ layer available there.

This also mean we use thefmt library in both languagesas the formatter. We prefer this is over other string-interpolatinglibraries in R which are similar but subtly different. Should their usebe desired, they can of course be used: the default call to any of theloggers is just a single-argument call with a text variable so users arefree to expand strings as they please. Anything starting frompaste andsprintf works.

Author

Dirk Eddelbuettel

License

spdl is released under the GNU GPL, version 2 or later, just like Ritself.

Initially created: Tue Nov 22 05:15:42 PM CST 2022
Last modified: Sun May 26 10:25:41 CDT 2024


[8]
ページ先頭

©2009-2025 Movatter.jp