Skip to content

Commit 07e0548

Browse files
committed
logging: log_output: Extend to support log_msg2 parsing
Added parsing of log_msg2. Signed-off-by: Krzysztof Chruscinski <[email protected]>
1 parent 69663da commit 07e0548

File tree

2 files changed

+104
-6
lines changed

2 files changed

+104
-6
lines changed

include/logging/log_output.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,18 @@ void log_output_msg_process(const struct log_output *output,
109109
struct log_msg *msg,
110110
uint32_t flags);
111111

112+
/** @brief Process log messages v2 to readable strings.
113+
*
114+
* Function is using provided context with the buffer and output function to
115+
* process formatted string and output the data.
116+
*
117+
* @param log_output Pointer to the log output instance.
118+
* @param msg Log message.
119+
* @param flags Optional flags.
120+
*/
121+
void log_output_msg2_process(const struct log_output *log_output,
122+
struct log_msg2 *msg, uint32_t flags);
123+
112124
/** @brief Process log string
113125
*
114126
* Function is formatting provided string adding optional prefixes and

subsys/logging/log_output.c

Lines changed: 92 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,16 @@ static int out_func(int c, void *ctx)
117117
return 0;
118118
}
119119

120+
static int cr_out_func(int c, void *ctx)
121+
{
122+
out_func(c, ctx);
123+
if (c == '\n') {
124+
out_func((int)'\r', ctx);
125+
}
126+
127+
return 0;
128+
}
129+
120130
static int print_formatted(const struct log_output *output,
121131
const char *fmt, ...)
122132
{
@@ -239,19 +249,22 @@ static void color_postfix(const struct log_output *output,
239249

240250

241251
static int ids_print(const struct log_output *output, bool level_on,
242-
bool func_on, uint32_t domain_id, uint32_t source_id, uint32_t level)
252+
bool func_on, uint32_t domain_id, int16_t source_id,
253+
uint32_t level)
243254
{
244255
int total = 0;
245256

246257
if (level_on) {
247258
total += print_formatted(output, "<%s> ", severity[level]);
248259
}
249260

250-
total += print_formatted(output,
261+
if (source_id >= 0) {
262+
total += print_formatted(output,
251263
(func_on &&
252264
((1 << level) & LOG_FUNCTION_PREFIX_MASK)) ?
253265
"%s." : "%s: ",
254266
log_source_name_get(domain_id, source_id));
267+
}
255268

256269
return total;
257270
}
@@ -427,6 +440,23 @@ static void hexdump_print(struct log_msg *msg,
427440
} while (true);
428441
}
429442

443+
static void log_msg2_hexdump(const struct log_output *output,
444+
uint8_t *data, uint32_t len,
445+
int prefix_offset, uint32_t flags)
446+
{
447+
size_t length;
448+
449+
do {
450+
length = MIN(len, HEXDUMP_BYTES_IN_LINE);
451+
452+
hexdump_line_print(output, data, length,
453+
prefix_offset, flags);
454+
data += length;
455+
len -= length;
456+
} while (len);
457+
}
458+
459+
430460
static void raw_string_print(struct log_msg *msg,
431461
const struct log_output *output)
432462
{
@@ -457,7 +487,7 @@ static void raw_string_print(struct log_msg *msg,
457487

458488
static uint32_t prefix_print(const struct log_output *output,
459489
uint32_t flags, bool func_on, uint32_t timestamp, uint8_t level,
460-
uint8_t domain_id, uint16_t source_id)
490+
uint8_t domain_id, int16_t source_id)
461491
{
462492
uint32_t length = 0U;
463493

@@ -499,6 +529,7 @@ static uint32_t prefix_print(const struct log_output *output,
499529
length += ids_print(output, level_on, func_on,
500530
domain_id, source_id, level);
501531

532+
502533
return length;
503534
}
504535

@@ -518,7 +549,7 @@ void log_output_msg_process(const struct log_output *output,
518549
uint32_t timestamp = log_msg_timestamp_get(msg);
519550
uint8_t level = (uint8_t)log_msg_level_get(msg);
520551
uint8_t domain_id = (uint8_t)log_msg_domain_id_get(msg);
521-
uint16_t source_id = (uint16_t)log_msg_source_id_get(msg);
552+
int16_t source_id = (int16_t)log_msg_source_id_get(msg);
522553
bool raw_string = (level == LOG_LEVEL_INTERNAL_RAW_STRING);
523554
int prefix_offset;
524555

@@ -547,6 +578,61 @@ void log_output_msg_process(const struct log_output *output,
547578
log_output_flush(output);
548579
}
549580

581+
void log_output_msg2_process(const struct log_output *output,
582+
struct log_msg2 *msg, uint32_t flags)
583+
{
584+
log_timestamp_t timestamp = log_msg2_get_timestamp(msg);
585+
uint8_t level = log_msg2_get_level(msg);
586+
bool raw_string = (level == LOG_LEVEL_INTERNAL_RAW_STRING);
587+
uint32_t prefix_offset;
588+
589+
if (IS_ENABLED(CONFIG_LOG_MIPI_SYST_ENABLE) &&
590+
flags & LOG_OUTPUT_FLAG_FORMAT_SYST) {
591+
__ASSERT_NO_MSG(0);
592+
/* todo not supported
593+
* log_output_msg_syst_process(output, msg, flags);
594+
*/
595+
return;
596+
}
597+
598+
if (!raw_string) {
599+
void *source = (void *)log_msg2_get_source(msg);
600+
uint8_t domain_id = log_msg2_get_domain(msg);
601+
int16_t source_id = source ?
602+
(IS_ENABLED(CONFIG_LOG_RUNTIME_FILTERING) ?
603+
log_dynamic_source_id(source) :
604+
log_const_source_id(source)) :
605+
-1;
606+
607+
prefix_offset = prefix_print(output, flags, 0, timestamp,
608+
level, domain_id, source_id);
609+
} else {
610+
prefix_offset = 0;
611+
}
612+
613+
size_t len;
614+
uint8_t *data = log_msg2_get_package(msg, &len);
615+
616+
if (len) {
617+
int err = cbpprintf(raw_string ? cr_out_func : out_func,
618+
(void *)output, data);
619+
620+
(void)err;
621+
__ASSERT_NO_MSG(err >= 0);
622+
}
623+
624+
data = log_msg2_get_data(msg, &len);
625+
if (len) {
626+
log_msg2_hexdump(output, data, len, prefix_offset, flags);
627+
}
628+
629+
if (!raw_string) {
630+
postfix_print(output, flags, level);
631+
}
632+
633+
log_output_flush(output);
634+
}
635+
550636
static bool ends_with_newline(const char *fmt)
551637
{
552638
char c = '\0';
@@ -566,7 +652,7 @@ void log_output_string(const struct log_output *output,
566652
int length;
567653
uint8_t level = (uint8_t)src_level.level;
568654
uint8_t domain_id = (uint8_t)src_level.domain_id;
569-
uint16_t source_id = (uint16_t)src_level.source_id;
655+
int16_t source_id = (int16_t)src_level.source_id;
570656
bool raw_string = (level == LOG_LEVEL_INTERNAL_RAW_STRING);
571657

572658
if (IS_ENABLED(CONFIG_LOG_MIPI_SYST_ENABLE) &&
@@ -605,7 +691,7 @@ void log_output_hexdump(const struct log_output *output,
605691
uint32_t prefix_offset;
606692
uint8_t level = (uint8_t)src_level.level;
607693
uint8_t domain_id = (uint8_t)src_level.domain_id;
608-
uint16_t source_id = (uint16_t)src_level.source_id;
694+
int16_t source_id = (int16_t)src_level.source_id;
609695

610696
if (IS_ENABLED(CONFIG_LOG_MIPI_SYST_ENABLE) &&
611697
flags & LOG_OUTPUT_FLAG_FORMAT_SYST) {

0 commit comments

Comments
 (0)