UP | HOME

ZIO Logging

Table of Contents

ZIO uses spdlog for logging.

1 Usage

In library code:

#include "zio/logging.hpp"

void my_func() {
    if (bad) zio::error("Oh no!");
}

In "main" code (code providing main() or app embedding ZIO)

#include "zio/main.hpp"

void main() {
    // more fine grained init functions available
    zio::init_all();
}

2 Run time log levels

Given the above initialization the user may control what log level is selected at run-time.

$ export SPDLOG_LEVEL=debug

3 Build time log levels

The logging system also has build-time level filtering. These only effect logging code placed inside the CPP macros ZIO_DEBUG and ZIO_TRACE (these are merely wrappers of spdlog equivalents). Any bare calls to, eg, zio::debug() are not affected by build-time filtering. Even if run-time filtering turns them off they will still consume CPU.

An example to exercise the options is provided and included here:

#include "zio/logging.hpp"
#include "zio/main.hpp"

using namespace zio;

int main()
{
    zio::init_all();

    zio::trace("trace");        // avoid bare trace
    zio::debug("debug");        // avoid bare debug
    zio::info("info");
    zio::warn("warn");
    zio::error("error");
    zio::critical("critical");

    ZIO_TRACE("TRACE");         // prefer over bare call
    ZIO_DEBUG("DEBUG");         // prefer over bare call

    return 0;
}

With it, you can test the effects of build-time and run-time filters. First, an example using the default "info" build-time level.

$ ./waf configure build --target=test_logging

$ unset SPDLOG_LEVEL

$ ./build/test_logging 
[2020-04-14 11:58:10.691] [console] [info] info
[2020-04-14 11:58:10.691] [console] [warning] warn
[2020-04-14 11:58:10.691] [console] [error] error
[2020-04-14 11:58:10.691] [console] [critical] critical

$ SPDLOG_LEVEL=trace ./build/test_logging 
[2020-04-14 11:58:32.664] [console] [trace] trace
[2020-04-14 11:58:32.664] [console] [debug] debug
[2020-04-14 11:58:32.664] [console] [info] info
[2020-04-14 11:58:32.664] [console] [warning] warn
[2020-04-14 11:58:32.664] [console] [error] error
[2020-04-14 11:58:32.664] [console] [critical] critical

The "trace" and "debug" lines seen in the second run were those using functions, not macros. After reconfiguring and rebuilding the source we can see the omitted lines made by the macros.

$ ./waf configure build --target=test_logging --active-log-level=trace

$ ./build/test_logging 
[2020-04-14 12:00:01.143] [console] [info] info
[2020-04-14 12:00:01.143] [console] [warning] warn
[2020-04-14 12:00:01.143] [console] [error] error
[2020-04-14 12:00:01.143] [console] [critical] critical

$ SPDLOG_LEVEL=trace ./build/test_logging 
[2020-04-14 12:00:11.005] [console] [trace] trace
[2020-04-14 12:00:11.005] [console] [debug] debug
[2020-04-14 12:00:11.005] [console] [info] info
[2020-04-14 12:00:11.005] [console] [warning] warn
[2020-04-14 12:00:11.005] [console] [error] error
[2020-04-14 12:00:11.005] [console] [critical] critical
[2020-04-14 12:00:11.005] [console] [trace] [test_logging.cpp:17] TRACE
[2020-04-14 12:00:11.005] [console] [debug] [test_logging.cpp:18] DEBUG

4 Possible future changes

Currently, ZIO logs into the default front end of spdlog and only the console backend is activated.

  • [ ] use more loggers to allow different verbosity for different parts of ZIO library
  • [ ] add hooks to set up file and syslog logging (or leave to user main())
  • [ ] consider a ZeroMQ backend for spdlog