Event Reporting (er)#
- group er
Event reporting (logging) with pluggable back ends.
For an overview, see Event Reporting (ER).
rcsw/er/client.h#
Include this header to use event reporting in a source file.
rcsw/er/er.h#
Event reporting levels, plugin IDs, and module name construction.
Event reporting levels
Values for RCSW_ERL and for run-time module levels (e.g. log4cl_mod_lvl_set()). See Levels .
-
RCSW_ERL_NONE#
No event reporting.
-
RCSW_ERL_FATAL#
FATAL only.
-
RCSW_ERL_ERROR#
FATAL and ERROR.
-
RCSW_ERL_WARN#
FATAL through WARN.
-
RCSW_ERL_INFO#
FATAL through INFO.
-
RCSW_ERL_DEBUG#
FATAL through DEBUG.
-
RCSW_ERL_TRACE#
Everything.
-
RCSW_ERL_ALL#
Same as RCSW_ERL_TRACE.
Terminal color codes
ANSI escape sequences for colored terminal output.
-
RCSW_ER_HEADC#
Cyan.
-
RCSW_ER_OKC#
Green.
-
RCSW_ER_WARNC#
Yellow.
-
RCSW_ER_FAILC#
Red.
-
RCSW_ER_ENDC#
Reset to the default text color.
ER plugin IDs
Values for RCSW_CONFIG_ER_PLUGIN. Any other value selects a custom plugin: define every RCSW_ER_PLUGIN_* macro that rcsw/er/plugin/plugin.h checks for before rcsw/er/client.h is included (e.g., in a header passed with -include). Leaving RCSW_CONFIG_ER_PLUGIN undefined selects LOG4CL, because an undefined macro is 0 in #if. See Event Reporting (ER) .
-
RCSW_ER_PLUGIN_LOG4CL#
LOG4CL: a lighter, simpler log4c with per-module levels.
-
RCSW_ER_PLUGIN_SIMPLE#
The simple plugin: one build-wide level, no per-module control.
-
RCSW_ER_PLUGIN_ZLOG#
zlog: the most full-featured built-in plugin. For Linux and other targets with a full-featured OS.
Defines
-
RCSW_ERL#
The compile-time event reporting level.
-
RCSW_ER_MODNAME_BUILDER(...)#
Takes a comma-separated list of string components of the name you want your module to have. Each “component” corresponds to a level in hierarchical logging scheme. For example, if you want to create a module called “mymodule” in a project called “myproject”, you would do:
#define RCSW_ER_MODNAME RCSW_ER_MODNAME_BUILDER("myproject", "mymodule")which would be expanded as appropriate (“myproject.mymodule”, “myproject_mymodule”, etc.) depending on the plugin you build RCSW with.
This macro ensures that you can use the same code with multiple ER plugins.
rcsw/er/macros.h#
Event reporting macros (ER_DEBUG(), ER_WARN(), …).
When RCSW_ERL is below a level, its ER_<LEVEL>() macro becomes a statement that is never executed and generates no code, so disabled events cost nothing. Its arguments are still type-checked against the format string and count as used. See Levels .
Defines
-
RCSW_ER_INIT(...)#
Initialize the defined ER plugin.
The arguments are passed to the plugin’s init function: none for the simple and LOG4CL plugins, the configuration file path for zlog. All usage of any ER machinery is undefined until this call.
Note
May not be idempotent if the underlying plugin initialization function is not idempotent. See plugin documentation for details.
-
RCSW_ER_DEINIT(...)#
Uninitialize/shutdown the defined ER plugin.
All usage of any ER machinery is undefined after this call until RCSW_ER_INIT is called.
Note
May not be idempotent if the underlying plugin shutdown function is not idempotent. See plugin documentation for details.
-
DPRINTF(...)#
Print through PRINTF() whenever RCSW_ERL is at least FATAL, regardless of module levels.
-
RCSW_ER_MODNAME#
The name of an ER module. Used by some logging plugins to identify a module.
If not specified, defined as __FILE_NAME__.
-
RCSW_ER_MODID#
The ID of an ER module. Used by some logging plugins (e.g., LOG4CL) to identify a module.
If not specified, defined as 0xFFFFFFFF. That value is above every ID RCSW uses internally, so it cannot collide with them; all translation units which do not define their own ID share it. With LOG4CL, an ID that is never installed reports at the plugin’s default level.
-
ER_FATAL(msg, ...)#
Report a FATAL message. With RCSW_ERL above FATAL it goes through the ER plugin, like the other levels. With RCSW_ERL equal to FATAL no plugin is used and the message is printed with DPRINTF(). Either way
"\\r\\n"is appended (see ER_REPORT).
-
ER_ERR(...)#
Report a non-FATAL ERROR message.
-
RCSW_ER_MODULE_INIT(...)#
Initialize a module in the currently selected ER plugin using the RCSW_ER_MODID and RCSW_ER_MODNAME currently in scope.
Initialization is idempotent if the selected plugin supports it. Registration is best-effort bookkeeping (e.g., the plugin may not be initialized yet), so
errnois preserved across the call.
-
RCSW_ER_INSMOD(ID, NAME)#
Install/enable an event report module in the current plugin.
-
ER_WARN(...)#
Report a WARNING message.
-
ER_INFO(...)#
Report an informational message.
-
ER_DEBUG(...)#
Report a debug message.
-
ER_TRACE(...)#
Report a TRACE message.
-
PRINTF(...)#
Print to the terminal, through the configured ER plugin’s print function.
-
ER_ASSERT(cond, msg, ...)#
Check a boolean condition cond in a function. If it is false, report msg as a FATAL event and then fail an
assert(), which halts the program unlessNDEBUGis defined. cond is evaluated in every build.
-
ER_CONDW(cond, msg, ...)#
Check a boolean condition cond in a function. If condition IS true, emit a warning message.
-
ER_CONDI(cond, msg, ...)#
Check a boolean condition cond in a function. If condition IS true, emit an informational message.
-
ER_CONDD(cond, msg, ...)#
Check a boolean condition cond in a function. If condition IS true, emit a debug message.
-
ER_FATAL_SENTINEL(msg, ...)#
Mark a place in the code that must never be reached. If execution gets there, report msg as a FATAL event and call
abort().
-
ER_CHECK(cond, msg, ...)#
Check a boolean condition cond in a function. If it is false, report msg as an ERROR event and go to the function’s
errorlabel, which must exist.
-
ER_SENTINEL(msg, ...)#
Mark a place in the code that should not be reached. If execution gets there, report msg as an ERROR event and go to the function’s
errorlabel, which must exist.
-
ER_REPORT(lvl, handle, ...)#
rcsw/er/plugin/log4cl.h#
A C debugging/logging framework in the style of log4c, but less complex.
Defines
-
RCSW_LOG4CL_NAMELEN#
Maximum length of a module name, including the NUL; longer names are truncated.
-
RCSW_LOG4CL_MODULES#
The LOG4CL module codes used by RCSW.
Your own module IDs must not be below
LOG4CL_EXTERNAL(these codes) or equal to 0xFFFFFFFF (the default RCSW_ER_MODID). Using the upper 32 bits avoids both; see Module ID allocation .
Enums
-
enum log4cl_module_codes#
Module codes generated from RCSW_LOG4CL_MODULES.
Values:
-
enumerator RCSW_XGET_MACRO#
-
enumerator RCSW_XGET_MACRO#
Functions
-
struct log4cl_module *log4cl_mod_query(int64_t id)#
Get the module to report through for the specified ID.
- Parameters:
id – The module ID.
- Returns:
The installed module with that ID. If none is installed, a shared default module whose level is the plugin’s default level (see log4cl_default_lvl_set()), so reports from unregistered modules are filtered rather than dropped. NULL if the plugin is not initialized, or when called from inside LOG4CL itself (which suppresses those reports). The pointer stays valid until the module is removed or the plugin is shut down.
-
bool log4cl_mod_emit(const struct log4cl_module *module, uint8_t lvl)#
Check if a message with the specified level should be emitted.
- Parameters:
module – The module, from log4cl_mod_query(). If NULL, nothing is emitted.
lvl – The level of the message (an
RCSW_ERL_*value).
-
status_t log4cl_init(void)#
Initialize the LOG4CL plugin.
This function is idempotent. It must complete before any other thread calls log4cl_insmod().
- Returns:
-
void log4cl_deinit(void)#
Shut down the LOG4CL plugin.
The plugin can be re-initialized later without error. Module handles previously returned by log4cl_mod_query() become invalid, so no other thread may be reporting through LOG4CL when this is called.
-
status_t log4cl_insmod(int64_t id, const char *name)#
Add a module to the active list of debug printing modules.
If a module with the same ID is already loaded, nothing is done and OK is returned. Safe to call before log4cl_init() (for example from a library module’s *_init() function): it then does nothing and returns ERROR with errno=EAGAIN.
- Parameters:
id – The ID of the module to install.
name – The name of the module. Names need not be unique, but should be. Truncated to RCSW_LOG4CL_NAMELEN - 1 characters.
- Returns:
-
status_t log4cl_rmmod(int64_t id)#
Remove a module from the active list by ID.
Warning
Not safe while other threads may be reporting; see log4cl_plugin.
- Parameters:
id – The ID of the module to remove.
- Returns:
status_t. ERROR with errno=ENOENT if no such module is loaded.
-
status_t log4cl_rmmod2(const char *name)#
Remove a module from the active list by name.
Warning
Not safe while other threads may be reporting; see log4cl_plugin.
- Parameters:
name – The name of the module to remove.
- Returns:
status_t. ERROR with errno=ENOENT if no such module is loaded.
-
status_t log4cl_mod_lvl_set(int64_t id, uint8_t lvl)#
Set the reporting level for a module.
Warning
Not safe while other threads may be reporting; see log4cl_plugin.
-
void log4cl_default_lvl_set(uint8_t lvl)#
Set the default reporting level for the plugin.
All modules installed after calling this function will have the specified level set by default. It also applies immediately to every module ID that is not installed (see log4cl_mod_query()).
Warning
Not safe while other threads may be reporting; see log4cl_plugin.
- Parameters:
lvl – The new default level.
-
int64_t log4cl_mod_id_get(const char *name)#
Get the ID of a module from its name.
- Parameters:
name – The name of the module to retrieve the ID for.
- Returns:
The ID, or -1 if an error occurred.
-
struct log4cl_module#
- #include <rcsw/er/plugin/log4cl.h>
Representation of a module in the LOG4CL plugin.
A module is defined on a per file basis (multiple modules in the same file are disallowed).
Aligned to dptr_t, so that arrays of these in caller-provided memory keep every member naturally aligned. (Not packed: packing would misalign the pointer members.)
Public Members
-
int64_t id#
ID of the module, unique within the application. See RCSW_ER_MODID.
-
uint8_t lvl#
The current reporting level for the module (an
RCSW_ERL_*value).
-
char name[32]#
Name of the module, truncated to fit (see RCSW_LOG4CL_NAMELEN).
-
int64_t id#
-
struct log4cl_plugin#
- #include <rcsw/er/plugin/log4cl.h>
The LOG4CL plugin.
The list of modules currently enabled is maintained by a llist, and the framework also contains a default level that can be set so that all future modules will be installed with that level by default.
On POSIX, module installation, removal, lookup, and level changes are serialized internally by a reader/writer lock, so library modules may be initialized from multiple threads concurrently. Reports issued from inside LOG4CL itself (e.g., by the llist that holds the modules) are suppressed.
log4cl_rmmod() and log4cl_rmmod2(): a concurrent report may still use the removed module (use after free).
log4cl_mod_lvl_set() and log4cl_default_lvl_set(): a concurrent report reads the level while it is written (a data race).
- Configure modules before reporting concurrently
A report looks up its module under the lock, then checks the module’s level after releasing it. Installing modules (log4cl_insmod()) is safe at any time, but the following must only be called while no other thread can be reporting, i.e. during startup or shutdown:
In short: install modules and set levels before starting the threads that report, and do not reconfigure LOG4CL while they run.
Public Members
-
struct llist *modules#
The installed log4cl_module objects.
-
uint8_t default_lvl#
Level given to newly installed modules.
-
bool initialized#
true between log4cl_init() and log4cl_deinit().
rcsw/er/plugin/plugin.h#
Selects the configured ER plugin and checks that it defines everything RCSW needs.
rcsw/er/plugin/simple.h#
A simple C debugging/logging framework which prints with RCSW’s stdio component (stdio_printf(), which wraps the bundled printf library).
Prints every compiled-in report, prefixed with its module name; there is one compile-time level for the whole build and no per-module control. Mainly intended for environments with no stdlib, such as bootstraps. Requires the stdio component.
rcsw/er/plugin/zlog.h#
Adaptor for using ZLOG with RCSW.