• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) Huawei Technologies Co., Ltd. 2022. All rights reserved.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef BPF_LOG_WRITER_H
17 #define BPF_LOG_WRITER_H
18 
19 #include "bpf_log.h"
20 #include "fstrace_types.h"
21 
22 #define BPFLOG(level, expression, format, ...) {                                          \
23     u32 bpf_log_level_index = BPF_LOG_LEVEL_INDEX;                                          \
24     u32* bpf_log_level_ptr = bpf_map_lookup_elem(&config_var_map, &bpf_log_level_index);    \
25     if (bpf_log_level_ptr) {                                                                \
26         u32 bpf_log_level = BPF_LOG_NONE;                                                   \
27         bpf_probe_read_kernel(&bpf_log_level, sizeof(u32), bpf_log_level_ptr);       \
28         if ((expression) && (bpf_log_level <= level)) {                                     \
29             char mesg[] = format;                                                           \
30             bpf_trace_printk(mesg, sizeof(mesg), ##__VA_ARGS__);                            \
31         }                                                                                   \
32     } else {                                                                                \
33         char mesg[] = "failed to get bpf log level";                                        \
34         bpf_trace_printk(mesg, sizeof(mesg));                                               \
35     }                                                                                       \
36 }
37 
38 #ifdef BPF_LOGGER_DEBUG
39 #define BPFLOGD(expression, format, ...) BPFLOG(BPF_LOG_DEBUG, expression, format, ##__VA_ARGS__)
40 #define BPFLOGI(expression, format, ...) BPFLOG(BPF_LOG_INFO, expression, format, ##__VA_ARGS__)
41 #define BPFLOGW(expression, format, ...) BPFLOG(BPF_LOG_WARN, expression, format, ##__VA_ARGS__)
42 #define BPFLOGE(expression, format, ...) BPFLOG(BPF_LOG_ERROR, expression, format, ##__VA_ARGS__)
43 #define BPFLOGF(expression, format, ...) BPFLOG(BPF_LOG_FATAL, expression, format, ##__VA_ARGS__)
44 
45 #elif defined(BPF_LOGGER_INFO)
46 #define BPFLOGD(expression, format, ...) {}
47 #define BPFLOGI(expression, format, ...) BPFLOG(BPF_LOG_INFO, expression, format, ##__VA_ARGS__)
48 #define BPFLOGW(expression, format, ...) BPFLOG(BPF_LOG_WARN, expression, format, ##__VA_ARGS__)
49 #define BPFLOGE(expression, format, ...) BPFLOG(BPF_LOG_ERROR, expression, format, ##__VA_ARGS__)
50 #define BPFLOGF(expression, format, ...) BPFLOG(BPF_LOG_FATAL, expression, format, ##__VA_ARGS__)
51 
52 #elif defined(BPF_LOGGER_WARN)
53 #define BPFLOGD(expression, format, ...) {}
54 #define BPFLOGI(expression, format, ...) {}
55 #define BPFLOGW(expression, format, ...) BPFLOG(BPF_LOG_WARN, expression, format, ##__VA_ARGS__)
56 #define BPFLOGE(expression, format, ...) BPFLOG(BPF_LOG_ERROR, expression, format, ##__VA_ARGS__)
57 #define BPFLOGF(expression, format, ...) BPFLOG(BPF_LOG_FATAL, expression, format, ##__VA_ARGS__)
58 
59 #elif defined(BPF_LOGGER_ERROR)
60 #define BPFLOGD(expression, format, ...) {}
61 #define BPFLOGI(expression, format, ...) {}
62 #define BPFLOGW(expression, format, ...) {}
63 #define BPFLOGE(expression, format, ...) BPFLOG(BPF_LOG_ERROR, expression, format, ##__VA_ARGS__)
64 #define BPFLOGF(expression, format, ...) BPFLOG(BPF_LOG_FATAL, expression, format, ##__VA_ARGS__)
65 
66 #elif defined(BPF_LOGGER_FATAL)
67 #define BPFLOGD(expression, format, ...) {}
68 #define BPFLOGI(expression, format, ...) {}
69 #define BPFLOGW(expression, format, ...) {}
70 #define BPFLOGE(expression, format, ...) {}
71 #define BPFLOGF(expression, format, ...) BPFLOG(BPF_LOG_FATAL, expression, format, ##__VA_ARGS__)
72 
73 #else
74 #define BPFLOGD(expression, format, ...) {}
75 #define BPFLOGI(expression, format, ...) {}
76 #define BPFLOGW(expression, format, ...) {}
77 #define BPFLOGE(expression, format, ...) {}
78 #define BPFLOGF(expression, format, ...) {}
79 
80 #endif // BPF_LOGGER_LEVEL
81 
82 #endif  // BPF_LOG_WRITER_H