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 |
|---|---|
|
Defined as |
|
Idempotent/not used by this plugin. |
|
Idempotent/not used by this plugin. |
|
None. |
|
Idempotent/not used by this plugin. |
|
Compares against the compile-time level only. Statements below it are already compiled out, so in practice every remaining statement is emitted. |
The name of the module. Can have any format; it can be convenient to use
a hierarchical format such as |
|
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 |
|---|---|
|
Defined as |
|
Idempotent. Takes no arguments. After initialization, no modules are installed. |
|
Idempotent. Takes no arguments. |
|
None. |
|
Idempotent: installing an installed ID changes nothing. A new module
starts at the default level, INFO unless changed with
|
|
Safe from any thread on POSIX, as is installing modules. Removing
modules ( |
The name of the module. Can have any format; it can be convenient to use
a hierarchical format such as |
|
Identifies the module, so give each module its own ID (see
Module ID allocation). Files that don’t define one all
get |
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
.conffile 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_BUILDERjoins 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 |
|---|---|
|
Defined as |
|
Defined as |
|
Defined as |
|
Handled internally by zlog. |
|
Not used by this plugin: the zlog category for a module is created on
first use ( |
|
Handled internally by zlog; thread safety is zlog’s responsibility. |
Must not contain dots; use |
|
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 fromRCSW_ER_PLUGIN_HANDLE().ID- The ID of the current module (file):RCSW_ER_MODID, or0xFFFFFFFFif 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 byRCSW_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 |
|
Per-module enable/disable |
No |
Yes |
Yes |
|
Runtime level adjustment |
No |
Yes† |
Yes |
|
Hierarchical logging |
No |
No |
Yes |
zlog supports hierarchical category matching. |
File/sink routing |
No |
No |
Yes |
zlog routes to files, syslog, etc. via |
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
|
Works with |
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 |