From: Lennart Poettering Date: Tue, 14 Feb 2023 15:10:58 +0000 (+0100) Subject: log: add common helper log_set_target_and_open() X-Git-Tag: v254-rc1~1263 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=1e344c1dc79d93976d019dfa0dbe6d24b28d64d7;p=thirdparty%2Fsystemd.git log: add common helper log_set_target_and_open() quite often we want to set a log target and immediately open it. Add a common helper for that. --- diff --git a/src/basic/log.c b/src/basic/log.c index fc5793139ed..6a437310120 100644 --- a/src/basic/log.c +++ b/src/basic/log.c @@ -347,6 +347,11 @@ void log_set_target(LogTarget target) { log_target = target; } +void log_set_target_and_open(LogTarget target) { + log_set_target(target); + log_open(); +} + void log_close(void) { /* Do not call from library code. */ diff --git a/src/basic/log.h b/src/basic/log.h index f73d4c41545..0d4956e6b57 100644 --- a/src/basic/log.h +++ b/src/basic/log.h @@ -51,6 +51,7 @@ static inline void clear_log_syntax_callback(dummy_t *dummy) { const char *log_target_to_string(LogTarget target) _const_; LogTarget log_target_from_string(const char *s) _pure_; void log_set_target(LogTarget target); +void log_set_target_and_open(LogTarget target); int log_set_target_from_string(const char *e); LogTarget log_get_target(void) _pure_; diff --git a/src/core/main.c b/src/core/main.c index c0b8126d968..f28448f9e43 100644 --- a/src/core/main.c +++ b/src/core/main.c @@ -2787,8 +2787,7 @@ int main(int argc, char *argv[]) { if (detect_container() <= 0) { /* Running outside of a container as PID 1 */ - log_set_target(LOG_TARGET_KMSG); - log_open(); + log_set_target_and_open(LOG_TARGET_KMSG); if (in_initrd()) initrd_timestamp = userspace_timestamp; @@ -2832,8 +2831,7 @@ int main(int argc, char *argv[]) { } else { /* Running inside a container, as PID 1 */ - log_set_target(LOG_TARGET_CONSOLE); - log_open(); + log_set_target_and_open(LOG_TARGET_CONSOLE); /* For later on, see above... */ log_set_target(LOG_TARGET_JOURNAL); @@ -2880,8 +2878,7 @@ int main(int argc, char *argv[]) { /* Running as user instance */ arg_system = false; log_set_always_reopen_console(true); - log_set_target(LOG_TARGET_AUTO); - log_open(); + log_set_target_and_open(LOG_TARGET_AUTO); /* clear the kernel timestamp, because we are not PID 1 */ kernel_timestamp = DUAL_TIMESTAMP_NULL; diff --git a/src/coredump/coredump.c b/src/coredump/coredump.c index 013ebb4c287..d9db98bf323 100644 --- a/src/coredump/coredump.c +++ b/src/coredump/coredump.c @@ -1486,11 +1486,9 @@ static int process_kernel(int argc, char* argv[]) { if (r < 0) goto finish; - if (!context.is_journald) { + if (!context.is_journald) /* OK, now we know it's not the journal, hence we can make use of it now. */ - log_set_target(LOG_TARGET_JOURNAL_OR_KMSG); - log_open(); - } + log_set_target_and_open(LOG_TARGET_JOURNAL_OR_KMSG); /* If this is PID 1 disable coredump collection, we'll unlikely be able to process * it later on. @@ -1589,8 +1587,7 @@ static int run(int argc, char *argv[]) { /* First, log to a safe place, since we don't know what crashed and it might * be journald which we'd rather not log to then. */ - log_set_target(LOG_TARGET_KMSG); - log_open(); + log_set_target_and_open(LOG_TARGET_KMSG); /* Make sure we never enter a loop */ (void) prctl(PR_SET_DUMPABLE, 0); diff --git a/src/shared/bus-log-control-api.c b/src/shared/bus-log-control-api.c index 06e6697a330..40f99ac2bb2 100644 --- a/src/shared/bus-log-control-api.c +++ b/src/shared/bus-log-control-api.c @@ -86,8 +86,7 @@ int bus_property_set_log_target( return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid log target '%s'", t); log_info("Setting log target to %s.", log_target_to_string(target)); - log_set_target(target); - log_open(); + log_set_target_and_open(target); return 0; }