Skip to content

Commit f27c7c2

Browse files
committed
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>
1 parent 4d74e4a commit f27c7c2

File tree

5 files changed

+147
-2
lines changed

5 files changed

+147
-2
lines changed

lib/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ LIB_DIR = .
55
LIB_INSTALL := $(LIB_DIR)/install
66
include defines.mk
77

8-
SUBDIRS=
8+
SUBDIRS=util
99

1010
all: $(OBJECT_LIBBPF) $(OBJECT_LIBXDP)
1111
@set -e; \

lib/util/Makefile

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
include util.mk
2+
3+
LIB_DIR ?= ..
4+
5+
include $(LIB_DIR)/defines.mk
6+
7+
all: $(UTIL_OBJS)
8+
9+
# Create expansions for dependencies
10+
UTIL_H := ${UTIL_OBJS:.o=.h}
11+
12+
CFLAGS+= -I$(LIB_DIR)/install/include
13+
14+
$(UTIL_OBJS): %.o: %.c $(UTIL_H) $(LIBMK)
15+
$(QUIET_CC)$(CC) $(CFLAGS) -Wall -I../../headers -c -o $@ $<
16+
17+
clean:
18+
$(Q)rm -f $(UTIL_OBJS)

lib/util/logging.c

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
3+
#include <stdio.h>
4+
#include <stdarg.h>
5+
6+
#include <bpf/libbpf.h>
7+
#include <xdp/libxdp.h>
8+
9+
#define __unused __attribute__((unused))
10+
#include "logging.h"
11+
12+
static enum logging_print_level log_level = LOG_INFO;
13+
14+
static int print_func(enum logging_print_level level, const char *format,
15+
va_list args)
16+
{
17+
if (level > log_level)
18+
return 0;
19+
20+
return vfprintf(stderr, format, args);
21+
}
22+
23+
static int libbpf_print_func(enum libbpf_print_level level, const char *format,
24+
va_list args)
25+
{
26+
return print_func(level + 1, format, args);
27+
}
28+
29+
static int libbpf_silent_func(__unused enum libbpf_print_level level,
30+
__unused const char *format,
31+
__unused va_list args)
32+
{
33+
return 0;
34+
}
35+
36+
static int libxdp_print_func(enum libxdp_print_level level, const char *format,
37+
va_list args)
38+
{
39+
return print_func(level + 1, format, args);
40+
}
41+
42+
static int libxdp_silent_func(__unused enum libxdp_print_level level,
43+
__unused const char *format,
44+
__unused va_list args)
45+
{
46+
return 0;
47+
}
48+
49+
#define __printf(a, b) __attribute__((format(printf, a, b)))
50+
51+
__printf(2, 3) void logging_print(enum logging_print_level level,
52+
const char *format, ...)
53+
{
54+
va_list args;
55+
56+
va_start(args, format);
57+
print_func(level, format, args);
58+
va_end(args);
59+
}
60+
61+
void init_lib_logging(void)
62+
{
63+
libbpf_set_print(libbpf_print_func);
64+
libxdp_set_print(libxdp_print_func);
65+
}
66+
67+
void silence_libbpf_logging(void)
68+
{
69+
if (log_level < LOG_VERBOSE)
70+
libbpf_set_print(libbpf_silent_func);
71+
}
72+
73+
void silence_libxdp_logging(void)
74+
{
75+
if (log_level < LOG_VERBOSE)
76+
libxdp_set_print(libxdp_silent_func);
77+
}
78+
79+
enum logging_print_level set_log_level(enum logging_print_level level)
80+
{
81+
enum logging_print_level old_level = log_level;
82+
83+
log_level = level;
84+
return old_level;
85+
}
86+
87+
enum logging_print_level increase_log_level(void)
88+
{
89+
if (log_level < LOG_VERBOSE)
90+
log_level++;
91+
return log_level;
92+
}

lib/util/logging.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
3+
#ifndef __LOGGING_H
4+
#define __LOGGING_H
5+
6+
/* This matches the libbpf logging levels, but with an additional VERBOSE level;
7+
* we demote all libbpf messages by one level so debug messages only show up on
8+
* VERBOSE.
9+
*/
10+
enum logging_print_level {
11+
LOG_WARN,
12+
LOG_INFO,
13+
LOG_DEBUG,
14+
LOG_VERBOSE,
15+
};
16+
17+
extern void logging_print(enum logging_print_level level, const char *format,
18+
...) __attribute__((format(printf, 2, 3)));
19+
20+
#define __pr(level, fmt, ...) \
21+
do { \
22+
logging_print(level, fmt, ##__VA_ARGS__); \
23+
} while (0)
24+
25+
#define pr_warn(fmt, ...) __pr(LOG_WARN, fmt, ##__VA_ARGS__)
26+
#define pr_info(fmt, ...) __pr(LOG_INFO, fmt, ##__VA_ARGS__)
27+
#define pr_debug(fmt, ...) __pr(LOG_DEBUG, fmt, ##__VA_ARGS__)
28+
29+
void init_lib_logging(void);
30+
void silence_libbpf_logging(void);
31+
void silence_libxdp_logging(void);
32+
enum logging_print_level set_log_level(enum logging_print_level level);
33+
enum logging_print_level increase_log_level();
34+
35+
#endif

lib/util/util.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
# list of objects in this directory
2-
UTIL_OBJS := json_writer.o
2+
UTIL_OBJS := json_writer.o logging.o

0 commit comments

Comments
 (0)