Add systemd_namespace_sink for logging to specific systemd namespace#3606
Merged
Conversation
Adds systemd_namespace_sink that can open a stream to a specific systemd journal namespace for logging. This was created as a separate sink from systemd_sink because it has significant difference due to systemd's journal API: `sd_journal_send()` does not have namespace-specific counterpart and thus this cannot utilize other extra journal fields than `PRIORITY` (via kernel-style log level prefixes). Another difference is that this API is only available from systemd version 256, thus a version check was added to CMake file for the unit tests so that they are not automatically included if `libsystemd` is not new enough. Unit tests are split to two parts: first one runs always and it just uses the stream to log to default namespace (should always work). Second one requires manual setting up of "custom" journal namespace and thus is ignored from the default set of tests (can be run by specifying `[systemd_namespace]` tag explicitly when running tests). On the other hand, the second part also automatically checks the output correctness from journal.
gabime
requested changes
May 7, 2026
`SD_JOURNAL_SUPPRESS_LOCATION` is not needed by this sink since it does not use the APIs that add the location info. Moved level prefixes to `static constexpr` to indicate those being compile-time constants (and also saves a bit space). Removed message length truncation since with this stream API we don't even know the maximum length of the messages. Journald truncates the message at its end based on its own configuration. Added error checking to `fwrite_bytes()` calls similarly to other sinks. Optimized away excess `\n` check. Added `flush_()` implementation.
Contributor
Author
|
Did a bunch of fixes, cleanup, and some optimizations. See the commit message for details. |
gabime
reviewed
May 7, 2026
gabime
reviewed
May 7, 2026
gabime
reviewed
May 7, 2026
gabime
requested changes
May 7, 2026
gabime
requested changes
May 8, 2026
gabime
reviewed
May 9, 2026
gabime
requested changes
May 9, 2026
Owner
|
Thanks @oko256 ! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds systemd_namespace_sink that can open a stream to a specific systemd journal namespace for logging. This was created as a separate sink from systemd_sink because it has significant difference due to systemd's journal API:
sd_journal_send()does not have namespace-specific counterpart and thus this cannot utilize other extra journal fields thanPRIORITY(via kernel-style log level prefixes).Another difference is that this namespace stream API is only available from systemd version 256, thus a version check was added to CMake file for the unit tests so that they are not automatically included if
libsystemdis not new enough.Unit tests are split to two parts: first one runs always and it just uses the stream to log to default namespace (should always work). Second one requires manual setting up of "custom" journal namespace and thus is ignored from the default set of tests (can be run by specifying
[systemd_namespace]tag explicitly when running tests). On the other hand, the second part also automatically checks the output correctness from journal.