Skip to content

Commit 6df67f1

Browse files
committed
feat: set log level in runtime
1 parent 0b851d3 commit 6df67f1

File tree

5 files changed

+113
-19
lines changed

5 files changed

+113
-19
lines changed

example/stub_main.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -155,11 +155,13 @@ static __attribute__((unused)) int handle_test1(va_list ap)
155155
STUB_LOG("stub command test:%c\n", 'A');
156156
STUB_LOG("stub command test:%l\n", 10); // not supported
157157

158-
STUB_LOGE("stub command test\n");
159-
STUB_LOGW("stub command test\n");
160-
STUB_LOGI("stub command test\n");
161-
STUB_LOGD("stub command test\n");
162-
STUB_LOGV("stub command test\n");
158+
STUB_LOGE("This is an error message\n");
159+
STUB_LOGW("This is a warning message\n");
160+
STUB_LOGI("This is an info message\n");
161+
162+
stub_lib_log_set_level(STUB_LOG_LEVEL_V);
163+
STUB_LOGD("This is a debug message\n");
164+
STUB_LOGV("This is a verbose message\n");
163165
STUB_LOG_TRACE();
164166
STUB_LOG_TRACEF("foo:%u\n", 0x2A);
165167

@@ -230,7 +232,7 @@ int stub_main(int cmd, ...)
230232

231233
va_start(ap, cmd);
232234

233-
STUB_LOG_INIT();
235+
STUB_LOG_INIT(STUB_LIB_LOG_LEVEL);
234236

235237
STUB_LOGI("Command: 0x%x\n", cmd);
236238

include/esp-stub-lib/log.h

Lines changed: 69 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,39 +6,97 @@
66

77
#pragma once
88

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+
924
#if defined(STUB_LOG_ENABLED)
1025

1126
#ifdef __cplusplus
1227
extern "C" {
1328
#endif // __cplusplus
1429

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+
*/
1660
void stub_lib_log_printf(const char *fmt, ...);
1761

1862
#ifdef __cplusplus
1963
}
2064
#endif // __cplusplus
2165

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__)
2468

2569
#else // defined(STUB_LOG_ENABLED)
2670

27-
#define STUB_LOG_INIT() \
71+
#define STUB_LOG_INIT(level) \
2872
do { \
73+
(void)(level); \
2974
} while (0)
3075
#define STUB_LOG(fmt, ...) \
3176
do { \
3277
} while (0)
3378

3479
#endif // defined(STUB_LOG_ENABLED)
3580

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__)
4199
// 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__)
43101
// 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__)

src/log_buf.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ static void stub_lib_log_buf_write_char(char c)
2626
g_stub_lib_log_buf.count = (g_stub_lib_log_buf.count + 1) % STUB_LIB_LOG_BUF_SIZE;
2727
}
2828

29-
void stub_lib_log_init()
29+
void stub_lib_log_backend_init(void)
3030
{
3131
ets_install_putc1(stub_lib_log_buf_write_char);
3232
ets_install_putc2(NULL);

src/log_common.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,41 @@
55
*/
66
#include <stdarg.h>
77

8+
#include <esp-stub-lib/log.h>
9+
810
extern void ets_printf(const char *fmt, ...);
11+
extern void stub_lib_log_backend_init(void);
12+
13+
static stub_lib_log_level_t g_stub_lib_log_level = STUB_LIB_LOG_LEVEL;
14+
15+
static stub_lib_log_level_t stub_lib_log_level_normalize(stub_lib_log_level_t level)
16+
{
17+
if (level < STUB_LOG_LEVEL_NONE) {
18+
return STUB_LOG_LEVEL_NONE;
19+
}
20+
21+
if (level > STUB_LOG_LEVEL_V) {
22+
return STUB_LOG_LEVEL_V;
23+
}
24+
25+
return level;
26+
}
27+
28+
void stub_lib_log_set_level(stub_lib_log_level_t level)
29+
{
30+
g_stub_lib_log_level = stub_lib_log_level_normalize(level);
31+
}
32+
33+
int stub_lib_log_level_enabled(stub_lib_log_level_t level)
34+
{
35+
return g_stub_lib_log_level >= level;
36+
}
37+
38+
void stub_lib_log_init(stub_lib_log_level_t level)
39+
{
40+
stub_lib_log_set_level(level);
41+
stub_lib_log_backend_init();
42+
}
943

1044
// This function is designed to avoid implementing vprintf() to reduce code size.
1145
// It only supports a subset of format specifiers: %s, %d, %u, %x, %X, %c.

src/log_uart.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ extern void ets_install_putc1(void (*p)(char c));
1212
extern void ets_install_putc2(void (*p)(char c));
1313
extern void ets_install_uart_printf(void);
1414

15-
void stub_lib_log_init()
15+
void stub_lib_log_backend_init(void)
1616
{
1717
stub_target_uart_init(0);
1818
ets_install_putc1(NULL);

0 commit comments

Comments
 (0)