MyNixOS website logo
Description

The 'plog' C++ Logging Library.

A simple header-only logging library for C++. Add 'LinkingTo: plogr' to 'DESCRIPTION', and '#include <plogr.h>' in your C++ modules to use it.

plogr Travis-CI Build Status AppVeyor Build Status

Provides the header files for a stripped-down version of the plog header-only C++ logging library, and a method to log to R's standard error stream.

Installation

You can install plogr from GitHub with:

# install.packages("devtools")
devtools::install_github("krlmlr/plogr")

Usage

Add LinkingTo: plogr to your DESCRIPTION, and add #include <plogr.h> to all modules where you want to access the logging. If your package has a universal header file which you include from all modules, it's probably a good idea to insert the #include directive there, so that all of your code has access to logging. The following system header files will be included:

  • sstream
  • iostream
  • vector
  • cassert
  • cstring
  • time.h (on Linux/OS X)
  • sys/time.h (on Windows)
  • R.h

Example

The code shows a small usage example and a demo which we'll call from R below. (Rcpp is not necessary to use plogr, it is only needed to run the C++ code chunk.) The init_r() function is the only new function added by the R package, and initializes a logger that logs to R's standard error stream. For further details consult the plog documentation; for compatibility reasons you won't find the file appenders in this package.

// If we omit this, logging is disabled altogether.
// In a package, you would probably add -DPLOGR_ENABLE to PKG_CPPFLAGS.
#define PLOGR_ENABLE

// [[Rcpp::depends(plogr)]]
#include <plogr.h>

// [[Rcpp::export]]
void plogr_demo() {
  LOG_INFO << "test 1";
  plog::init_r(plog::info);
  LOG_INFO << "test 2";
  LOG_DEBUG << "test 3";
  plog::init_r("DEBUG"); // You can also pass a string
  LOG_INFO << "test 4";
  LOG_DEBUG << "test 5";
}

#include <Rcpp.h> // not necessary to use plogr

The R code below calls the plogr_demo() C++ function defined above. Currently, the messages are printed straight to the standard error stream, so the message capturing mechanisms employed by knitr don't work. We use a sink with a text connection to capture the messages, and print the contents of the variable to which the text connection assigns.

output <- character()
con <- textConnection("output", "a")
withr::with_message_sink(con, plogr_demo())
close(con)
cat(output, sep = "\n")
#> void plogr_demo@12: test 2
#> void plogr_demo@15: test 4
#> void plogr_demo@16: test 5

Nothing is printed before we actually initialize the logger. Because it is initialized to the info level, the debug log message is not shown, and only "test 2" comes through. After changing the log level, the debug message is also shown.

Metadata

Version

0.2.0

License

Unknown

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows