]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
core: append LogExtraFields= values to log_unit* messages 26794/head
authorLuca Boccassi <bluca@debian.org>
Mon, 13 Mar 2023 21:34:37 +0000 (21:34 +0000)
committerLuca Boccassi <bluca@debian.org>
Tue, 14 Mar 2023 16:38:23 +0000 (16:38 +0000)
This ensure messages from PID1 regarding a unit also contain those
fields. For example, portable services have PORTABLE=<image> as
extra fields, which is useful to identify which version of a portable
image produced a log message like an error or an oomd kill.

man/systemd.exec.xml
src/core/unit.h

index 653aa0d7921ce240d19976da8d594131e8ac225c..4001123a96242abfe9bc5b11ed1846a0c3b4ed5c 100644 (file)
@@ -2909,8 +2909,8 @@ StandardInputData=V2XigLJyZSBubyBzdHJhbmdlcnMgdG8gbG92ZQpZb3Uga25vdyB0aGUgcnVsZX
         <term><varname>LogExtraFields=</varname></term>
 
         <listitem><para>Configures additional log metadata fields to include in all log records generated by
-        processes associated with this unit. This setting takes one or more journal field assignments in the
-        format <literal>FIELD=VALUE</literal> separated by whitespace. See
+        processes associated with this unit, including systemd. This setting takes one or more journal field
+        assignments in the format <literal>FIELD=VALUE</literal> separated by whitespace. See
         <citerefentry><refentrytitle>systemd.journal-fields</refentrytitle><manvolnum>7</manvolnum></citerefentry>
         for details on the journal field concept. Even though the underlying journal implementation permits
         binary field values, this setting accepts only valid UTF-8 values. To include space characters in a
index 3f8377fbf64ebdc558dba196317a3e152cee7db9..d5a6d595e293778b5c51035648c2334a36660636 100644 (file)
@@ -1078,7 +1078,13 @@ Condition *unit_find_failed_condition(Unit *u);
         ({                                                              \
                 const Unit *_u = (unit);                                \
                 const int _l = (level);                                 \
-                (log_get_max_level() < LOG_PRI(_l) || (_u && !unit_log_level_test(_u, _l))) ? -ERRNO_VALUE(error) : \
+                bool _do_log = !(log_get_max_level() < LOG_PRI(_l) ||   \
+                        (_u && !unit_log_level_test(_u, _l)));          \
+                const ExecContext *_c = _do_log && _u ?                 \
+                        unit_get_exec_context(_u) : NULL;               \
+                LOG_CONTEXT_PUSH_IOV(_c ? _c->log_extra_fields : NULL,  \
+                                     _c ? _c->n_log_extra_fields : 0);  \
+                !_do_log ? -ERRNO_VALUE(error) :                        \
                         _u ? log_object_internal(_l, error, PROJECT_FILE, __LINE__, __func__, _u->manager->unit_log_field, _u->id, _u->manager->invocation_log_field, _u->invocation_id_string, ##__VA_ARGS__) : \
                                 log_internal(_l, error, PROJECT_FILE, __LINE__, __func__, ##__VA_ARGS__); \
         })
@@ -1116,7 +1122,12 @@ Condition *unit_find_failed_condition(Unit *u);
         ({                                                              \
                 const Unit *_u = (unit);                                \
                 const int _l = (level);                                 \
-                unit_log_level_test(_u, _l) ?                           \
+                bool _do_log = unit_log_level_test(_u, _l);             \
+                const ExecContext *_c = _do_log && _u ?                 \
+                        unit_get_exec_context(_u) : NULL;               \
+                LOG_CONTEXT_PUSH_IOV(_c ? _c->log_extra_fields : NULL,  \
+                                     _c ? _c->n_log_extra_fields : 0);  \
+                _do_log ?                                               \
                         log_struct_errno(_l, error, __VA_ARGS__, LOG_UNIT_ID(_u)) : \
                         -ERRNO_VALUE(error);                            \
         })
@@ -1125,8 +1136,14 @@ Condition *unit_find_failed_condition(Unit *u);
 
 #define log_unit_struct_iovec_errno(unit, level, error, iovec, n_iovec) \
         ({                                                              \
+                const Unit *_u = (unit);                                \
                 const int _l = (level);                                 \
-                unit_log_level_test(unit, _l) ?                         \
+                bool _do_log = unit_log_level_test(_u, _l);             \
+                const ExecContext *_c = _do_log && _u ?                 \
+                        unit_get_exec_context(_u) : NULL;               \
+                LOG_CONTEXT_PUSH_IOV(_c ? _c->log_extra_fields : NULL,  \
+                                     _c ? _c->n_log_extra_fields : 0);  \
+                _do_log ?                                               \
                         log_struct_iovec_errno(_l, error, iovec, n_iovec) : \
                         -ERRNO_VALUE(error);                            \
         })