Event Reporting (ER)#

The ER framework provides a common logging front-end that can be backed by any of several plugins. Application code uses a single set of macros (ER_DEBUG(), ER_WARN(), etc.) regardless of which plugin is active; the plugin is selected at build time via RCSW_CONFIG_ER_PLUGIN.

See Event Reporting for modules, module IDs, levels, and compile-time versus run-time filtering.

Quickstart#

Step 1 — Near the top of your .c file, define the module ID and name, then include the ER client header:

#define RCSW_ER_MODID   0x0001000000000000ULL
#define RCSW_ER_MODNAME "myapp.subsystem.component"
#include "rcsw/er/client.h"

Step 2 — In the module’s initialization function, install the module:

void mycomponent_init(void) {
    RCSW_ER_MODULE_INIT();
}

Step 3 — Use the logging macros anywhere in the file:

ER_DEBUG("Initialized with n_elts=%zu", n_elts);
ER_WARN("Buffer nearly full: %zu/%zu", used, capacity);
ER_FATAL("Unrecoverable error: %d", err);

That’s it. RCSW_ER_MODULE_INIT() is the recommended path — use RCSW_ER_INSMOD(id, name) only if you need to install multiple modules from a single initializer that doesn’t correspond to a specific file.

Tip

Not all plugins use both RCSW_ER_MODID and RCSW_ER_MODNAME (see plugin details below). Define both regardless to keep your code portable across plugins.

Use RCSW_ER_MODNAME_BUILDER to construct the name string so it works correctly with every plugin (e.g., it substitutes _ for . when building for zlog).

Plugins#

The simple bare-bones logger.

This plugin uses RCSW’s built-in stdio_printf() and minimal stdlib implementation, to provide the necessary logging functionality in environments where stdlib is not available, and/or using stdlib hogs too much space.

In this plugin, each source file within RCSW and of each project which links with RCSW can define a logging “module”; modules are file-based, and therefore you can’t have multiple modules/loggers in a single file. If you architect your projects well, this should not be a burdensome restriction. You can split a logging module across several files if you want by defining RCSW_ER_MODNAME equivalently.

The modules in this plugin:

  • Are unconditionally enabled and cannot be disabled; that is, each time a logging statement is encountered during execution, it is always emitted.

  • Do not have a “level” associated with them which determines if a given logging statement should be emitted. Put another way, there is only a single global module whose level is set at compile time; statements less than the compile-time level are compiled out.

  • The name of each module is prepended to each logging statement to help identify the logging source.

Each emitted logging statement is of the form:

<RCSW_ER_MODNAME> [LVL  ] <message>

LVL is one of [FATAL, ERROR, WARN, INFO, DEBUG, TRACE], padded to five characters; FATAL and ERROR are colored red on an ANSI terminal. <message> is the rendered message. RCSW_ER_MODNAME defines the logical name of the module.

This plugin is useful in:

  • Bare metal environments such as bootstraps without an OS.

  • Bare metal hardware validation tests.

Plugin configuration

Configuration Item

Notes

RCSW_ER_PLUGIN_PRINTF

Defined as stdio_printf().

RCSW_ER_PLUGIN_INIT()

Idempotent/not used by this plugin.

RCSW_ER_PLUGIN_DEINIT()

Idempotent/not used by this plugin.

RCSW_ER_PLUGIN_REPORT()

None.

RCSW_ER_PLUGIN_INSMOD

Idempotent/not used by this plugin.

RCSW_ER_PLUGIN_LVL_CHECK

Compares against the compile-time level only. Statements below it are already compiled out, so in practice every remaining statement is emitted.

RCSW_ER_MODNAME

The name of the module. Can have any format; it can be convenient to use a hierarchical format such as foo.bar.baz for interoperability with other plugins. See also RCSW_ER_MODNAME_BUILDER.

RCSW_ER_MODID

Not used by this plugin.

A lighter/simpler version of log4c.

In this plugin, each source file within RCSW and of each project which links with RCSW can define a logging “module”; modules are file-based, and therefore you can’t have multiple modules/loggers in a single file. If you architect your projects well, this should not be a burdensome restriction. You can split a logging module across several files by defining the same RCSW_ER_MODID in each; LOG4CL identifies modules by ID, not name.

Each module can be enabled/disabled independently in a lightweight manner; unlike log4j loggers, the enable/disable status of one logger/module does not have any effect on the status of another. That is, the hierarchy is “flat”.

This plugin does not provide most features found in log4c, with the exception of levels: FATAL, ERROR, WARN, INFO, DEBUG, TRACE. If you want something with features comparable to log4c, use the zlog plugin.

Each emitted logging statement is of the form:

<RCSW_ER_MODNAME> [LVL] <message>

LVL is one of [FATAL, ERROR, WARN, INFO, DEBUG, TRACE], and <message> is the rendered message. RCSW_ER_MODNAME defines the logical name of the module.

This plugin is useful in:

  • Medium-complexity embedded systems with limited resources.

  • Systems where you want to have multiple modules, only some of which should be enabled, but don’t need the hierarchical logging of log4c. In this scheme, modules are either enabled or not whether a given module is enabled has no effect on other modules. In addition, the name given to a specific module is purely for debugging purposes, and has no effect on event reporting; you can have multiple modules with the same name and different IDs, if you want.

Plugin configuration

Configuration Item

Notes

RCSW_ER_PLUGIN_PRINTF

Defined as printf()

RCSW_ER_PLUGIN_INIT()

Idempotent. Takes no arguments. After initialization, no modules are installed.

RCSW_ER_PLUGIN_DEINIT()

Idempotent. Takes no arguments.

RCSW_ER_PLUGIN_REPORT()

None.

RCSW_ER_PLUGIN_INSMOD

Idempotent: installing an installed ID changes nothing. A new module starts at the default level, INFO unless changed with log4cl_default_lvl_set(); statements from modules that were never installed are filtered at that level too.

RCSW_ER_PLUGIN_LVL_CHECK

Safe from any thread on POSIX, as is installing modules. Removing modules (log4cl_rmmod()), changing levels (log4cl_mod_lvl_set(), log4cl_default_lvl_set()) and RCSW_ER_DEINIT() must happen while no other thread is reporting.

RCSW_ER_MODNAME

The name of the module. Can have any format; it can be convenient to use a hierarchical format such as foo.bar.baz for interoperability with other plugins. See also RCSW_ER_MODNAME_BUILDER.

RCSW_ER_MODID

Identifies the module, so give each module its own ID (see Module ID allocation). Files that don’t define one all get 0xFFFFFFFF and therefore share a single module.

The zlog plugin.

In this plugin, each source file can define one logging module; modules are file-scoped, so you cannot have multiple modules in a single file. You can share a module name across files by defining RCSW_ER_MODNAME identically in each.

Configuration is controlled via the zlog .conf file or the zlog API; RCSW does not wrap that functionality beyond what is necessary to use the logging macros.

Notes:

  • Because zlog is a third-party library, you must install its headers and libraries to CMAKE_INSTALL_PREFIX (or make them accessible via your package manager) before building RCSW with this plugin.

  • zlog does not define a TRACE level by default. RCSW adds one as described in the zlog developer documentation. Add the following to your .conf file to enable TRACE output:

    [levels]
    TRACE = 10
    

    The numeric value must match the value defined in the RCSW zlog ER plugin; mismatches will cause TRACE events to be silently dropped or misrouted.

  • Module names must not contain dots (.). Use underscores (_) instead, as zlog expects. RCSW_ER_MODNAME_BUILDER joins name components with _ under this plugin (and . under LOG4CL), so names built with it work under either; it doesn’t rewrite dots inside a component.

This plugin is useful for:

  • Linux targets and other targets with a filesystem.

  • Systems requiring hierarchical logging or flexible output routing (files, syslog, etc.).

Plugin configuration

Configuration Item

Notes

RCSW_ER_PLUGIN_PRINTF

Defined as printf().

RCSW_ER_PLUGIN_INIT()

Defined as zlog_init(). Takes the path to the .conf file as its argument. Idempotency is not guaranteed by zlog; avoid calling more than once.

RCSW_ER_PLUGIN_DEINIT()

Defined as zlog_fini(). Takes no arguments. Idempotency is not guaranteed by zlog.

RCSW_ER_PLUGIN_REPORT()

Handled internally by zlog.

RCSW_ER_PLUGIN_INSMOD

Not used by this plugin: the zlog category for a module is created on first use (zlog_get_category()), and its rules come from the .conf file.

RCSW_ER_PLUGIN_LVL_CHECK

Handled internally by zlog; thread safety is zlog’s responsibility.

RCSW_ER_MODNAME

Must not contain dots; use _ instead. See RCSW_ER_MODNAME_BUILDER.

RCSW_ER_MODID

Not used by this plugin.

A custom plugin which is defined exactly how you want in terms of modules, levels, etc. To integrate your plugin with RCSW, you must create a .h file with a few #define macros. If your plugin uses RCSW_ER_MODID, it should support 64-bit IDs if you want to use RCSW with it.

Important

Your .h file is NOT installed/packaged with RCSW, so you will need to ensure that it is findable by any applications you want to use with RCSW+your custom ER plugin.

Your file must define every macro below; RCSW stops the build with an #error if one is missing. A macro your plugin has no use for can be defined as nothing (or, for RCSW_ER_PLUGIN_LVL_CHECK(), as a true value). You can of course have whatever else you want in the file.

The main ER plugin hook. Will be called as part of every ER_WARN(), etc. statement. Arguments:

  • LVL - The level of the statement, as a bare token (WARN, INFO, …). See Levels for details.

  • HANDLE - Whatever was returned from RCSW_ER_PLUGIN_HANDLE().

  • ID - The ID of the current module (file): RCSW_ER_MODID, or 0xFFFFFFFF if the file doesn’t define one.

  • NAME - The name of the current module (file)

  • MSG - The message string, with "\r\n" already appended

  • ... - Any additional arguments for the message string

Note

This macro is used as a statement inside RCSW’s ER machinery, so it must expand to a complete statement: a braced block, or a call ending in ;.

The name of a printf()-like function with the same signature; used to define the PRINTF() / DPRINTF() macros. If you define it as nothing, you can’t use those macros.

A framework initialization hook, called by RCSW_ER_INIT(); should be idempotent. Can take any number of arguments of any type. If it is not needed by your plugin, #define as nothing.

A framework shutdown hook, called by RCSW_ER_DEINIT(); should be idempotent. Can take any number of arguments of any type. If it is not needed by your plugin #define as nothing.

Arguments:

  • ID - The numeric ID for the module.

  • NAME - The string name for the module.

Install/enable a module with the specified ID and name. If not needed by your plugin, #define as nothing.

Arguments:

  • ID - The numeric ID for the module.

  • NAME - The string name for the module.

Get a logger “handle” of some kind which contains the necessary information to determine if a given module is enabled. For example, in the LOG4CL plugin, the log4cl_mod_query() function serves this purpose.

RCSW doesn’t inspect the handle; it only passes it to RCSW_ER_PLUGIN_LVL_CHECK(). If the module with the specified ID, NAME is not enabled, return something (such as NULL) that your level check treats as “don’t emit”.

If not needed by your plugin, #define as nothing.

Arguments:

  • HANDLE - The module handle returned by RCSW_ER_PLUGIN_HANDLE().

  • LVL - The level associated with the current reporting statement, as a bare token.

Given an active module HANDLE, determine if the statement with the specified LVL should be emitted or not.

If not needed by your plugin, #define as a truth-y value, such as 1.

The string RCSW_ER_MODNAME_BUILDER puts between name components, such as ".".

Plugin Comparison#

Feature

Simple

LOG4CL

zlog

Notes

Stdlib required

No

Yes

Yes

simple uses stdio_printf(); LOG4CL uses libc printf().

Per-module enable/disable

No

Yes

Yes

simple has one global compile-time level only.

Runtime level adjustment

No

Yes†

Yes

simple is compile-time only.

Hierarchical logging

No

No

Yes

zlog supports hierarchical category matching.

File/sink routing

No

No

Yes

zlog routes to files, syslog, etc. via .conf.

Thread safety

N/A

Yes†

N/A (zlog-internal)

† On POSIX, reporting and installing modules are safe from any thread. Removing modules, changing levels and RCSW_ER_DEINIT() must happen while no other thread is reporting, e.g. at startup or shutdown.

Works with RCSW_CONFIG_NOALLOC

Yes

No

Yes

LOG4CL keeps its modules in a heap-allocated list. zlog allocates through its own library, not RCSW.

Best for

Bare-metal, bootstraps, no OS

Embedded systems, moderate complexity

Linux, complex routing needs