]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
log: add common helper log_set_target_and_open()
authorLennart Poettering <lennart@poettering.net>
Tue, 14 Feb 2023 15:10:58 +0000 (16:10 +0100)
committerLennart Poettering <lennart@poettering.net>
Thu, 16 Feb 2023 15:32:41 +0000 (16:32 +0100)
quite often we want to set a log target and immediately open it. Add a
common helper for that.

src/basic/log.c
src/basic/log.h
src/core/main.c
src/coredump/coredump.c
src/shared/bus-log-control-api.c

index fc5793139ed709c656248ae0b6eb6018aa96ba83..6a4373101205a62b2a0368088f76a8672d696614 100644 (file)
@@ -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. */
 
index f73d4c41545c07c6cad1345ee562672043f9c098..0d4956e6b5782d65a8b52f49d94b44f4a4f713ef 100644 (file)
@@ -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_;
 
index c0b8126d9689be20ba2d1489e81cc566d423b71e..f28448f9e43bccf4b532d733f7018d54fbf794ec 100644 (file)
@@ -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;
index 013ebb4c2871d4c50ace650a7f29ba4de35aabcc..d9db98bf323a1a4d8b6e13cfcf46d2267ebb44be 100644 (file)
@@ -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);
index 06e6697a3301a044c53ac4fc1fb3296eecafc7ba..40f99ac2bb29a4c6c7e35880546b09b4b9f5c99b 100644 (file)
@@ -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;
 }