systemd/backport-log-add-common-helper-log_set_target_and_open.patch
2024-12-13 16:06:53 +08:00

127 lines
4.7 KiB
Diff

From 1e344c1dc79d93976d019dfa0dbe6d24b28d64d7 Mon Sep 17 00:00:00 2001
From: Lennart Poettering <lennart@poettering.net>
Date: Tue, 14 Feb 2023 16:10:58 +0100
Subject: [PATCH] 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.
Conflict:NA
Reference:https://github.com/systemd/systemd/commit/1e344c1dc79d93976d019dfa0dbe6d24b28d64d7
---
src/basic/log.c | 5 +++++
src/basic/log.h | 1 +
src/core/main.c | 9 +++------
src/coredump/coredump.c | 9 +++------
src/shared/bus-log-control-api.c | 3 +--
5 files changed, 13 insertions(+), 14 deletions(-)
diff --git a/src/basic/log.c b/src/basic/log.c
index fc5793139..6a4373101 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 f73d4c415..0d4956e6b 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 c0b8126d9..f28448f9e 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 013ebb4c2..d9db98bf3 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 06e6697a3..40f99ac2b 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;
}
--
2.33.0