From: Daan De Meyer Date: Fri, 18 Apr 2025 14:37:50 +0000 (+0200) Subject: core: Turn manager unit log fields into unit functions X-Git-Tag: v258-rc1~756^2~17 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=64fd6ba9ca462da5b5cb5f913007fd00b7dd87f4;p=thirdparty%2Fsystemd.git core: Turn manager unit log fields into unit functions 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. --- diff --git a/src/analyze/analyze-condition.c b/src/analyze/analyze-condition.c index 111f24f8d98..f96a8c2b40f 100644 --- a/src/analyze/analyze-condition.c +++ b/src/analyze/analyze-condition.c @@ -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, diff --git a/src/core/manager.c b/src/core/manager.c index 3b34f0494e4..cf000d8f218 100644 --- a/src/core/manager.c +++ b/src/core/manager.c @@ -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 }; diff --git a/src/core/manager.h b/src/core/manager.h index 6fef909350d..a07c665e5b5 100644 --- a/src/core/manager.h +++ b/src/core/manager.h @@ -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= */ diff --git a/src/core/transaction.c b/src/core/transaction.c index 2c4eef965dc..07fd612d918 100644 --- a/src/core/transaction.c +++ b/src/core/transaction.c @@ -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 */ diff --git a/src/core/unit.c b/src/core/unit.c index ac0528511d5..c25777671df 100644 --- a/src/core/unit.c +++ b/src/core/unit.c @@ -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, diff --git a/src/core/unit.h b/src/core/unit.h index 7e59443415b..6a46cbda0a2 100644 --- a/src/core/unit.h +++ b/src/core/unit.h @@ -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())