Skip to content

Commit 305302a

Browse files
committed
logger: add logging to a RAM buffer
1 parent e9cac1d commit 305302a

File tree

7 files changed

+107
-31
lines changed

7 files changed

+107
-31
lines changed

CMakeLists.txt

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,30 @@ if(NOT ESP_TARGET_LIB)
1111
endif()
1212

1313
set(srcs
14-
src/log.c
1514
src/flash.c
1615
)
1716

17+
if(STUB_LOG_ENABLED IN_LIST STUB_COMPILE_DEFS)
18+
list(APPEND srcs src/log_common.c)
19+
20+
if(STUB_LIB_LOG_UART IN_LIST STUB_COMPILE_DEFS)
21+
list(APPEND srcs src/log_uart.c)
22+
else()
23+
# STUB_LIB_LOG_BUF is by default
24+
if(NOT STUB_LIB_LOG_BUF IN_LIST STUB_COMPILE_DEFS)
25+
list(APPEND STUB_COMPILE_DEFS STUB_LIB_LOG_BUF)
26+
endif()
27+
list(APPEND srcs src/log_buf.c)
28+
endif()
29+
endif()
30+
1831
add_library(${ESP_STUB_LIB} STATIC ${srcs})
1932

2033
target_include_directories(${ESP_STUB_LIB}
21-
PUBLIC include
34+
INTERFACE include
2235
PRIVATE include/esp-stub-lib
2336
)
37+
include_directories(include)
2438

2539
# STUB_COMPILE_DEFS is optional definitions coming from the parent CMakeLists.txt
2640
target_compile_definitions(${ESP_STUB_LIB} PRIVATE ${STUB_COMPILE_DEFS})

example/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ if(${ESP_TARGET} STREQUAL "esp8266")
3838
target_link_options(${PROJECT_NAME} PRIVATE -Wl,--entry=stub_main_esp8266)
3939
endif()
4040

41-
target_compile_definitions(${PROJECT_NAME} PRIVATE asm=__asm__ STUB_LOG_ENABLED)
41+
set(STUB_COMPILE_DEFS "STUB_LOG_ENABLED")
42+
target_compile_definitions(${PROJECT_NAME} PRIVATE asm=__asm__ ${STUB_COMPILE_DEFS})
4243

4344
set(MAP_FILE ${CMAKE_CURRENT_BINARY_DIR}/${APP_NAME}.map)
4445
target_link_options(${PROJECT_NAME} PRIVATE -Wl,-Map=${MAP_FILE})

example/stub_main.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,6 @@ struct stub_cmd_handler {
2222
int (*handler)(va_list ap);
2323
};
2424

25-
#ifdef STUB_LOG_ENABLED
26-
#define STUB_LOG_INIT(uart_num, baudrate) stub_lib_log_init(uart_num, baudrate)
27-
#define STUB_LOG(fmt, ...) stub_lib_log_printf(fmt, ##__VA_ARGS__)
28-
#else
29-
#define STUB_LOG_INIT(uart_num, baudrate)
30-
#define STUB_LOG(fmt, ...)
31-
#endif
32-
3325
static __attribute__((unused)) int handle_test1(va_list ap)
3426
{
3527
(void)ap;
@@ -41,6 +33,14 @@ static __attribute__((unused)) int handle_test1(va_list ap)
4133
STUB_LOG("stub command test:%c\n", 'A');
4234
STUB_LOG("stub command test:%l\n", 10); // not supported
4335

36+
STUB_LOGE("stub command test\n");
37+
STUB_LOGW("stub command test\n");
38+
STUB_LOGI("stub command test\n");
39+
STUB_LOGD("stub command test\n");
40+
STUB_LOGV("stub command test\n");
41+
STUB_LOG_TRACE();
42+
STUB_LOG_TRACEF("foo:%u\n", 0x2A);
43+
4444
return 0;
4545
}
4646

@@ -84,7 +84,7 @@ int stub_main(int cmd, ...)
8484

8585
va_start(ap, cmd);
8686

87-
STUB_LOG_INIT(0, 115200);
87+
STUB_LOG_INIT();
8888

8989
stub_lib_flash_init(&flash_state);
9090

include/esp-stub-lib/log.h

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,35 @@
66

77
#pragma once
88

9-
#include <stdint.h>
9+
#if defined(STUB_LOG_ENABLED)
1010

1111
#ifdef __cplusplus
1212
extern "C" {
1313
#endif // __cplusplus
1414

15-
void stub_lib_log_init(uint8_t uart_num, uint32_t baudrate);
15+
void stub_lib_log_init();
1616
void stub_lib_log_printf(const char *fmt, ...);
1717

1818
#ifdef __cplusplus
1919
}
2020
#endif // __cplusplus
21+
22+
#define STUB_LOG_INIT() stub_lib_log_init()
23+
#define STUB_LOG(fmt, ...) stub_lib_log_printf(fmt, ##__VA_ARGS__)
24+
25+
#else // defined(STUB_LOG_ENABLED)
26+
27+
#define STUB_LOG_INIT() do {} while(0)
28+
#define STUB_LOG(fmt, ...) do {} while(0)
29+
30+
#endif // defined(STUB_LOG_ENABLED)
31+
32+
#define STUB_LOGE(fmt, ...) STUB_LOG("STUB_E: " fmt, ##__VA_ARGS__)
33+
#define STUB_LOGW(fmt, ...) STUB_LOG("STUB_W: " fmt, ##__VA_ARGS__)
34+
#define STUB_LOGI(fmt, ...) STUB_LOG("STUB_I: " fmt, ##__VA_ARGS__)
35+
#define STUB_LOGD(fmt, ...) STUB_LOG("STUB_D: " fmt, ##__VA_ARGS__)
36+
#define STUB_LOGV(fmt, ...) STUB_LOG("STUB_V: " fmt, ##__VA_ARGS__)
37+
// trace only function name
38+
#define STUB_LOG_TRACE() STUB_LOG("STUB_TRACE %s()\n", __func__)
39+
// trace with format
40+
#define STUB_LOG_TRACEF(fmt, ...) STUB_LOG("STUB_TRACE %s(): " fmt, __func__, ##__VA_ARGS__)

src/log_buf.c

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD
3+
*
4+
* SPDX-License-Identifier: Apache-2.0 OR MIT
5+
*/
6+
#include <log.h>
7+
8+
#include <stdint.h>
9+
#include <stddef.h>
10+
11+
extern void ets_printf(const char *fmt, ...);
12+
extern void ets_install_putc1(void (*p)(char c));
13+
extern void ets_install_putc2(void (*p)(char c));
14+
15+
#if !defined(STUB_LIB_LOG_BUF_SIZE)
16+
#define STUB_LIB_LOG_BUF_SIZE 4096
17+
#endif
18+
19+
struct stub_lib_log_buf {
20+
uint32_t count;
21+
char buf[STUB_LIB_LOG_BUF_SIZE];
22+
};
23+
24+
static struct stub_lib_log_buf g_stub_lib_log_buf;
25+
static void stub_lib_log_buf_write_char(char c)
26+
{
27+
g_stub_lib_log_buf.buf[g_stub_lib_log_buf.count] = c;
28+
g_stub_lib_log_buf.count = (g_stub_lib_log_buf.count + 1) % STUB_LIB_LOG_BUF_SIZE;
29+
}
30+
31+
void stub_lib_log_init()
32+
{
33+
ets_install_putc1(stub_lib_log_buf_write_char);
34+
ets_install_putc2(NULL);
35+
}

src/log.c renamed to src/log_common.c

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,11 @@
33
*
44
* SPDX-License-Identifier: Apache-2.0 OR MIT
55
*/
6-
#include <stdint.h>
7-
#include <stdarg.h>
8-
#include <stddef.h>
6+
#include <log.h>
97

10-
#include <target/esp_rom_caps.h>
11-
#include <target/uart.h>
8+
#include <stdarg.h>
129

13-
// These functions are defined in the ROM
14-
extern void ets_install_uart_printf(void);
1510
extern void ets_printf(const char *fmt, ...);
16-
extern void ets_install_putc1(void (*p)(char c));
17-
extern void ets_install_putc2(void (*p)(char c));
18-
19-
void stub_lib_log_init(uint8_t uart_num, uint32_t baudrate)
20-
{
21-
stub_target_uart_init(uart_num, baudrate);
22-
ets_install_putc1(NULL);
23-
ets_install_putc2(NULL);
24-
ets_install_uart_printf();
25-
}
2611

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

src/log_uart.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD
3+
*
4+
* SPDX-License-Identifier: Apache-2.0 OR MIT
5+
*/
6+
#include <log.h>
7+
8+
#include <target/uart.h>
9+
#include <stddef.h>
10+
11+
extern void ets_install_putc1(void (*p)(char c));
12+
extern void ets_install_putc2(void (*p)(char c));
13+
extern void ets_install_uart_printf(void);
14+
15+
void stub_lib_log_init()
16+
{
17+
stub_target_uart_init(0, 115200);
18+
ets_install_putc1(NULL);
19+
ets_install_putc2(NULL);
20+
ets_install_uart_printf();
21+
}

0 commit comments

Comments
 (0)