|
6 | 6 |
|
7 | 7 | #pragma once |
8 | 8 |
|
| 9 | +typedef enum { |
| 10 | + STUB_LOG_LEVEL_NONE = 0, |
| 11 | + STUB_LOG_LEVEL_E = 1, |
| 12 | + STUB_LOG_LEVEL_W = 2, |
| 13 | + STUB_LOG_LEVEL_I = 3, |
| 14 | + STUB_LOG_LEVEL_D = 4, |
| 15 | + STUB_LOG_LEVEL_T = 5, |
| 16 | + STUB_LOG_LEVEL_TRACE = STUB_LOG_LEVEL_T, |
| 17 | + STUB_LOG_LEVEL_V = 6, |
| 18 | +} stub_lib_log_level_t; |
| 19 | + |
| 20 | +#if !defined(STUB_LIB_LOG_LEVEL) |
| 21 | +#define STUB_LIB_LOG_LEVEL STUB_LOG_LEVEL_I |
| 22 | +#endif |
| 23 | + |
9 | 24 | #if defined(STUB_LOG_ENABLED) |
10 | 25 |
|
11 | 26 | #ifdef __cplusplus |
12 | 27 | extern "C" { |
13 | 28 | #endif // __cplusplus |
14 | 29 |
|
15 | | -void stub_lib_log_init(); |
| 30 | +void stub_lib_log_init(stub_lib_log_level_t level); |
| 31 | +void stub_lib_log_level(stub_lib_log_level_t level); |
| 32 | +int stub_lib_log_level_enabled(stub_lib_log_level_t level); |
16 | 33 | void stub_lib_log_printf(const char *fmt, ...); |
17 | 34 |
|
18 | 35 | #ifdef __cplusplus |
19 | 36 | } |
20 | 37 | #endif // __cplusplus |
21 | 38 |
|
22 | | -#define STUB_LOG_INIT() stub_lib_log_init() |
23 | | -#define STUB_LOG(fmt, ...) stub_lib_log_printf(fmt, ##__VA_ARGS__) |
| 39 | +#define STUB_LOG_INIT(level) stub_lib_log_init(level) |
| 40 | +#define STUB_LOG(fmt, ...) stub_lib_log_printf(fmt, ##__VA_ARGS__) |
24 | 41 |
|
25 | 42 | #else // defined(STUB_LOG_ENABLED) |
26 | 43 |
|
27 | | -#define STUB_LOG_INIT() \ |
| 44 | +#define STUB_LOG_INIT(level) \ |
28 | 45 | do { \ |
| 46 | + (void)(level); \ |
29 | 47 | } while (0) |
30 | 48 | #define STUB_LOG(fmt, ...) \ |
31 | 49 | do { \ |
32 | 50 | } while (0) |
33 | 51 |
|
34 | 52 | #endif // defined(STUB_LOG_ENABLED) |
35 | 53 |
|
36 | | -#define STUB_LOGE(fmt, ...) STUB_LOG("STUB_E: " fmt, ##__VA_ARGS__) |
37 | | -#define STUB_LOGW(fmt, ...) STUB_LOG("STUB_W: " fmt, ##__VA_ARGS__) |
38 | | -#define STUB_LOGI(fmt, ...) STUB_LOG("STUB_I: " fmt, ##__VA_ARGS__) |
39 | | -#define STUB_LOGD(fmt, ...) STUB_LOG("STUB_D: " fmt, ##__VA_ARGS__) |
40 | | -#define STUB_LOGV(fmt, ...) STUB_LOG("STUB_V: " fmt, ##__VA_ARGS__) |
| 54 | +#if defined(STUB_LOG_ENABLED) |
| 55 | +#define STUB_LOG_AT_LEVEL(level, fmt, ...) \ |
| 56 | + do { \ |
| 57 | + if (stub_lib_log_level_enabled(level)) { \ |
| 58 | + STUB_LOG(fmt, ##__VA_ARGS__); \ |
| 59 | + } \ |
| 60 | + } while (0) |
| 61 | +#else |
| 62 | +#define STUB_LOG_AT_LEVEL(level, fmt, ...) \ |
| 63 | + do { \ |
| 64 | + } while (0) |
| 65 | +#endif |
| 66 | + |
| 67 | +#define STUB_LOGE(fmt, ...) STUB_LOG_AT_LEVEL(STUB_LOG_LEVEL_E, "STUB_E: " fmt, ##__VA_ARGS__) |
| 68 | +#define STUB_LOGW(fmt, ...) STUB_LOG_AT_LEVEL(STUB_LOG_LEVEL_W, "STUB_W: " fmt, ##__VA_ARGS__) |
| 69 | +#define STUB_LOGI(fmt, ...) STUB_LOG_AT_LEVEL(STUB_LOG_LEVEL_I, "STUB_I: " fmt, ##__VA_ARGS__) |
| 70 | +#define STUB_LOGD(fmt, ...) STUB_LOG_AT_LEVEL(STUB_LOG_LEVEL_D, "STUB_D: " fmt, ##__VA_ARGS__) |
| 71 | +#define STUB_LOGV(fmt, ...) STUB_LOG_AT_LEVEL(STUB_LOG_LEVEL_V, "STUB_V: " fmt, ##__VA_ARGS__) |
41 | 72 | // trace only function name |
42 | | -#define STUB_LOG_TRACE() STUB_LOG("STUB_T: %s()\n", __func__) |
| 73 | +#define STUB_LOG_TRACE() STUB_LOG_AT_LEVEL(STUB_LOG_LEVEL_T, "STUB_T: %s()\n", __func__) |
43 | 74 | // trace with format |
44 | | -#define STUB_LOG_TRACEF(fmt, ...) STUB_LOG("STUB_T: %s(): " fmt, __func__, ##__VA_ARGS__) |
| 75 | +#define STUB_LOG_TRACEF(fmt, ...) STUB_LOG_AT_LEVEL(STUB_LOG_LEVEL_T, "STUB_T: %s(): " fmt, __func__, ##__VA_ARGS__) |
0 commit comments