]> git.ipfire.org Git - fireperf.git/commitdiff
Add a basic logging infrastructure
authorMichael Tremer <michael.tremer@ipfire.org>
Mon, 25 Jan 2021 13:45:25 +0000 (13:45 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Mon, 25 Jan 2021 13:45:48 +0000 (13:45 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
Makefile.am
src/logging.c [new file with mode: 0644]
src/logging.h [new file with mode: 0644]
src/main.c
src/main.h

index 8a5413db2982bd937c1b15dc4b1ced7f7d65d1bf..a2bb795c567eca2b84715d8d34b4bb7ca52a6b67 100644 (file)
@@ -60,8 +60,10 @@ bin_PROGRAMS = \
        fireperf
 
 fireperf_SOURCES = \
-       src/main.h \
-       src/main.c
+       src/logging.c \
+       src/logging.h \
+       src/main.c \
+       src/main.h
 
 # ------------------------------------------------------------------------------
 
diff --git a/src/logging.c b/src/logging.c
new file mode 100644 (file)
index 0000000..476e63d
--- /dev/null
@@ -0,0 +1,44 @@
+/*#############################################################################
+#                                                                             #
+# fireperf - A network benchmarking tool                                      #
+# Copyright (C) 2021 IPFire 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 <stdarg.h>
+#include <syslog.h>
+
+#include "logging.h"
+#include "main.h"
+
+int fireperf_get_log_level(struct fireperf_config* conf) {
+       return conf->loglevel;
+}
+
+static void fireperf_log_syslog(struct fireperf_config* conf, int priority,
+               const char* file, int line, const char* fn, const char* format, va_list args) {
+       openlog(PACKAGE_NAME, LOG_PID, LOG_DAEMON);
+       vsyslog(priority | LOG_DAEMON, format, args);
+}
+
+void fireperf_log(struct fireperf_config* conf, int priority,
+               const char* file, int line, const char* fn, const char* format, ...) {
+       va_list args;
+
+       va_start(args, format);
+       fireperf_log_syslog(conf, priority, file, line, fn, format, args);
+       va_end(args);
+}
diff --git a/src/logging.h b/src/logging.h
new file mode 100644 (file)
index 0000000..56c4f71
--- /dev/null
@@ -0,0 +1,53 @@
+/*#############################################################################
+#                                                                             #
+# fireperf - A network benchmarking tool                                      #
+# Copyright (C) 2021 IPFire 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/>.       #
+#                                                                             #
+#############################################################################*/
+
+#ifndef FIREPERF_LOGGING_H
+#define FIREPERF_LOGGING_H
+
+#include <syslog.h>
+
+#include "main.h"
+
+static inline void __attribute__((always_inline, format(printf, 2, 3)))
+       fireperf_log_null(struct fireperf_config* conf, const char* format, ...) {}
+
+#define fireperf_log_cond(conf, prio, arg...) \
+       do { \
+               if (fireperf_get_log_level(conf) >= prio) \
+                       fireperf_log(conf, prio, __FILE__, __LINE__, __FUNCTION__, ## arg); \
+       } while (0)
+
+
+#ifdef ENABLE_DEBUG
+#  define DEBUG(conf, arg...) fireperf_log_cond(conf, LOG_DEBUG, ## arg)
+#else
+#  define DEBUG(conf, arg...) fireperf_log_null(conf, ## arg)
+#endif
+
+#define INFO(conf, arg...) fireperf_log_cond(conf, LOG_INFO, ## arg)
+#define ERROR(conf, arg...) fireperf_log_cond(conf, LOG_ERR, ## arg)
+
+int fireperf_get_log_level(struct fireperf_config* conf);
+
+void fireperf_log(struct fireperf_config* config,
+       int priority, const char* file, int line, const char* fn,
+       const char* format, ...) __attribute__((format(printf, 6, 7)));
+
+#endif /* FIREPERF_LOGGING_H */
index 03ed2d1833bc57dd22d310e2bcb11532e60261f4..aeb3a970efa73f797c40c38f5ab99c557ba2bc60 100644 (file)
 #include <stdlib.h>
 
 #include "main.h"
+#include "logging.h"
 
 static int parse_argv(int argc, char* argv[], struct fireperf_config* conf) {
        static struct option long_options[] = {
                {"client",     required_argument, 0, 'c'},
+               {"debug",      no_argument,       0, 'd'},
                {"port",       required_argument, 0, 'p'},
                {"server",     no_argument,       0, 's'},
                {0, 0, 0, 0},
@@ -36,7 +38,7 @@ static int parse_argv(int argc, char* argv[], struct fireperf_config* conf) {
        int done = 0;
 
        while (!done) {
-               int c = getopt_long(argc, argv, "c:p:s", long_options, &option_index);
+               int c = getopt_long(argc, argv, "c:dp:s", long_options, &option_index);
 
                // End
                if (c == -1)
@@ -63,6 +65,10 @@ static int parse_argv(int argc, char* argv[], struct fireperf_config* conf) {
                                conf->mode = FIREPERF_MODE_CLIENT;
                                break;
 
+                       case 'd':
+                               conf->loglevel = LOG_DEBUG;
+                               break;
+
                        case 'p':
                                conf->port = atoi(optarg);
 
@@ -88,6 +94,7 @@ static int parse_argv(int argc, char* argv[], struct fireperf_config* conf) {
 
 int main(int argc, char* argv[]) {
        struct fireperf_config conf = {
+               .loglevel = DEFAULT_LOG_LEVEL,
                .port = DEFAULT_PORT,
        };
        int r;
@@ -103,5 +110,9 @@ int main(int argc, char* argv[]) {
                return 2;
        }
 
+       // Dump configuration
+       DEBUG(&conf, "Configuration:\n");
+       DEBUG(&conf, "  Port = %d\n", conf.port);
+
        return 0;
 }
index 3b823652e06b04ed8df2609499a6e5eb818b4d69..9e0f6296bb0d7242b24c197fbf50d1b68df00188 100644 (file)
 #                                                                             #
 #############################################################################*/
 
+#ifndef FIREPERF_MAIN_H
+#define FIREPERF_MAIN_H
+
+#define DEFAULT_LOG_LEVEL LOG_INFO
 #define DEFAULT_PORT 5001
 
 struct fireperf_config {
+       int loglevel;
        enum {
                FIREPERF_MODE_NONE = 0,
                FIREPERF_MODE_CLIENT,
@@ -28,3 +33,5 @@ struct fireperf_config {
        } mode;
        int port;
 };
+
+#endif /* FIREPERF_MAIN_H */