]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
core: Turn manager unit log fields into unit functions
authorDaan De Meyer <daan.j.demeyer@gmail.com>
Fri, 18 Apr 2025 14:37:50 +0000 (16:37 +0200)
committerDaan De Meyer <daan.j.demeyer@gmail.com>
Wed, 23 Apr 2025 07:53:53 +0000 (09:53 +0200)
There's no need for these to be fields inside the manager struct,
let's turn them into functions in unit.h instead, again to allow
forward declaring the Manager struct in a later commit.

src/analyze/analyze-condition.c
src/core/manager.c
src/core/manager.h
src/core/transaction.c
src/core/unit.c
src/core/unit.h

index 111f24f8d98ebbd8e4159eb41972fcd166411a32..f96a8c2b40f74eeffa7a01a6b0c2dc85bee9d690 100644 (file)
@@ -61,7 +61,6 @@ static int parse_condition(Unit *u, const char *line) {
 _printf_(7, 8)
 static int log_helper(void *userdata, int level, int error, const char *file, int line, const char *func, const char *format, ...) {
         Unit *u = ASSERT_PTR(userdata);
-        Manager *m = ASSERT_PTR(u->manager);
         va_list ap;
         int r;
 
@@ -70,7 +69,7 @@ static int log_helper(void *userdata, int level, int error, const char *file, in
 
         va_start(ap, format);
         r = log_object_internalv(level, error, file, line, func,
-                                 /* object_field = */ m->unit_log_field,
+                                 /* object_field = */ unit_log_field(u),
                                  /* object = */ u->id,
                                  /* extra_field = */ NULL,
                                  /* extra = */ NULL,
index 3b34f0494e46daf6038afee635feac5db3bedaf2..cf000d8f2188060720bec0974abacb3bd372f793 100644 (file)
@@ -928,15 +928,6 @@ int manager_new(RuntimeScope runtime_scope, ManagerTestRunFlags test_run_flags,
                                 m->timestamps + MANAGER_TIMESTAMP_LOADER);
 #endif
 
-        /* Prepare log fields we can use for structured logging */
-        if (MANAGER_IS_SYSTEM(m)) {
-                m->unit_log_field = "UNIT=";
-                m->invocation_log_field = "INVOCATION_ID=";
-        } else {
-                m->unit_log_field = "USER_UNIT=";
-                m->invocation_log_field = "USER_INVOCATION_ID=";
-        }
-
         /* Reboot immediately if the user hits C-A-D more often than 7x per 2s */
         m->ctrl_alt_del_ratelimit = (const RateLimit) { .interval = 2 * USEC_PER_SEC, .burst = 7 };
 
index 6fef909350d6ddee706efc249dd49ac5a115fe60..a07c665e5b5b1c651ab97090b60231227c32cd7d 100644 (file)
@@ -472,9 +472,6 @@ struct Manager {
         RateLimit ctrl_alt_del_ratelimit;
         EmergencyAction cad_burst_action;
 
-        const char *unit_log_field;
-        const char *invocation_log_field;
-
         int first_boot; /* tri-state */
 
         /* Prefixes of e.g. RuntimeDirectory= */
index 2c4eef965dc58fde803f12026b564e091a068bb6..07fd612d918f767f05344e4a85da635906333abd 100644 (file)
@@ -393,7 +393,7 @@ static int transaction_verify_order_one(Transaction *tr, Job *j, Job *from, unsi
                                 break;
                 }
 
-                unit_ids = merge_unit_ids(j->manager->unit_log_field, array); /* ignore error */
+                unit_ids = merge_unit_ids(unit_log_field(j->unit), array); /* ignore error */
 
                 STRV_FOREACH_PAIR(unit_id, job_type, array)
                         /* logging for j not k here to provide a consistent narrative */
index ac0528511d5f7c82699a9f9c771ca6eaca0e7198..c25777671df0f79d5b48b3ac78c968d97f3b1398 100644 (file)
@@ -1727,9 +1727,9 @@ static int log_unit_internal(void *userdata, int level, int error, const char *f
         va_start(ap, format);
         if (u)
                 r = log_object_internalv(level, error, file, line, func,
-                                         u->manager->unit_log_field,
+                                         unit_log_field(u),
                                          u->id,
-                                         u->manager->invocation_log_field,
+                                         unit_invocation_log_field(u),
                                          u->invocation_id_string,
                                          format, ap);
         else
@@ -2491,10 +2491,10 @@ static int unit_log_resources(Unit *u) {
         if (!set_iovec_string_field(iovec, &n_iovec, "MESSAGE_ID=", SD_MESSAGE_UNIT_RESOURCES_STR))
                 return log_oom();
 
-        if (!set_iovec_string_field(iovec, &n_iovec, u->manager->unit_log_field, u->id))
+        if (!set_iovec_string_field(iovec, &n_iovec, unit_log_field(u), u->id))
                 return log_oom();
 
-        if (!set_iovec_string_field(iovec, &n_iovec, u->manager->invocation_log_field, u->invocation_id_string))
+        if (!set_iovec_string_field(iovec, &n_iovec, unit_invocation_log_field(u), u->invocation_id_string))
                 return log_oom();
 
         log_unit_struct_iovec(u, log_level, iovec, n_iovec);
@@ -6642,6 +6642,14 @@ int unit_compare_priority(Unit *a, Unit *b) {
         return strcmp(a->id, b->id);
 }
 
+const char* unit_log_field(const Unit *u) {
+        return MANAGER_IS_SYSTEM(ASSERT_PTR(u)->manager) ? "UNIT=" : "USER_UNIT=";
+}
+
+const char* unit_invocation_log_field(const Unit *u) {
+        return MANAGER_IS_SYSTEM(ASSERT_PTR(u)->manager) ? "INVOCATION_ID=" : "USER_INVOCATION_ID=";
+}
+
 const ActivationDetailsVTable * const activation_details_vtable[_UNIT_TYPE_MAX] = {
         [UNIT_PATH]  = &activation_details_path_vtable,
         [UNIT_TIMER] = &activation_details_timer_vtable,
index 7e59443415b3e98afe28ab45c06902b65a5c10ac..6a46cbda0a22322006fd1e79ccd86959bb15b7d1 100644 (file)
@@ -1054,6 +1054,9 @@ bool unit_passes_filter(Unit *u, char * const *states, char * const *patterns);
 
 int unit_compare_priority(Unit *a, Unit *b);
 
+const char* unit_log_field(const Unit *u);
+const char* unit_invocation_log_field(const Unit *u);
+
 UnitMountDependencyType unit_mount_dependency_type_from_string(const char *s) _const_;
 const char* unit_mount_dependency_type_to_string(UnitMountDependencyType t) _const_;
 UnitDependency unit_mount_dependency_type_to_dependency_type(UnitMountDependencyType t) _pure_;
@@ -1071,7 +1074,7 @@ UnitDependency unit_mount_dependency_type_to_dependency_type(UnitMountDependency
                 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__) : \
+                        _u ? log_object_internal(_l, error, PROJECT_FILE, __LINE__, __func__, unit_log_field(_u), _u->id, unit_invocation_log_field(_u), _u->invocation_id_string, ##__VA_ARGS__) : \
                                 log_internal(_l, error, PROJECT_FILE, __LINE__, __func__, ##__VA_ARGS__); \
         })
 
@@ -1138,8 +1141,8 @@ UnitDependency unit_mount_dependency_type_to_dependency_type(UnitMountDependency
 
 /* Like LOG_MESSAGE(), but with the unit name prefixed. */
 #define LOG_UNIT_MESSAGE(unit, fmt, ...) LOG_MESSAGE("%s: " fmt, (unit)->id, ##__VA_ARGS__)
-#define LOG_UNIT_ID(unit) LOG_ITEM("%s%s", (unit)->manager->unit_log_field, (unit)->id)
-#define LOG_UNIT_INVOCATION_ID(unit) LOG_ITEM("%s%s", (unit)->manager->invocation_log_field, (unit)->invocation_id_string)
+#define LOG_UNIT_ID(unit) LOG_ITEM("%s%s", unit_log_field((unit)), (unit)->id)
+#define LOG_UNIT_INVOCATION_ID(unit) LOG_ITEM("%s%s", unit_invocation_log_field((unit)), (unit)->invocation_id_string)
 
 const char* collect_mode_to_string(CollectMode m) _const_;
 CollectMode collect_mode_from_string(const char *s) _pure_;
@@ -1198,8 +1201,8 @@ typedef struct UnitForEachDependencyData {
 #define _LOG_CONTEXT_PUSH_UNIT(unit, u, c)                                                              \
         const Unit *u = (unit);                                                                         \
         const ExecContext *c = unit_get_exec_context(u);                                                \
-        LOG_CONTEXT_PUSH_KEY_VALUE(u->manager->unit_log_field, u->id);                                  \
-        LOG_CONTEXT_PUSH_KEY_VALUE(u->manager->invocation_log_field, u->invocation_id_string);          \
+        LOG_CONTEXT_PUSH_KEY_VALUE(unit_log_field(u), u->id);                                           \
+        LOG_CONTEXT_PUSH_KEY_VALUE(unit_invocation_log_field(u), u->invocation_id_string);              \
         LOG_CONTEXT_PUSH_IOV(c ? c->log_extra_fields : NULL, c ? c->n_log_extra_fields : 0);            \
         LOG_CONTEXT_SET_LOG_LEVEL(c->log_level_max >= 0 ? c->log_level_max : log_get_max_level())