mirror of
https://github.com/xdp-project/bpf-examples.git
synced 2024-05-06 15:54:53 +00:00
lib/util: Import logging helpers from xdp-tools
Include logging.{h,c} from xdp-tools so utilities can use them for setting libxdp and libbpf logging. Signed-off-by: Toke Høiland-Jørgensen <toke@redhat.com>
This commit is contained in:
@@ -5,7 +5,7 @@ LIB_DIR = .
|
||||
LIB_INSTALL := $(LIB_DIR)/install
|
||||
include defines.mk
|
||||
|
||||
SUBDIRS=
|
||||
SUBDIRS=util
|
||||
|
||||
all: $(OBJECT_LIBBPF) $(OBJECT_LIBXDP)
|
||||
@set -e; \
|
||||
|
18
lib/util/Makefile
Normal file
18
lib/util/Makefile
Normal file
@@ -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)
|
92
lib/util/logging.c
Normal file
92
lib/util/logging.c
Normal file
@@ -0,0 +1,92 @@
|
||||
/* SPDX-License-Identifier: GPL-2.0 */
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
#include <bpf/libbpf.h>
|
||||
#include <xdp/libxdp.h>
|
||||
|
||||
#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;
|
||||
}
|
35
lib/util/logging.h
Normal file
35
lib/util/logging.h
Normal file
@@ -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
|
@@ -1,2 +1,2 @@
|
||||
# list of objects in this directory
|
||||
UTIL_OBJS := json_writer.o
|
||||
UTIL_OBJS := json_writer.o logging.o
|
||||
|
Reference in New Issue
Block a user