]> git.ipfire.org Git - thirdparty/systemd.git/commit - docs/ENVIRONMENT.md
basic: Add log context
authorDaan De Meyer <daan.j.demeyer@gmail.com>
Thu, 12 May 2022 11:50:51 +0000 (13:50 +0200)
committerDaan De Meyer <daan.j.demeyer@gmail.com>
Sun, 8 Jan 2023 15:31:16 +0000 (16:31 +0100)
commit7c7a9138a20a6657071b3dd112fda9747ba1d6c1
tree5ccfaa8ae2df0691badd2631a85f7866eb034b03
parent6658f7c7920856d638028cd6b9932f14f65b42e1
basic: Add log context

This commit adds support for attaching extra metadata to log
messages written to the journal via log.h. We keep track of a
thread local log context in log.c onto which we can push extra
metadata fields that should be logged. Once a field is no longer
relevant, it can be popped again from the log context.

On top of this, we then add macros to allow pushing extra fields
onto the log context.

LOG_CONTEXT_PUSH() will push the provided field onto the log context
and pop the last field from the log context when the current block
ends. LOG_CONTEXT_PUSH_STRV() will do the same but for all fields in
the given strv.

Using the macros is as simple as putting them anywhere inside a block
to add a field to all following log messages logged from inside that
block.

void myfunction(...) {
...

LOG_CONTEXT_PUSH("MYMETADATA=abc");

// Every journal message logged will now have the MYMETADATA=abc
        // field included.
}

For convenience, there's also LOG_CONTEXT_PUSHF() to allow constructing
the field to be logged using printf() syntax.

log_context_new()/log_context_free() can be used to attach a log context
to an async operation by storing it in the associated userdata struct.
docs/ENVIRONMENT.md
src/basic/list.h
src/basic/log.c
src/basic/log.h
src/test/test-log.c