]> git.ipfire.org Git - people/ms/network.git/commitdiff
networkd: Log to journald
authorMichael Tremer <michael.tremer@ipfire.org>
Mon, 13 Feb 2023 16:00:43 +0000 (16:00 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Mon, 13 Feb 2023 16:00:43 +0000 (16:00 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
Makefile.am
src/networkd/link.c
src/networkd/logging.c [new file with mode: 0644]
src/networkd/logging.h

index 893f1b8394447cb3d3189ce417fe7720a81c1520..eded2924663f3ad5102b543c87ef57e9ab50e2bd 100644 (file)
@@ -323,6 +323,7 @@ dist_networkd_SOURCES = \
        src/networkd/link.h \
        src/networkd/links.c \
        src/networkd/links.h \
+       src/networkd/logging.c \
        src/networkd/logging.h \
        src/networkd/main.c \
        src/networkd/ports.c \
index 80478f4342a4e4c659ea71c0cba58d038c9a8587..7f49606a00550d1fa65a76f240d88c0930047485 100644 (file)
@@ -194,7 +194,7 @@ static int nw_link_update_flags(nw_link* link, sd_netlink_message* message) {
        // Fetch flags
        r = sd_rtnl_message_link_get_flags(message, &flags);
        if (r < 0) {
-               return DEBUG("Could not read link flags: %m\n");
+               DEBUG("Could not read link flags: %m\n");
                return 1;
        }
 
diff --git a/src/networkd/logging.c b/src/networkd/logging.c
new file mode 100644 (file)
index 0000000..c4809e8
--- /dev/null
@@ -0,0 +1,65 @@
+/*#############################################################################
+#                                                                             #
+# IPFire.org - A linux based firewall                                         #
+# Copyright (C) 2023 IPFire Network Development Team                          #
+#                                                                             #
+# This program is free software: you can redistribute it and/or modify        #
+# it under the terms of the GNU General Public License as published by        #
+# the Free Software Foundation, either version 3 of the License, or           #
+# (at your option) any later version.                                         #
+#                                                                             #
+# This program is distributed in the hope that it will be useful,             #
+# but WITHOUT ANY WARRANTY; without even the implied warranty of              #
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               #
+# GNU General Public License for more details.                                #
+#                                                                             #
+# You should have received a copy of the GNU General Public License           #
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+#############################################################################*/
+
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#include <systemd/sd-journal.h>
+
+#include "logging.h"
+
+void nw_log(int priority, const char* file,
+               int line, const char* fn, const char* format, ...) {
+       char* buffer = NULL;
+       va_list args;
+       int r;
+
+       // Format log message
+       va_start(args, format);
+       r = vasprintf(&buffer, format, args);
+       va_end(args);
+       if (r < 0)
+               return;
+
+       // Send message to journald
+       r = sd_journal_send(
+               "MESSAGE=%s", buffer,
+               "PRIORITY=%d", priority,
+
+               // Syslog compat
+               "SYSLOG_IDENTIFIER=networkd",
+
+               // Debugging stuff
+               "ERRNO=%d", errno,
+               "CODE_FILE=%s", file,
+               "CODE_LINE=%d", line,
+               "CODE_FUNC=%s", fn,
+
+               NULL
+       );
+
+       // Fall back to standard output
+       if (r)
+               sd_journal_perror(buffer);
+
+       // Cleanup
+       free(buffer);
+}
index 9d51f212aaa722c7e9e491828d028f6a0d86d89b..ea75ba020f835badd50bc81e0b7efe261511afd7 100644 (file)
 #ifndef NETWORKD_LOGGING_H
 #define NETWORKD_LOGGING_H
 
-#include <stdio.h>
+#include <syslog.h>
+
+void nw_log(int priority, const char *file, int line, const char* fn,
+       const char *format, ...) __attribute__((format(printf, 5, 6)));
 
 /*
        This is just something simple which will work for now...
 */
-#define ERROR(...) fprintf(stderr, __VA_ARGS__)
-#define DEBUG(...) printf(__VA_ARGS__)
+#define INFO(args...)  nw_log(LOG_INFO, __FILE__, __LINE__, __FUNCTION__, ## args)
+#define ERROR(args...) nw_log(LOG_ERR, __FILE__, __LINE__, __FUNCTION__, ## args)
+#define DEBUG(args...) nw_log(LOG_DEBUG, __FILE__, __LINE__, __FUNCTION__, ## args)
 
 #endif /* NETWORKD_LOGGING_H */