|
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 | +/** |
| 31 | + * @brief Initialize logging backend and set the initial log level. |
| 32 | + * |
| 33 | + * @param level Initial log level. |
| 34 | + */ |
| 35 | +void stub_lib_log_init(stub_lib_log_level_t level); |
| 36 | + |
| 37 | +/** |
| 38 | + * @brief Set the current runtime log level. |
| 39 | + * |
| 40 | + * @param level New log level. |
| 41 | + */ |
| 42 | +void stub_lib_log_set_level(stub_lib_log_level_t level); |
| 43 | + |
| 44 | +/** |
| 45 | + * @brief Check whether a log level is currently enabled. |
| 46 | + * |
| 47 | + * @param level Log level to check. |
| 48 | + * @return Non-zero if enabled, zero otherwise. |
| 49 | + */ |
| 50 | +int stub_lib_log_level_enabled(stub_lib_log_level_t level); |
| 51 | + |
| 52 | +/** |
| 53 | + * @brief Print a formatted log message. |
| 54 | + * |
| 55 | + * @param fmt Format string. |
| 56 | + * |
| 57 | + * @note Supported format specifiers are: `%s`, `%d`, `%u`, `%x`, `%X`, `%c`. |
| 58 | + * Other specifiers are not supported. |
| 59 | + */ |
16 | 60 | void stub_lib_log_printf(const char *fmt, ...); |
17 | 61 |
|
18 | 62 | #ifdef __cplusplus |
19 | 63 | } |
20 | 64 | #endif // __cplusplus |
21 | 65 |
|
22 | | -#define STUB_LOG_INIT() stub_lib_log_init() |
23 | | -#define STUB_LOG(fmt, ...) stub_lib_log_printf(fmt, ##__VA_ARGS__) |
| 66 | +#define STUB_LOG_INIT(level) stub_lib_log_init(level) |
| 67 | +#define STUB_LOG(fmt, ...) stub_lib_log_printf(fmt, ##__VA_ARGS__) |
24 | 68 |
|
25 | 69 | #else // defined(STUB_LOG_ENABLED) |
26 | 70 |
|
27 | | -#define STUB_LOG_INIT() \ |
| 71 | +#define STUB_LOG_INIT(level) \ |
28 | 72 | do { \ |
| 73 | + (void)(level); \ |
29 | 74 | } while (0) |
30 | 75 | #define STUB_LOG(fmt, ...) \ |
31 | 76 | do { \ |
32 | 77 | } while (0) |
33 | 78 |
|
34 | 79 | #endif // defined(STUB_LOG_ENABLED) |
35 | 80 |
|
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__) |
| 81 | +#if defined(STUB_LOG_ENABLED) |
| 82 | +#define STUB_LOG_AT_LEVEL(level, fmt, ...) \ |
| 83 | + do { \ |
| 84 | + if (stub_lib_log_level_enabled(level)) { \ |
| 85 | + STUB_LOG(fmt, ##__VA_ARGS__); \ |
| 86 | + } \ |
| 87 | + } while (0) |
| 88 | +#else |
| 89 | +#define STUB_LOG_AT_LEVEL(level, fmt, ...) \ |
| 90 | + do { \ |
| 91 | + } while (0) |
| 92 | +#endif |
| 93 | + |
| 94 | +#define STUB_LOGE(fmt, ...) STUB_LOG_AT_LEVEL(STUB_LOG_LEVEL_E, "STUB_E: " fmt, ##__VA_ARGS__) |
| 95 | +#define STUB_LOGW(fmt, ...) STUB_LOG_AT_LEVEL(STUB_LOG_LEVEL_W, "STUB_W: " fmt, ##__VA_ARGS__) |
| 96 | +#define STUB_LOGI(fmt, ...) STUB_LOG_AT_LEVEL(STUB_LOG_LEVEL_I, "STUB_I: " fmt, ##__VA_ARGS__) |
| 97 | +#define STUB_LOGD(fmt, ...) STUB_LOG_AT_LEVEL(STUB_LOG_LEVEL_D, "STUB_D: " fmt, ##__VA_ARGS__) |
| 98 | +#define STUB_LOGV(fmt, ...) STUB_LOG_AT_LEVEL(STUB_LOG_LEVEL_V, "STUB_V: " fmt, ##__VA_ARGS__) |
41 | 99 | // trace only function name |
42 | | -#define STUB_LOG_TRACE() STUB_LOG("STUB_T: %s()\n", __func__) |
| 100 | +#define STUB_LOG_TRACE() STUB_LOG_AT_LEVEL(STUB_LOG_LEVEL_T, "STUB_T: %s()\n", __func__) |
43 | 101 | // trace with format |
44 | | -#define STUB_LOG_TRACEF(fmt, ...) STUB_LOG("STUB_T: %s(): " fmt, __func__, ##__VA_ARGS__) |
| 102 | +#define STUB_LOG_TRACEF(fmt, ...) STUB_LOG_AT_LEVEL(STUB_LOG_LEVEL_T, "STUB_T: %s(): " fmt, __func__, ##__VA_ARGS__) |
0 commit comments