From 7881f485c9f57b1c7de4308eeab54458890c5c19 Mon Sep 17 00:00:00 2001 From: Daan De Meyer Date: Wed, 31 Jul 2024 13:11:51 +0200 Subject: [PATCH 1/2] execute: Drop log level to unit log level in exec_spawn() All messages logged from exec_spawn() are attributed to the unit and as such we should set the log level to the unit's max log level for the duration of the function. --- src/basic/log.c | 5 ++++- src/basic/log.h | 11 ++++++++++- src/core/execute.c | 1 + 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/basic/log.c b/src/basic/log.c index a8056d2635..09500f9cb0 100644 --- a/src/basic/log.c +++ b/src/basic/log.c @@ -396,9 +396,10 @@ void log_forget_fds(void) { console_fd_is_tty = -1; } -void log_set_max_level(int level) { +int log_set_max_level(int level) { assert(level == LOG_NULL || LOG_PRI(level) == level); + int old = log_max_level; log_max_level = level; /* Also propagate max log level to libc's syslog(), just in case some other component loaded into our @@ -411,6 +412,8 @@ void log_set_max_level(int level) { /* Ensure that our own LOG_NULL define maps sanely to the log mask */ assert_cc(LOG_UPTO(LOG_NULL) == 0); + + return old; } void log_set_facility(int facility) { diff --git a/src/basic/log.h b/src/basic/log.h index 2da6d5767d..0412e8a871 100644 --- a/src/basic/log.h +++ b/src/basic/log.h @@ -56,7 +56,7 @@ int log_set_target_from_string(const char *e); LogTarget log_get_target(void) _pure_; void log_settle_target(void); -void log_set_max_level(int level); +int log_set_max_level(int level); int log_set_max_level_from_string(const char *e); int log_get_max_level(void) _pure_; int log_max_levels_to_string(int level, char **ret); @@ -491,6 +491,15 @@ size_t log_context_num_contexts(void); /* Returns the number of fields in all attached log contexts. */ size_t log_context_num_fields(void); +static inline void _reset_log_level(int *saved_log_level) { + assert(saved_log_level); + + log_set_max_level(*saved_log_level); +} + +#define LOG_CONTEXT_SET_LOG_LEVEL(level) \ + _cleanup_(_reset_log_level) _unused_ int _saved_log_level_ = log_set_max_level(level); + #define LOG_CONTEXT_PUSH(...) \ LOG_CONTEXT_PUSH_STRV(STRV_MAKE(__VA_ARGS__)) diff --git a/src/core/execute.c b/src/core/execute.c index cdc1277956..26b2c0fb4a 100644 --- a/src/core/execute.c +++ b/src/core/execute.c @@ -396,6 +396,7 @@ int exec_spawn( assert(ret); LOG_CONTEXT_PUSH_UNIT(unit); + LOG_CONTEXT_SET_LOG_LEVEL(context->log_level_max >= 0 ? context->log_level_max : log_get_max_level()); r = exec_context_load_environment(unit, context, ¶ms->files_env); if (r < 0) From fc83ff3f55ee53fd9101d4e45736f3f996ee7ca6 Mon Sep 17 00:00:00 2001 From: Daan De Meyer Date: Wed, 31 Jul 2024 11:39:04 +0200 Subject: [PATCH 2/2] log: Fix size calculation for number of iovecs Each log context field can expand to up to three iovecs (key, value and newline) so let's fix the size calculation to take this into account. --- src/basic/log.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/basic/log.c b/src/basic/log.c index 09500f9cb0..571f5e43dc 100644 --- a/src/basic/log.c +++ b/src/basic/log.c @@ -747,7 +747,7 @@ static int write_to_journal( if (LOG_PRI(level) > log_target_max_level[LOG_TARGET_JOURNAL]) return 0; - iovec_len = MIN(6 + _log_context_num_fields * 2, IOVEC_MAX); + iovec_len = MIN(6 + _log_context_num_fields * 3, IOVEC_MAX); iovec = newa(struct iovec, iovec_len); log_do_header(header, sizeof(header), level, error, file, line, func, object_field, object, extra_field, extra); @@ -1099,7 +1099,7 @@ int log_struct_internal( int r; bool fallback = false; - iovec_len = MIN(17 + _log_context_num_fields * 2, IOVEC_MAX); + iovec_len = MIN(17 + _log_context_num_fields * 3, IOVEC_MAX); iovec = newa(struct iovec, iovec_len); /* If the journal is available do structured logging. @@ -1196,7 +1196,7 @@ int log_struct_iovec_internal( struct iovec *iovec; size_t n = 0, iovec_len; - iovec_len = MIN(1 + n_input_iovec * 2 + _log_context_num_fields * 2, IOVEC_MAX); + iovec_len = MIN(1 + n_input_iovec * 2 + _log_context_num_fields * 3, IOVEC_MAX); iovec = newa(struct iovec, iovec_len); log_do_header(header, sizeof(header), level, error, file, line, func, NULL, NULL, NULL, NULL);