fireperf
fireperf_SOURCES = \
- src/main.h \
- src/main.c
+ src/logging.c \
+ src/logging.h \
+ src/main.c \
+ src/main.h
# ------------------------------------------------------------------------------
--- /dev/null
+/*#############################################################################
+# #
+# 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);
+}
--- /dev/null
+/*#############################################################################
+# #
+# 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 */
#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},
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)
conf->mode = FIREPERF_MODE_CLIENT;
break;
+ case 'd':
+ conf->loglevel = LOG_DEBUG;
+ break;
+
case 'p':
conf->port = atoi(optarg);
int main(int argc, char* argv[]) {
struct fireperf_config conf = {
+ .loglevel = DEFAULT_LOG_LEVEL,
.port = DEFAULT_PORT,
};
int r;
return 2;
}
+ // Dump configuration
+ DEBUG(&conf, "Configuration:\n");
+ DEBUG(&conf, " Port = %d\n", conf.port);
+
return 0;
}
# #
#############################################################################*/
+#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,
} mode;
int port;
};
+
+#endif /* FIREPERF_MAIN_H */