Elektra 0.8.26
How-To: Logging

Step by Step Guide

Preparation

  1. Import the logging library in your code:

    #include <kdblogger.h>
    Logger Interface.
  2. Insert logging statements:

    ELEKTRA_LOG("Hello, %s!", "🌎");

Log Everything

If Elektra should display all log messages, then please follow the steps below.

  1. Uncomment the line:

    // #define NO_FILTER

    in the file src/libs/elektra/log.c.

  2. Optional: Change the debug level in src/include/kdblogger.h. For example, if you want to see debug messages too, then change the line

    @ ELEKTRA_LOG_LEVEL_INFO
    The standard log level.
    Definition: kdblogger.h:53
    @ ELEKTRA_LOG_LEVEL
    Alias for the standard log level.
    Definition: kdblogger.h:69

    to

    @ ELEKTRA_LOG_LEVEL_DEBUG
    The debug log level.
    Definition: kdblogger.h:64

File Specific Logging

If you want to only log messages below a specific directory prefix, then please follow the steps below.

  1. Search for the code:

    #ifndef NO_FILTER
    // XXX Filter level …
    #endif

    in the file src/libs/elektra/log.c.

  2. Replace the code with something like:

    #ifndef NO_FILTER
    if (strncmp (file, "src/postfix/", sizeof ("src/postfix"))) goto end;
    #endif

    , where src/postfix contains all source files with logging statements that Elektra should log. For example, if you want to log everything from the yamlcpp plugin, then use the following code.

    #ifndef NO_FILTER
    if (strncmp (file, "src/plugins/yamlcpp/", sizeof ("src/plugins/yamlcpp"))) goto end;
    #endif

    . To log messages from multiple source you can use the operator && to chain multiple calls to strncmp. For example, to log messages from the directoryvalue and yamlcpp plugin use the code:

    #ifndef NO_FILTER
    if (strncmp (file, "src/plugins/directoryvalue/", sizeof ("src/plugins/directoryvalue")) &&
    strncmp (file, "src/plugins/yamlcpp/", sizeof ("src/plugins/yamlcpp")))
    goto end;
    #endif

Compilation

  1. Enable the logger: e.g. run cmake with the switch -DENABLE_LOGGER=ON
  2. Build Elektra

Log Levels

There are four log levels (ERROR is reserved for aborts within ELEKTRA_ASSERT):