diff --git a/lib/Makefile b/lib/Makefile index a5c562d..7c32959 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -5,7 +5,7 @@ LIB_DIR = . LIB_INSTALL := $(LIB_DIR)/install include defines.mk -SUBDIRS= +SUBDIRS=util all: $(OBJECT_LIBBPF) $(OBJECT_LIBXDP) @set -e; \ diff --git a/lib/util/Makefile b/lib/util/Makefile new file mode 100644 index 0000000..6df9a73 --- /dev/null +++ b/lib/util/Makefile @@ -0,0 +1,18 @@ +include util.mk + +LIB_DIR ?= .. + +include $(LIB_DIR)/defines.mk + +all: $(UTIL_OBJS) + +# Create expansions for dependencies +UTIL_H := ${UTIL_OBJS:.o=.h} + +CFLAGS+= -I$(LIB_DIR)/install/include + +$(UTIL_OBJS): %.o: %.c $(UTIL_H) $(LIBMK) + $(QUIET_CC)$(CC) $(CFLAGS) -Wall -I../../headers -c -o $@ $< + +clean: + $(Q)rm -f $(UTIL_OBJS) diff --git a/lib/util/logging.c b/lib/util/logging.c new file mode 100644 index 0000000..149386e --- /dev/null +++ b/lib/util/logging.c @@ -0,0 +1,92 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +#include +#include + +#include +#include + +#define __unused __attribute__((unused)) +#include "logging.h" + +static enum logging_print_level log_level = LOG_INFO; + +static int print_func(enum logging_print_level level, const char *format, + va_list args) +{ + if (level > log_level) + return 0; + + return vfprintf(stderr, format, args); +} + +static int libbpf_print_func(enum libbpf_print_level level, const char *format, + va_list args) +{ + return print_func(level + 1, format, args); +} + +static int libbpf_silent_func(__unused enum libbpf_print_level level, + __unused const char *format, + __unused va_list args) +{ + return 0; +} + +static int libxdp_print_func(enum libxdp_print_level level, const char *format, + va_list args) +{ + return print_func(level + 1, format, args); +} + +static int libxdp_silent_func(__unused enum libxdp_print_level level, + __unused const char *format, + __unused va_list args) +{ + return 0; +} + +#define __printf(a, b) __attribute__((format(printf, a, b))) + +__printf(2, 3) void logging_print(enum logging_print_level level, + const char *format, ...) +{ + va_list args; + + va_start(args, format); + print_func(level, format, args); + va_end(args); +} + +void init_lib_logging(void) +{ + libbpf_set_print(libbpf_print_func); + libxdp_set_print(libxdp_print_func); +} + +void silence_libbpf_logging(void) +{ + if (log_level < LOG_VERBOSE) + libbpf_set_print(libbpf_silent_func); +} + +void silence_libxdp_logging(void) +{ + if (log_level < LOG_VERBOSE) + libxdp_set_print(libxdp_silent_func); +} + +enum logging_print_level set_log_level(enum logging_print_level level) +{ + enum logging_print_level old_level = log_level; + + log_level = level; + return old_level; +} + +enum logging_print_level increase_log_level(void) +{ + if (log_level < LOG_VERBOSE) + log_level++; + return log_level; +} diff --git a/lib/util/logging.h b/lib/util/logging.h new file mode 100644 index 0000000..16c4e74 --- /dev/null +++ b/lib/util/logging.h @@ -0,0 +1,35 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +#ifndef __LOGGING_H +#define __LOGGING_H + +/* This matches the libbpf logging levels, but with an additional VERBOSE level; + * we demote all libbpf messages by one level so debug messages only show up on + * VERBOSE. + */ +enum logging_print_level { + LOG_WARN, + LOG_INFO, + LOG_DEBUG, + LOG_VERBOSE, +}; + +extern void logging_print(enum logging_print_level level, const char *format, + ...) __attribute__((format(printf, 2, 3))); + +#define __pr(level, fmt, ...) \ + do { \ + logging_print(level, fmt, ##__VA_ARGS__); \ + } while (0) + +#define pr_warn(fmt, ...) __pr(LOG_WARN, fmt, ##__VA_ARGS__) +#define pr_info(fmt, ...) __pr(LOG_INFO, fmt, ##__VA_ARGS__) +#define pr_debug(fmt, ...) __pr(LOG_DEBUG, fmt, ##__VA_ARGS__) + +void init_lib_logging(void); +void silence_libbpf_logging(void); +void silence_libxdp_logging(void); +enum logging_print_level set_log_level(enum logging_print_level level); +enum logging_print_level increase_log_level(); + +#endif diff --git a/lib/util/util.mk b/lib/util/util.mk index b7f160a..ab443bb 100644 --- a/lib/util/util.mk +++ b/lib/util/util.mk @@ -1,2 +1,2 @@ # list of objects in this directory -UTIL_OBJS := json_writer.o +UTIL_OBJS := json_writer.o logging.o