7.2. log — Logging

The logging module consists of log objects and log handlers. A log object filters log entries and a log handler writes log entries to an output channel.

A log object called “log” and a log handler writing to standard output are created during the log module initialization. The log handler can be replaced by calling log_set_default_handler_output_channel().

Normally one log object is created for each subsystem in an application. This gives the user the power to control which parts of the system to debug and/or monitor at runtime.

Sometimes it’s useful to write log entries to multiple channels. This is possible by creating and adding another log handler to the log module.

7.2.1. Log levels

There are five log levels defined; fatal, error, warning, info and debug. The log levels are defined as LOG_<upper case level> in the log module header file.

7.2.2. Log entry format

A log entry consists of a timestamp, log level, thread name, log object name and the message. The timestamp is the log entry creation time and the log level is one of fatal, error, warning, info and debug. The thread name is the name of the thread that created the log entry and the log object name is the name of the log object the entry was printed on. The message is a user defined string.

<timestamp>:<log level>:<thread name>:<log object name>: <message>

7.2.3. Debug file system commands

Three debug file system commands are available, all located in the directory debug/log/.

Command Description
list Print a list of all log objects.
print <string> Print a log entry using the default log object and log
level LOG_INFO. This command has no use except to test
that the log module works.
set_log_mask <obejct> <mask> Set the log mask to <mask> for log object <object>.

Example output from the shell:

$ debug/log/list
     OBJECT NAME  MASK
         default  0x0f
$ debug/log/print "Hello World!"
$ debug/log/set_log_mask default 0x1f
$ debug/log/list
     OBJECT NAME  MASK
         default  0x1f
$ debug/log/print "Hello World!!!"
56:info:main:default: Hello World!!!

7.2.4. Example

Here are a few example outputs using three log objects; foo, bar and the default log object default. All logs are from the main thread as can be seen in the third field in the entries.

23:info:main:foo: A foo info message.
24:info:main:bar: A bar info message.
37:debug:main:bar: A bar debug message.
56:error:main:default: A main error message.

Source code: src/debug/log.h, src/debug/log.c

Test code: tst/debug/log/main.c

Test coverage: src/debug/log.c


Version
7.0.0

Defines

LOG_FATAL

An unhandleable error that results in a program crash.

LOG_ERROR

A handable error conditions.

LOG_WARNING

A warning.

LOG_INFO

Generic (useful) information about system operation.

LOG_DEBUG

Developer debugging messages.

LOG_MASK(level)

Craete a log mask with given level set.

LOG_UPTO(level)

Set all levels up to and including given level.

LOG_ALL

Set all levels.

LOG_NONE

Clear all levels.

Functions

int log_module_init(void)

Initialize the logging module. This function must be called before calling any other function in this module.

The module will only be initialized once even if this function is called multiple times.

Return
zero(0) or negative error code.

int log_object_init(struct log_object_t *self_p, const char *name_p, char mask)

Initialize given log object with given name and mask.

Return
zero(0) or negative error code.
Parameters
  • self_p -

    Log object to initialize.

  • name_p -

    Log object name.

  • mask -

    Log object mask.

int log_object_set_log_mask(struct log_object_t *self_p, char mask)

Set given log mask for given log object.

Return
zero(0) or negative error code.
Parameters
  • self_p -

    Log object.

  • mask -

    Log object mask.

char log_object_get_log_mask(struct log_object_t *self_p)

Get the log mask of given log object.

Return
Log mask.
Parameters
  • self_p -

    Log object.

int log_object_print(struct log_object_t *self_p, int level, const char *fmt_p, ...)

Check if given log level is set in the log object mask. If so, format a log entry and write it to all log handlers.

self_p may be NULL, and in that case the current thread’s log mask is used instead of the log object mask.

Return
zero(0) or negative error code.
Parameters
  • self_p -

    Log object, or NULL to use the thread’s log mask.

  • level -

    Log level.

  • fmt_p -

    Log format string.

  • ... -

    Variable argument list.

int log_handler_init(struct log_handler_t *self_p, chan_t *chout_p)

Initialize given log handler with given output channel.

Return
zero(0) or negative error code.
Parameters
  • self_p -

    Log handler to initialize.

  • chout_p -

    Output handler.

int log_add_handler(struct log_handler_t *handler_p)

Add given log handler to the list of log handlers. Log entries will be written to all log handlers in the list.

Return
zero(0) or negative error code.
Parameters
  • handler_p -

    Log handler to add.

int log_remove_handler(struct log_handler_t *handler_p)

Remove given log handler from the list of log handlers.

Return
zero(0) or negative error code.
Parameters
  • handler_p -

    Log handler to remove.

int log_add_object(struct log_object_t *object_p)

Add given log object to the list of log objects. There are file system commands to list all log objects in the list and also modify their log mask.

Return
zero(0) or negative error code.
Parameters
  • object_p -

    Log object to add.

int log_remove_object(struct log_object_t *object_p)

Remove given log object from the list of log objects.

Return
zero(0) or negative error code.
Parameters
  • object_p -

    Object to remove.

int log_set_default_handler_output_channel(chan_t *chout_p)

Set the output channel of the default log handler.

Return
zero(0) or negative error code.
Parameters
  • chout_p -

    Channel to set as the default output channel. May be NULL if no output should be written.

struct log_handler_t

Public Members

chan_t *chout_p
struct log_handler_t *next_p
struct log_object_t

Public Members

const char *name_p
char mask
struct log_object_t *next_p