106f6ba60Sopenharmony_ci/*
206f6ba60Sopenharmony_ci* Copyright (c) 2024 Huawei Device Co., Ltd. All rights reserved.
306f6ba60Sopenharmony_ci
406f6ba60Sopenharmony_ci* The bpf_log_writer.h is dual licensed: you can use it either under the terms of
506f6ba60Sopenharmony_ci* the GPL V2, or the 3-Clause BSD license, at your option.
606f6ba60Sopenharmony_ci* See the LICENSE file in the root of this repository for complete details.
706f6ba60Sopenharmony_ci*/
806f6ba60Sopenharmony_ci#ifndef BPF_LOG_WRITER_H
906f6ba60Sopenharmony_ci#define BPF_LOG_WRITER_H
1006f6ba60Sopenharmony_ci
1106f6ba60Sopenharmony_ci#include "bpf_log.h"
1206f6ba60Sopenharmony_ci#include "fstrace_types.h"
1306f6ba60Sopenharmony_ci
1406f6ba60Sopenharmony_ci#define BPFLOG(level, expression, format, ...) {                                          \
1506f6ba60Sopenharmony_ci    u32 bpf_log_level_index = BPF_LOG_LEVEL_INDEX;                                          \
1606f6ba60Sopenharmony_ci    u32* bpf_log_level_ptr = bpf_map_lookup_elem(&config_var_map, &bpf_log_level_index);    \
1706f6ba60Sopenharmony_ci    if (bpf_log_level_ptr) {                                                                \
1806f6ba60Sopenharmony_ci        u32 bpf_log_level = BPF_LOG_NONE;                                                   \
1906f6ba60Sopenharmony_ci        bpf_probe_read_kernel(&bpf_log_level, sizeof(u32), bpf_log_level_ptr);       \
2006f6ba60Sopenharmony_ci        if ((expression) && (bpf_log_level <= level)) {                                     \
2106f6ba60Sopenharmony_ci            char mesg[] = format;                                                           \
2206f6ba60Sopenharmony_ci            bpf_trace_printk(mesg, sizeof(mesg), ##__VA_ARGS__);                            \
2306f6ba60Sopenharmony_ci        }                                                                                   \
2406f6ba60Sopenharmony_ci    } else {                                                                                \
2506f6ba60Sopenharmony_ci        char mesg[] = "failed to get bpf log level";                                        \
2606f6ba60Sopenharmony_ci        bpf_trace_printk(mesg, sizeof(mesg));                                               \
2706f6ba60Sopenharmony_ci    }                                                                                       \
2806f6ba60Sopenharmony_ci}
2906f6ba60Sopenharmony_ci
3006f6ba60Sopenharmony_ci#ifdef BPF_LOGGER_DEBUG
3106f6ba60Sopenharmony_ci#define BPFLOGD(expression, format, ...) BPFLOG(BPF_LOG_DEBUG, expression, format, ##__VA_ARGS__)
3206f6ba60Sopenharmony_ci#define BPFLOGI(expression, format, ...) BPFLOG(BPF_LOG_INFO, expression, format, ##__VA_ARGS__)
3306f6ba60Sopenharmony_ci#define BPFLOGW(expression, format, ...) BPFLOG(BPF_LOG_WARN, expression, format, ##__VA_ARGS__)
3406f6ba60Sopenharmony_ci#define BPFLOGE(expression, format, ...) BPFLOG(BPF_LOG_ERROR, expression, format, ##__VA_ARGS__)
3506f6ba60Sopenharmony_ci#define BPFLOGF(expression, format, ...) BPFLOG(BPF_LOG_FATAL, expression, format, ##__VA_ARGS__)
3606f6ba60Sopenharmony_ci
3706f6ba60Sopenharmony_ci#elif defined(BPF_LOGGER_INFO)
3806f6ba60Sopenharmony_ci#define BPFLOGD(expression, format, ...) {}
3906f6ba60Sopenharmony_ci#define BPFLOGI(expression, format, ...) BPFLOG(BPF_LOG_INFO, expression, format, ##__VA_ARGS__)
4006f6ba60Sopenharmony_ci#define BPFLOGW(expression, format, ...) BPFLOG(BPF_LOG_WARN, expression, format, ##__VA_ARGS__)
4106f6ba60Sopenharmony_ci#define BPFLOGE(expression, format, ...) BPFLOG(BPF_LOG_ERROR, expression, format, ##__VA_ARGS__)
4206f6ba60Sopenharmony_ci#define BPFLOGF(expression, format, ...) BPFLOG(BPF_LOG_FATAL, expression, format, ##__VA_ARGS__)
4306f6ba60Sopenharmony_ci
4406f6ba60Sopenharmony_ci#elif defined(BPF_LOGGER_WARN)
4506f6ba60Sopenharmony_ci#define BPFLOGD(expression, format, ...) {}
4606f6ba60Sopenharmony_ci#define BPFLOGI(expression, format, ...) {}
4706f6ba60Sopenharmony_ci#define BPFLOGW(expression, format, ...) BPFLOG(BPF_LOG_WARN, expression, format, ##__VA_ARGS__)
4806f6ba60Sopenharmony_ci#define BPFLOGE(expression, format, ...) BPFLOG(BPF_LOG_ERROR, expression, format, ##__VA_ARGS__)
4906f6ba60Sopenharmony_ci#define BPFLOGF(expression, format, ...) BPFLOG(BPF_LOG_FATAL, expression, format, ##__VA_ARGS__)
5006f6ba60Sopenharmony_ci
5106f6ba60Sopenharmony_ci#elif defined(BPF_LOGGER_ERROR)
5206f6ba60Sopenharmony_ci#define BPFLOGD(expression, format, ...) {}
5306f6ba60Sopenharmony_ci#define BPFLOGI(expression, format, ...) {}
5406f6ba60Sopenharmony_ci#define BPFLOGW(expression, format, ...) {}
5506f6ba60Sopenharmony_ci#define BPFLOGE(expression, format, ...) BPFLOG(BPF_LOG_ERROR, expression, format, ##__VA_ARGS__)
5606f6ba60Sopenharmony_ci#define BPFLOGF(expression, format, ...) BPFLOG(BPF_LOG_FATAL, expression, format, ##__VA_ARGS__)
5706f6ba60Sopenharmony_ci
5806f6ba60Sopenharmony_ci#elif defined(BPF_LOGGER_FATAL)
5906f6ba60Sopenharmony_ci#define BPFLOGD(expression, format, ...) {}
6006f6ba60Sopenharmony_ci#define BPFLOGI(expression, format, ...) {}
6106f6ba60Sopenharmony_ci#define BPFLOGW(expression, format, ...) {}
6206f6ba60Sopenharmony_ci#define BPFLOGE(expression, format, ...) {}
6306f6ba60Sopenharmony_ci#define BPFLOGF(expression, format, ...) BPFLOG(BPF_LOG_FATAL, expression, format, ##__VA_ARGS__)
6406f6ba60Sopenharmony_ci
6506f6ba60Sopenharmony_ci#else
6606f6ba60Sopenharmony_ci#define BPFLOGD(expression, format, ...) {}
6706f6ba60Sopenharmony_ci#define BPFLOGI(expression, format, ...) {}
6806f6ba60Sopenharmony_ci#define BPFLOGW(expression, format, ...) {}
6906f6ba60Sopenharmony_ci#define BPFLOGE(expression, format, ...) {}
7006f6ba60Sopenharmony_ci#define BPFLOGF(expression, format, ...) {}
7106f6ba60Sopenharmony_ci
7206f6ba60Sopenharmony_ci#endif // BPF_LOGGER_LEVEL
7306f6ba60Sopenharmony_ci
7406f6ba60Sopenharmony_ci#endif  // BPF_LOG_WRITER_H