]> git.proxmox.com Git - mirror_frr.git/commitdiff
lib: autodetect systemd/journald log on stdout
authorDavid Lamparter <equinox@opensourcerouting.org>
Sun, 16 Jan 2022 15:15:41 +0000 (16:15 +0100)
committerDavid Lamparter <equinox@opensourcerouting.org>
Sun, 16 Jan 2022 23:39:00 +0000 (00:39 +0100)
systemd sets up environment variables to allow autodetecting and
switching the log format to journald native.  Make use of that for the
stdout logging target.

Signed-off-by: David Lamparter <equinox@opensourcerouting.org>
lib/log_vty.c
lib/systemd.h

index d50e2788ef51d2eb66141e70a586516c8d6c0902..682c9ea372b36ed14187dba1ba1c3b3a7b84b770 100644 (file)
@@ -27,6 +27,7 @@
 #include "lib/zlog_5424.h"
 #include "lib/lib_errors.h"
 #include "lib/printfrr.h"
+#include "lib/systemd.h"
 
 #ifndef VTYSH_EXTRACT_PL
 #include "lib/log_vty_clippy.c"
@@ -52,15 +53,34 @@ static struct zlog_cfg_file zt_file_cmdline = {
 static struct zlog_cfg_file zt_file = {
        .prio_min = ZLOG_DISABLED,
 };
-static struct zlog_cfg_file zt_stdout = {
-       .prio_min = ZLOG_DISABLED,
-};
 static struct zlog_cfg_filterfile zt_filterfile = {
        .parent = {
                .prio_min = ZLOG_DISABLED,
        },
 };
 
+static struct zlog_cfg_file zt_stdout_file = {
+       .prio_min = ZLOG_DISABLED,
+};
+static struct zlog_cfg_5424 zt_stdout_journald = {
+       .prio_min = ZLOG_DISABLED,
+
+       .fmt = ZLOG_FMT_JOURNALD,
+       .dst = ZLOG_5424_DST_UNIX,
+       .filename = "/run/systemd/journal/socket",
+
+       /* this can't be changed through config since this target substitutes
+        * in for the "plain" stdout target
+        */
+       .facility = LOG_DAEMON,
+       .kw_version = false,
+       .kw_location = true,
+       .kw_uid = true,
+       .kw_ec = true,
+       .kw_args = true,
+};
+static bool stdout_journald_in_use;
+
 const char *zlog_progname;
 static const char *zlog_protoname;
 
@@ -163,14 +183,18 @@ DEFUN_NOSH (show_logging,
            SHOW_STR
            "Show current logging configuration\n")
 {
+       int stdout_prio;
+
        log_show_syslog(vty);
 
+       stdout_prio = stdout_journald_in_use ? zt_stdout_journald.prio_min
+                                            : zt_stdout_file.prio_min;
+
        vty_out(vty, "Stdout logging: ");
-       if (zt_stdout.prio_min == ZLOG_DISABLED)
+       if (stdout_prio == ZLOG_DISABLED)
                vty_out(vty, "disabled");
        else
-               vty_out(vty, "level %s",
-                       zlog_priority[zt_stdout.prio_min]);
+               vty_out(vty, "level %s", zlog_priority[stdout_prio]);
        vty_out(vty, "\n");
 
        vty_out(vty, "File logging: ");
@@ -210,6 +234,21 @@ DEFUN_NOSH (show_logging,
        return CMD_SUCCESS;
 }
 
+static void log_stdout_apply_level(void)
+{
+       int maxlvl;
+
+       maxlvl = ZLOG_MAXLVL(log_config_stdout_lvl, log_cmdline_stdout_lvl);
+
+       if (stdout_journald_in_use) {
+               zt_stdout_journald.prio_min = maxlvl;
+               zlog_5424_apply_meta(&zt_stdout_journald);
+       } else {
+               zt_stdout_file.prio_min = maxlvl;
+               zlog_file_set_other(&zt_stdout_file);
+       }
+}
+
 DEFPY (config_log_stdout,
        config_log_stdout_cmd,
        "log stdout [<emergencies|alerts|critical|errors|warnings|notifications|informational|debugging>$levelarg]",
@@ -227,9 +266,7 @@ DEFPY (config_log_stdout,
                level = log_default_lvl;
 
        log_config_stdout_lvl = level;
-       zt_stdout.prio_min = ZLOG_MAXLVL(log_config_stdout_lvl,
-                                        log_cmdline_stdout_lvl);
-       zlog_file_set_other(&zt_stdout);
+       log_stdout_apply_level();
        return CMD_SUCCESS;
 }
 
@@ -242,9 +279,7 @@ DEFUN (no_config_log_stdout,
        LOG_LEVEL_DESC)
 {
        log_config_stdout_lvl = ZLOG_DISABLED;
-       zt_stdout.prio_min = ZLOG_MAXLVL(log_config_stdout_lvl,
-                                        log_cmdline_stdout_lvl);
-       zlog_file_set_other(&zt_stdout);
+       log_stdout_apply_level();
        return CMD_SUCCESS;
 }
 
@@ -378,9 +413,7 @@ void command_setup_early_logging(const char *dest, const char *level)
 
        if (strcmp(type, "stdout") == 0) {
                log_cmdline_stdout_lvl = nlevel;
-               zt_stdout.prio_min = ZLOG_MAXLVL(log_config_stdout_lvl,
-                                                log_cmdline_stdout_lvl);
-               zlog_file_set_other(&zt_stdout);
+               log_stdout_apply_level();
                return;
        }
        if (strcmp(type, "syslog") == 0) {
@@ -414,9 +447,7 @@ DEFUN (clear_log_cmdline,
                                             log_cmdline_syslog_lvl));
 
        log_cmdline_stdout_lvl = ZLOG_DISABLED;
-       zt_stdout.prio_min = ZLOG_MAXLVL(log_config_stdout_lvl,
-                                        log_cmdline_stdout_lvl);
-       zlog_file_set_other(&zt_stdout);
+       log_stdout_apply_level();
 
        return CMD_SUCCESS;
 }
@@ -524,8 +555,10 @@ DEFUN (config_log_record_priority,
 {
        zt_file.record_priority = true;
        zlog_file_set_other(&zt_file);
-       zt_stdout.record_priority = true;
-       zlog_file_set_other(&zt_stdout);
+       if (!stdout_journald_in_use) {
+               zt_stdout_file.record_priority = true;
+               zlog_file_set_other(&zt_stdout_file);
+       }
        zt_filterfile.parent.record_priority = true;
        zlog_file_set_other(&zt_filterfile.parent);
        return CMD_SUCCESS;
@@ -540,8 +573,10 @@ DEFUN (no_config_log_record_priority,
 {
        zt_file.record_priority = false;
        zlog_file_set_other(&zt_file);
-       zt_stdout.record_priority = false;
-       zlog_file_set_other(&zt_stdout);
+       if (!stdout_journald_in_use) {
+               zt_stdout_file.record_priority = false;
+               zlog_file_set_other(&zt_stdout_file);
+       }
        zt_filterfile.parent.record_priority = false;
        zlog_file_set_other(&zt_filterfile.parent);
        return CMD_SUCCESS;
@@ -557,8 +592,10 @@ DEFPY (config_log_timestamp_precision,
 {
        zt_file.ts_subsec = precision;
        zlog_file_set_other(&zt_file);
-       zt_stdout.ts_subsec = precision;
-       zlog_file_set_other(&zt_stdout);
+       if (!stdout_journald_in_use) {
+               zt_stdout_file.ts_subsec = precision;
+               zlog_file_set_other(&zt_stdout_file);
+       }
        zt_filterfile.parent.ts_subsec = precision;
        zlog_file_set_other(&zt_filterfile.parent);
        return CMD_SUCCESS;
@@ -575,8 +612,10 @@ DEFUN (no_config_log_timestamp_precision,
 {
        zt_file.ts_subsec = 0;
        zlog_file_set_other(&zt_file);
-       zt_stdout.ts_subsec = 0;
-       zlog_file_set_other(&zt_stdout);
+       if (!stdout_journald_in_use) {
+               zt_stdout_file.ts_subsec = 0;
+               zlog_file_set_other(&zt_stdout_file);
+       }
        zt_filterfile.parent.ts_subsec = 0;
        zlog_file_set_other(&zt_filterfile.parent);
        return CMD_SUCCESS;
@@ -822,7 +861,12 @@ static int log_vty_init(const char *progname, const char *protoname,
 
        zlog_filterfile_init(&zt_filterfile);
 
-       zlog_file_set_fd(&zt_stdout, STDOUT_FILENO);
+       if (sd_stdout_is_journal) {
+               stdout_journald_in_use = true;
+               zlog_5424_init(&zt_stdout_journald);
+               zlog_5424_apply_dst(&zt_stdout_journald);
+       } else
+               zlog_file_set_fd(&zt_stdout_file, STDOUT_FILENO);
        return 0;
 }
 
index 1933f4f6887dea14b8b074890869adb7493bf2a2..b9290930d727859a2fcb80ba5e62aa281899b332 100644 (file)
 extern "C" {
 #endif
 
+/* fd 1/2 connected to journald? */
+extern bool sd_stdout_is_journal;
+extern bool sd_stderr_is_journal;
+
 /*
  * Wrapper functions to systemd calls.
  *