libqb
1.0.4
|
The logging API provides four main parts (basics, filtering, threading & blackbox).
The idea behind this logging system is not to be prescriptive but to provide a set of tools to help the developer achieve what they want quickly and easily.
Simplest possible use:
QB_LOG_INIT_DATA()
macro placed in the top file scope in exactly one source file (preferably the main one) to be mixed into the resulting compilation unit. This is a self-defensive measure for when the linker-assisted collection of callsite data silently fails, which could otherwise go unnoticed, causing troubles down the road, but alas it cannot discern misuse of QB_LOG_INIT_DATA()
macro in no-logging context from broken callsite section handling assumptions owing to overboard fancy linker – situation that the self-check aims to detect in the first place.To enable a target do the following:
syslog, stderr, the blackbox, and stdout are static (they don't need to be created, just enabled or disabled). However, you can open multiple logfiles (falling within inclusive range QB_LOG_TARGET_DYNAMIC_START
up to QB_LOG_TARGET_DYNAMIC_END
). To do this, use the following code:
Once your targets are enabled/opened, you can configure them as follows: Configure the size of blackbox:
Make logging to file threaded:
Sometimes, syslog daemons are (pre)configured to filter messages not exceeding a particular priority. When this happens to be the logging target, the designated priority of the message is passed along unchanged, possibly resulting in message loss. For messages up to LOG_DEBUG
importance, this can be worked around by proportionally bumping the priorities to be passed to syslog (here, the step is such that LOG_DEBUG
gets promoted to LOG_INFO
):
To ensure all logs to file targets are fsync'ed (new messages expressly transferred to the storage device as they keep coming, otherwise defaults to QB_FALSE
):
So to make all logs from evil_function() go to stderr, do the following:
So to make all logs from totem* (with a priority <= LOG_INFO) go to stderr, do the following:
So to make all logs with the substring "ringbuffer" go to stderr, do the following:
To achieve non-blocking logging, so that any calls to write() or syslog() will not hold up your program, you can use threaded logging as well.
Threaded logging use:
Blackbox usage:
The code above will produce: