From: Michael Tremer Date: Mon, 25 Jan 2021 13:45:25 +0000 (+0000) Subject: Add a basic logging infrastructure X-Git-Tag: 0.1.0~55 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f5f35f2b40dac089cac2891d8ba71d62316ab604;p=fireperf.git Add a basic logging infrastructure Signed-off-by: Michael Tremer --- diff --git a/Makefile.am b/Makefile.am index 8a5413d..a2bb795 100644 --- a/Makefile.am +++ b/Makefile.am @@ -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 index 0000000..476e63d --- /dev/null +++ b/src/logging.c @@ -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 . # +# # +#############################################################################*/ + +#include +#include + +#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 index 0000000..56c4f71 --- /dev/null +++ b/src/logging.h @@ -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 . # +# # +#############################################################################*/ + +#ifndef FIREPERF_LOGGING_H +#define FIREPERF_LOGGING_H + +#include + +#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 */ diff --git a/src/main.c b/src/main.c index 03ed2d1..aeb3a97 100644 --- a/src/main.c +++ b/src/main.c @@ -23,10 +23,12 @@ #include #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; } diff --git a/src/main.h b/src/main.h index 3b82365..9e0f629 100644 --- a/src/main.h +++ b/src/main.h @@ -18,9 +18,14 @@ # # #############################################################################*/ +#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 */