Skip to content

Commit ad008f1

Browse files
committed
wip add compile time BUF/UART dispatching for logger
1 parent 91d22aa commit ad008f1

File tree

5 files changed

+41
-36
lines changed

5 files changed

+41
-36
lines changed

CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ target_include_directories(${ESP_STUB_LIB}
2222
PRIVATE include/esp-stub-lib
2323
)
2424

25-
message("DBG ESP_STUB_LIB: ${ESP_STUB_LIB}, STUB_COMPILE_DEFS: ${STUB_COMPILE_DEFS}")
2625
# STUB_COMPILE_DEFS is optional definitions coming from the parent CMakeLists.txt
2726
target_compile_definitions(${ESP_STUB_LIB} PRIVATE ${STUB_COMPILE_DEFS})
2827

example/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,5 +51,6 @@ add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
5151
)
5252

5353
get_filename_component(PROJECT_ROOT ${CMAKE_CURRENT_LIST_DIR}/.. ABSOLUTE)
54+
set(STUB_COMPILE_DEFS "STUB_LOG_ENABLED;STUB_LIB_LOG_BUF")
5455
add_subdirectory(${PROJECT_ROOT} esp-stub-lib)
5556
target_link_libraries(${PROJECT_NAME} PRIVATE esp-stub-lib)

example/stub_main.c

Lines changed: 0 additions & 8 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() stub_lib_log_init(STUB_LIB_LOG_DEST_UART)
27-
#define STUB_LOG(fmt, ...) stub_lib_log_printf(fmt, ##__VA_ARGS__)
28-
#else
29-
#define STUB_LOG_INIT()
30-
#define STUB_LOG(fmt, ...)
31-
#endif
32-
3325
static __attribute__((unused)) int handle_test1(va_list ap)
3426
{
3527
(void)ap;

include/esp-stub-lib/log.h

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,32 @@
66

77
#pragma once
88

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

11-
enum stub_lib_log_destination {
12-
STUB_LIB_LOG_DEST_NONE,
13-
STUB_LIB_LOG_DEST_UART, /* using default UART configuration */
14-
STUB_LIB_LOG_DEST_BUF, /* using a SRAM buffer for logging, that passed back via g_stub_lib_log_buf */
15-
};
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)
16+
#include <stdint.h>
1617

1718
/* should be power of 2 */
1819
#define STUB_LIB_LOG_BUF_SIZE 4096
1920
struct stub_lib_log_buf {
20-
uint32_t count;
21-
char buf[STUB_LIB_LOG_BUF_SIZE];
21+
uint32_t count;
22+
char buf[STUB_LIB_LOG_BUF_SIZE];
2223
};
24+
#endif // defined(STUB_LIB_LOG_BUF)
2325

24-
void stub_lib_log_init(enum stub_lib_log_destination dest);
26+
void stub_lib_log_init();
2527
void stub_lib_log_printf(const char *fmt, ...);
28+
29+
#define STUB_LOG_INIT() stub_lib_log_init()
30+
#define STUB_LOG(fmt, ...) stub_lib_log_printf(fmt, ##__VA_ARGS__)
31+
32+
#else // defined(STUB_LOG_ENABLED)
33+
34+
#define STUB_LOG_INIT()
35+
#define STUB_LOG(fmt, ...)
36+
37+
#endif // defined(STUB_LOG_ENABLED)

src/log.c

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
*/
66
#include <log.h>
77

8+
#if defined(STUB_LOG_ENABLED)
9+
810
#include <stdint.h>
911
#include <stdarg.h>
1012
#include <stddef.h>
@@ -18,30 +20,27 @@ extern void ets_printf(const char *fmt, ...);
1820
extern void ets_install_putc1(void (*p)(char c));
1921
extern void ets_install_putc2(void (*p)(char c));
2022

21-
static enum stub_lib_log_destination s_log_dest = STUB_LIB_LOG_DEST_BUF;
22-
23-
static struct stub_lib_log_buf g_stub_lib_log_buf;
24-
25-
static void log_buf_write(char c)
23+
#if defined(STUB_LIB_LOG_BUF)
24+
struct stub_lib_log_buf g_stub_lib_log_buf;
25+
static void stub_lib_log_buf_write_internal(char c)
2626
{
2727
g_stub_lib_log_buf.buf[g_stub_lib_log_buf.count] = c;
2828
g_stub_lib_log_buf.count = (g_stub_lib_log_buf.count + 1) & (STUB_LIB_LOG_BUF_SIZE - 1);
2929
}
30+
#endif // defined(STUB_LIB_LOG_BUF)
3031

31-
void stub_lib_log_init(enum stub_lib_log_destination dest)
32+
void stub_lib_log_init()
3233
{
33-
if (dest == STUB_LIB_LOG_DEST_UART) {
34-
stub_target_uart_init(0, 115200);
35-
//fixme: call ets_install_putc1(0)/putc2(0) here?
36-
ets_install_uart_printf();
37-
s_log_dest = STUB_LIB_LOG_DEST_UART;
38-
} else if (dest == STUB_LIB_LOG_DEST_BUF) {
39-
ets_install_putc1(log_buf_write);
40-
ets_install_putc2(NULL);
41-
s_log_dest = STUB_LIB_LOG_DEST_BUF;
42-
} else {
43-
s_log_dest = STUB_LIB_LOG_DEST_NONE;
44-
}
34+
#if defined(STUB_LIB_LOG_UART)
35+
stub_target_uart_init(0, 115200);
36+
//fixme: call ets_install_putc1(0)/putc2(0) here?
37+
ets_install_uart_printf();
38+
#elif defined(STUB_LIB_LOG_BUF)
39+
ets_install_putc1(stub_lib_log_buf_write_internal);
40+
ets_install_putc2(NULL);
41+
#else
42+
#error "STUB_LIB_LOG_X destination should be defined"
43+
#endif
4544
}
4645

4746
// This function is designed to avoid implementing vprintf() to reduce code size.
@@ -99,3 +98,5 @@ void stub_lib_log_printf(const char *fmt, ...)
9998
}
10099
va_end(args);
101100
}
101+
102+
#endif // defined(STUB_LOG_ENABLED)

0 commit comments

Comments
 (0)