Skip to content

Commit e06a014

Browse files
committed
logging: add UART backend for dictionary based logging
This adds a new UART backend for dictionary based logging, where this can output binary data in both binary and hexidecimal strings. Signed-off-by: Daniel Leung <[email protected]>
1 parent f4202c6 commit e06a014

File tree

2 files changed

+80
-3
lines changed

2 files changed

+80
-3
lines changed

subsys/logging/Kconfig.backends

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,49 @@ config LOG_BACKEND_UART
1010
help
1111
When enabled backend is using UART to output logs.
1212

13+
if LOG_BACKEND_UART
14+
15+
config LOG_BACKEND_UART_OUTPUT_DICTIONARY
16+
bool
17+
depends on LOG2
18+
select LOG_DICTIONARY_SUPPORT
19+
help
20+
UART backend is in dictionary-based logging output mode.
21+
22+
choice
23+
prompt "UART Backend Output Mode"
24+
default LOG_BACKEND_UART_OUTPUT_TEXT
25+
26+
config LOG_BACKEND_UART_OUTPUT_TEXT
27+
bool "Text"
28+
help
29+
Output in text.
30+
1331
config LOG_BACKEND_UART_SYST_ENABLE
14-
bool "Enable UART syst backend"
32+
bool "MIPI SyS-T"
1533
depends on LOG_BACKEND_UART
1634
depends on LOG_MIPI_SYST_ENABLE
1735
help
1836
When enabled backend is using UART to output syst format logs.
1937

38+
config LOG_BACKEND_UART_OUTPUT_DICTIONARY_HEX
39+
bool "Dictionary (hexadecimal)"
40+
depends on LOG2
41+
select LOG_BACKEND_UART_OUTPUT_DICTIONARY
42+
help
43+
Dictionary-based logging output in hexadecimal.
44+
45+
config LOG_BACKEND_UART_OUTPUT_DICTIONARY_BIN
46+
bool "Dictionary (binary)"
47+
depends on LOG2
48+
select LOG_BACKEND_UART_OUTPUT_DICTIONARY
49+
help
50+
Dictionary-based logging output in binary.
51+
52+
endchoice
53+
54+
endif # LOG_BACKEND_UART
55+
2056
config LOG_BACKEND_SWO
2157
bool "Enable Serial Wire Output (SWO) backend"
2258
depends on HAS_SWO

subsys/logging/log_backend_uart.c

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,40 @@
88
#include <logging/log_core.h>
99
#include <logging/log_msg.h>
1010
#include <logging/log_output.h>
11+
#include <logging/log_output_dict.h>
1112
#include <logging/log_backend_std.h>
1213
#include <device.h>
1314
#include <drivers/uart.h>
1415
#include <sys/__assert.h>
1516

17+
/* Fixed size to avoid auto-added trailing '\0'.
18+
* Used if CONFIG_LOG_BACKEND_UART_OUTPUT_DICTIONARY_HEX.
19+
*/
20+
static const char LOG_HEX_SEP[10] = "##ZLOGV1##";
21+
1622
static const struct device *uart_dev;
1723

1824
static int char_out(uint8_t *data, size_t length, void *ctx)
1925
{
2026
ARG_UNUSED(ctx);
2127

2228
for (size_t i = 0; i < length; i++) {
29+
#if defined(CONFIG_LOG_BACKEND_UART_OUTPUT_DICTIONARY_HEX)
30+
char c;
31+
uint8_t x;
32+
33+
/* upper 8-bit */
34+
x = data[i] >> 4;
35+
(void)hex2char(x, &c);
36+
uart_poll_out(uart_dev, c);
37+
38+
/* lower 8-bit */
39+
x = data[i] & 0x0FU;
40+
(void)hex2char(x, &c);
41+
uart_poll_out(uart_dev, c);
42+
#else
2343
uart_poll_out(uart_dev, data[i]);
44+
#endif
2445
}
2546

2647
return length;
@@ -44,13 +65,29 @@ static void process(const struct log_backend *const backend,
4465
{
4566
uint32_t flags = log_backend_std_get_flags();
4667

47-
log_output_msg2_process(&log_output_uart, &msg->log, flags);
68+
if (IS_ENABLED(CONFIG_LOG_BACKEND_UART_OUTPUT_DICTIONARY)) {
69+
log_dict_output_msg2_process(&log_output_uart,
70+
&msg->log, flags);
71+
} else {
72+
log_output_msg2_process(&log_output_uart, &msg->log, flags);
73+
}
4874
}
4975

5076
static void log_backend_uart_init(struct log_backend const *const backend)
5177
{
5278
uart_dev = device_get_binding(CONFIG_UART_CONSOLE_ON_DEV_NAME);
5379
__ASSERT_NO_MSG((void *)uart_dev);
80+
81+
if (IS_ENABLED(CONFIG_LOG_BACKEND_UART_OUTPUT_DICTIONARY_HEX)) {
82+
/* Print a separator so the output can be fed into
83+
* log parser directly. This is useful when capturing
84+
* from UART directly where there might be other output
85+
* (e.g. bootloader).
86+
*/
87+
for (int i = 0; i < sizeof(LOG_HEX_SEP); i++) {
88+
uart_poll_out(uart_dev, LOG_HEX_SEP[i]);
89+
}
90+
}
5491
}
5592

5693
static void panic(struct log_backend const *const backend)
@@ -62,7 +99,11 @@ static void dropped(const struct log_backend *const backend, uint32_t cnt)
6299
{
63100
ARG_UNUSED(backend);
64101

65-
log_backend_std_dropped(&log_output_uart, cnt);
102+
if (IS_ENABLED(CONFIG_LOG_BACKEND_UART_OUTPUT_DICTIONARY)) {
103+
log_dict_output_dropped_process(&log_output_uart, cnt);
104+
} else {
105+
log_backend_std_dropped(&log_output_uart, cnt);
106+
}
66107
}
67108

68109
static void sync_string(const struct log_backend *const backend,

0 commit comments

Comments
 (0)