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.
This commit is contained in:
Daan De Meyer
2025-04-23 09:53:53 +02:00
parent 8aa304d313
commit 64fd6ba9ca
6 changed files with 22 additions and 24 deletions
+1 -2
View 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,
-9
View 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 };
-3
View 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= */
+1 -1
View 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 */
+12 -4
View 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,
+8 -5
View 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())