From: Michael Tremer Date: Mon, 13 Feb 2023 16:00:43 +0000 (+0000) Subject: networkd: Log to journald X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=1a70a6864ca83ed91c9acfd6be5a64185ac34917;p=network.git networkd: Log to journald Signed-off-by: Michael Tremer --- diff --git a/Makefile.am b/Makefile.am index 893f1b83..eded2924 100644 --- a/Makefile.am +++ b/Makefile.am @@ -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 \ diff --git a/src/networkd/link.c b/src/networkd/link.c index 80478f43..7f49606a 100644 --- a/src/networkd/link.c +++ b/src/networkd/link.c @@ -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 index 00000000..c4809e84 --- /dev/null +++ b/src/networkd/logging.c @@ -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 . # +# # +#############################################################################*/ + +#include +#include +#include + +#include + +#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); +} diff --git a/src/networkd/logging.h b/src/networkd/logging.h index 9d51f212..ea75ba02 100644 --- a/src/networkd/logging.h +++ b/src/networkd/logging.h @@ -21,12 +21,16 @@ #ifndef NETWORKD_LOGGING_H #define NETWORKD_LOGGING_H -#include +#include + +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 */