-
Notifications
You must be signed in to change notification settings - Fork 151
/
logging.c
96 lines (76 loc) · 1.92 KB
/
logging.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/* SPDX-License-Identifier: GPL-2.0 */
#include <stdio.h>
#include <stdarg.h>
#include <bpf/libbpf.h>
#include <xdp/libxdp.h>
#include "logging.h"
#include "util.h"
static enum logging_print_level log_level = LOG_INFO;
static int print_func(enum logging_print_level level, int indent,
const char *format, va_list args)
{
int i;
if (level > log_level)
return 0;
for (i = 0; i < indent; i++)
fprintf(stderr, " ");
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, 2, 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, 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, 0, 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;
}