1 /*
2  * Copyright (c) 2018 Nordic Semiconductor ASA
3  * Copyright (c) 2021 Intel Corporation
4  *
5  * SPDX-License-Identifier: Apache-2.0
6  */
7 
8 #include <zephyr/logging/log.h>
9 #include <zephyr/logging/log_ctrl.h>
10 #include <zephyr/logging/log_output.h>
11 #include <zephyr/logging/log_output_dict.h>
12 #include <zephyr/sys/__assert.h>
13 #include <zephyr/sys/util.h>
14 
buffer_write(log_output_func_t outf,uint8_t * buf,size_t len,void * ctx)15 static void buffer_write(log_output_func_t outf, uint8_t *buf, size_t len,
16 			 void *ctx)
17 {
18 	int processed;
19 
20 	do {
21 		processed = outf(buf, len, ctx);
22 		len -= processed;
23 		buf += processed;
24 	} while (len != 0);
25 }
26 
log_dict_output_msg_process(const struct log_output * output,struct log_msg * msg,uint32_t flags)27 void log_dict_output_msg_process(const struct log_output *output,
28 				 struct log_msg *msg, uint32_t flags)
29 {
30 	struct log_dict_output_normal_msg_hdr_t output_hdr;
31 	void *source = (void *)log_msg_get_source(msg);
32 
33 	/* Keep sync with header in struct log_msg */
34 	output_hdr.type = MSG_NORMAL;
35 	output_hdr.domain = msg->hdr.desc.domain;
36 	output_hdr.level = msg->hdr.desc.level;
37 	output_hdr.package_len = msg->hdr.desc.package_len;
38 	output_hdr.data_len = msg->hdr.desc.data_len;
39 	output_hdr.timestamp = msg->hdr.timestamp;
40 
41 	output_hdr.source = (source != NULL) ?
42 				(IS_ENABLED(CONFIG_LOG_RUNTIME_FILTERING) ?
43 					log_dynamic_source_id(source) :
44 					log_const_source_id(source)) :
45 				0U;
46 
47 	buffer_write(output->func, (uint8_t *)&output_hdr, sizeof(output_hdr),
48 		     (void *)output);
49 
50 	size_t len;
51 	uint8_t *data = log_msg_get_package(msg, &len);
52 
53 	if (len > 0U) {
54 		buffer_write(output->func, data, len, (void *)output);
55 	}
56 
57 	data = log_msg_get_data(msg, &len);
58 	if (len > 0U) {
59 		buffer_write(output->func, data, len, (void *)output);
60 	}
61 
62 	log_output_flush(output);
63 }
64 
log_dict_output_dropped_process(const struct log_output * output,uint32_t cnt)65 void log_dict_output_dropped_process(const struct log_output *output, uint32_t cnt)
66 {
67 	struct log_dict_output_dropped_msg_t msg;
68 
69 	msg.type = MSG_DROPPED_MSG;
70 	msg.num_dropped_messages = MIN(cnt, 9999);
71 
72 	buffer_write(output->func, (uint8_t *)&msg, sizeof(msg),
73 		     (void *)output);
74 }
75