IAP GITLAB

Skip to content

Complete rework of logging using spdlog

Remy Prechelt requested to merge rprechelt-logging-update into master

The Framework/Logging structure was not used anywhere in the current master branch and wasn't quite feature complete. This MR replaces this infrastructure with spdlog - in my opinion, one of the best logging libraries written available for any programming language. It's insanely fast, incredibly powerful and flexible, and extremely easy to use (it uses the fmt library for string formatting which is fantastic!). I strongly encourage you to look at the README for plenty of examples, and the Wiki for more detail information. spdlog is added as a Git submodule (this also helps improve compile-times compared to just using it as a header-only library)

This MR implements a default logger, "corsika", that is used for default calls to the log functions:

using namespace corsika::logging;

info("This is an info message!");
warn("This is a warning message!");
debug("This is a debug message!");
error("This is an error message!");
critical("This is a critical error message!");

which results in:

[corsika:info    ] This is an info message!
[corsika:warning ] This is a warning message!
[corsika:error   ] This is an error message!
[corsika:critical] This is a critical error message!

(NB: the info, warning, error, and critical are colorized on color-capable terminal emulators).

This MR also allows for creating named loggers using the GetLogger(std::string const&) function.

auto logger = GetLogger("sibyll");
info("This is an info message!");

which results in:

[sibyll:info    ] This is an info message!

The spdlog format string has dozens of options (I chose the format above for simplicity but I am completely open to suggestions - one change to include in the future is to add the thread that produced the logging message). See the formatting documentation here.

While spdlog is insanely fast, especially when the runtime log level has disabled printing to the sink, it also possible to use macro-style logging that can be optimized away at compile-time by setting SPDLOG_ACTIVE_LEVEL

// these print with the "corsika" logger
C8LOG_INFO("This is a macro info msg!");
C8LOG_DEBUG("This is a macro debug msg!");
C8LOG_ERROR("This is a macro error msg!");
C8LOG_CRITICAL("This is a macro critical msg!");

// get a reference to an unknown logger
auto logger = logging::GetLogger("loggerD");

// these print with the "loggerD" logger
C8LOG_LOGGER_INFO(logger, "This is a macro info msg with value {}!", 42);
C8LOG_LOGGER_WARN(logger, "This is a macro warn msg!");
Edited by Remy Prechelt

Merge request reports