SD-JOURNAL(3)
π SD-JOURNAL(3) β the sd-journal manual, bestie edition π
okay so you found the sd-journal page (section 3, the library one, not the shell command one) and honestly? you're about to have a great time. this is the whole vibe of talking to the journal from C. let's go β¨
πΈ NAME πΈ
sd-journal β the APIs for submitting and querying log entries to and from the journal. that's it, that's the whole thing, and it's iconic π
π¦ SYNOPSIS π¦
#include <systemd/sd-journal.h>
pkg-config --cflags --libs libsystemd
like, that's the two lines you need. the include and the pkg-config call. no cap, that's the whole entry fee π
π DESCRIPTION π
sd-journal.h is part of libsystemd(3) and it hands you the APIs to submit log entries and to query them back out. the APIs it exposes are doing double duty, and the way that it's doing both at once is honestly so main character:
- it acts as a client for the
systemd-journald.service(8)journal service β¨ - and it also acts as a parser for the journal files sitting on disk πΎ
so yeah, same header, two whole personalities. we love a versatile icon.
for the actual details on the functions implemented, go read these, they're all linked and all relevant:
sd_journal_print(3)sd_journal_stream_fd(3)sd_journal_open(3)sd_journal_next(3)sd_journal_get_realtime_usec(3)sd_journal_add_match(3)sd_journal_seek_head(3)sd_journal_enumerate_fields(3)sd_journal_get_cursor(3)sd_journal_get_cutoff_realtime_usec(3)sd_journal_get_cutoff_monotonic_usec(3)sd_journal_get_usage(3)sd_journal_get_catalog(3)sd_journal_get_fd(3)sd_journal_has_runtime_files(3)sd_journal_has_persistent_files(3)
and if you'd rather not write C at all (valid, honestly), there's command line access too:
- want to submit entries to the journal from the terminal? that's
systemd-cat(1)π± - want to query entries from the journal? that's
journalctl(1), the goat π
π§ THREAD SAFETY π§
okay listen, this part matters, don't skip it π
the functions that operate on sd_journal objects are thread agnostic β meaning a given sd_journal pointer may only be used from one thread at a time, but multiple independent threads may use multiple objects concurrently. so like, one pointer per thread, but many pointers, many threads, all good. it's giving healthy boundaries π
but some functions are built different β they're fully thread-safe and may be called from multiple threads in parallel:
- the ones used to send entries to the journal, like
sd_journal_print(3)and similar β¨ - and the ones used to retrieve global information, like
sd_journal_stream_fd(3)andsd_journal_get_catalog_for_message_id(3)π
those ones you can just spam from everywhere. no thoughts, head empty, thread-safe π«Ά
β OPTIONAL DEPENDENCIES β
depending on which build-time options are enabled, functions that operate on sd_journal objects might cause optional shared libraries to be dynamically loaded via dlopen(3). which libraries? things like:
- decompression libraries β
xz,lz4,zstdποΈ - cryptographic libraries β
gcryptπ
so yeah, your build config decides whether those get pulled in at runtime. surprise mechanics, but make it libraries π
π NOTES π
the functions described here are available as a shared library, which you can compile against and link to with the libsystemd pkg-config(1) file. that's the pkg-config line from the synopsis, told you it was important π
β οΈ okay real talk, important caveat: the code described here usesgetenv(3), which is declared to be not multi-thread-safe. this means the code calling the functions described here must not callsetenv(3)from a parallel thread. it is recommended to only do calls tosetenv()from an early phase of the program when no other threads have been started. so set your env vars first, then start your threads. order matters, bestie, do not test this π
π SEE ALSO π
the whole friend group, all together, as it should be:
systemd(1)sd_journal_print(3)sd_journal_stream_fd(3)sd_journal_open(3)sd_journal_next(3)sd_journal_get_data(3)sd_journal_get_realtime_usec(3)sd_journal_add_match(3)sd_journal_seek_head(3)sd_journal_enumerate_fields(3)sd_journal_get_cursor(3)sd_journal_get_cutoff_realtime_usec(3)sd_journal_get_cutoff_monotonic_usec(3)sd_journal_get_usage(3)sd_journal_get_fd(3)sd_journal_query_unique(3)sd_journal_get_catalog(3)sd_journal_has_runtime_files(3)sd_journal_has_persistent_files(3)journalctl(1)sd-id128(3)pkg-config(1)
okay that's the whole page, you're officially journal-pilled π this little beauty is from systemd 261.3 β go forth and log things, ily, bye πβ¨π¦
π¦ where this page comes from
- ubuntu stonking: libsystemd-dev 261.3-0ubuntu3 Β· LGPL-2.1-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms