Skip to content

Commit 7fb4d7f

Browse files
committed
logger: add logging to a RAM buffer
1 parent e36a73f commit 7fb4d7f

File tree

5 files changed

+62
-19
lines changed

5 files changed

+62
-19
lines changed

CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@ set(srcs
1818
add_library(${ESP_STUB_LIB} STATIC ${srcs})
1919

2020
target_include_directories(${ESP_STUB_LIB}
21-
PUBLIC include
21+
INTERFACE include
2222
PRIVATE include/esp-stub-lib
2323
)
24+
include_directories(include)
2425

2526
# STUB_COMPILE_DEFS is optional definitions coming from the parent CMakeLists.txt
2627
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: 1 addition & 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;
@@ -84,7 +76,7 @@ int stub_main(int cmd, ...)
8476

8577
va_start(ap, cmd);
8678

87-
STUB_LOG_INIT(0, 115200);
79+
STUB_LOG_INIT();
8880

8981
stub_lib_flash_init(&flash_state);
9082

include/esp-stub-lib/log.h

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,42 @@
66

77
#pragma once
88

9+
#if defined(STUB_LOG_ENABLED)
10+
11+
#if !defined(STUB_LIB_LOG_BUF) && !defined(STUB_LIB_LOG_UART)
12+
#define STUB_LIB_LOG_BUF
13+
#endif
14+
15+
#if defined(STUB_LIB_LOG_BUF)
916
#include <stdint.h>
1017

18+
#if !defined(STUB_LIB_LOG_BUF_SIZE)
19+
#define STUB_LIB_LOG_BUF_SIZE 4096
20+
#endif
21+
22+
struct stub_lib_log_buf {
23+
uint32_t count;
24+
char buf[STUB_LIB_LOG_BUF_SIZE];
25+
};
26+
#endif // defined(STUB_LIB_LOG_BUF)
27+
1128
#ifdef __cplusplus
1229
extern "C" {
1330
#endif // __cplusplus
1431

15-
void stub_lib_log_init(uint8_t uart_num, uint32_t baudrate);
32+
void stub_lib_log_init();
1633
void stub_lib_log_printf(const char *fmt, ...);
1734

1835
#ifdef __cplusplus
1936
}
2037
#endif // __cplusplus
38+
39+
#define STUB_LOG_INIT() stub_lib_log_init()
40+
#define STUB_LOG(fmt, ...) stub_lib_log_printf(fmt, ##__VA_ARGS__)
41+
42+
#else // defined(STUB_LOG_ENABLED)
43+
44+
#define STUB_LOG_INIT()
45+
#define STUB_LOG(fmt, ...)
46+
47+
#endif // defined(STUB_LOG_ENABLED)

src/log.c

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,40 @@
33
*
44
* SPDX-License-Identifier: Apache-2.0 OR MIT
55
*/
6-
#include <stdint.h>
6+
#include <log.h>
7+
8+
#if defined(STUB_LOG_ENABLED)
9+
710
#include <stdarg.h>
11+
extern void ets_printf(const char *fmt, ...);
812

9-
#include <target/esp_rom_caps.h>
13+
#if defined(STUB_LIB_LOG_UART)
1014
#include <target/uart.h>
11-
12-
// These functions are defined in the ROM
1315
extern void ets_install_uart_printf(void);
14-
extern void ets_printf(const char *fmt, ...);
16+
#elif defined(STUB_LIB_LOG_BUF)
17+
#include <stddef.h>
18+
extern void ets_install_putc1(void (*p)(char c));
19+
extern void ets_install_putc2(void (*p)(char c));
20+
#endif // defined(STUB_LIB_LOG_BUF)
1521

16-
void stub_lib_log_init(uint8_t uart_num, uint32_t baudrate)
22+
#if defined(STUB_LIB_LOG_BUF)
23+
static struct stub_lib_log_buf g_stub_lib_log_buf;
24+
static void stub_lib_log_buf_write_internal(char c)
1725
{
18-
stub_target_uart_init(uart_num, baudrate);
26+
g_stub_lib_log_buf.buf[g_stub_lib_log_buf.count] = c;
27+
g_stub_lib_log_buf.count = (g_stub_lib_log_buf.count + 1) % STUB_LIB_LOG_BUF_SIZE;
28+
}
29+
#endif // defined(STUB_LIB_LOG_BUF)
30+
31+
void stub_lib_log_init()
32+
{
33+
#if defined(STUB_LIB_LOG_UART)
34+
stub_target_uart_init(0, 115200);
1935
ets_install_uart_printf();
36+
#elif defined(STUB_LIB_LOG_BUF)
37+
ets_install_putc1(stub_lib_log_buf_write_internal);
38+
ets_install_putc2(NULL);
39+
#endif
2040
}
2141

2242
// This function is designed to avoid implementing vprintf() to reduce code size.
@@ -74,3 +94,5 @@ void stub_lib_log_printf(const char *fmt, ...)
7494
}
7595
va_end(args);
7696
}
97+
98+
#endif // defined(STUB_LOG_ENABLED)

0 commit comments

Comments
 (0)