1 /*
2  * Copyright (c) 2019 Intel Corporation
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/kernel.h>
8 #include <zephyr/logging/log.h>
9 #include <zephyr/sys/printk.h>
10 #include <zephyr/logging/log_ctrl.h>
11 #include <zephyr/logging/log_output.h>
12 
13 #define DATA_MAX_DLEN 8
14 #define LOG_MODULE_NAME syst
15 LOG_MODULE_REGISTER(LOG_MODULE_NAME, LOG_LEVEL_DBG);
16 
17 struct test_frame {
18 	uint32_t id_type : 1;
19 	uint32_t rtr     : 1;
20 	/* Message identifier */
21 	union {
22 		uint32_t std_id  : 11;
23 		uint32_t ext_id  : 29;
24 	};
25 	/* The length of the message (max. 8) in byte */
26 	uint8_t dlc;
27 	/* The message data */
28 	union {
29 		uint8_t data[8];
30 		uint32_t data_32[2];
31 	};
32 } __packed;
33 
log_msgs(void)34 void log_msgs(void)
35 {
36 	struct test_frame frame = { 0 };
37 	const uint8_t data[DATA_MAX_DLEN] = { 0x01, 0x02, 0x03, 0x04,
38 					0x05, 0x06, 0x07, 0x08 };
39 
40 	char c = '!';
41 	const char *s = "static str";
42 	const char *s1 = "c str";
43 	char vs0[32];
44 	char vs1[32];
45 
46 	/* standard print */
47 	LOG_ERR("Error message example.");
48 	LOG_WRN("Warning message example.");
49 	LOG_INF("Info message example.");
50 	LOG_DBG("Debug message example.");
51 
52 	LOG_DBG("Debug message example, %d", 1);
53 	LOG_DBG("Debug message example, %d, %d", 1, 2);
54 	LOG_DBG("Debug message example, %d, %d, %d", 1, 2, 3);
55 	LOG_DBG("Debug message example, %d, %d, %d, 0x%x", 1, 2, 3, 4);
56 
57 	memset(vs0, 0, sizeof(vs0));
58 	snprintk(&vs0[0], sizeof(vs0), "%s", "dynamic str");
59 
60 	memset(vs1, 0, sizeof(vs1));
61 	snprintk(&vs1[0], sizeof(vs1), "%s", "another dynamic str");
62 
63 	LOG_DBG("char %c", c);
64 	LOG_DBG("s str %s %s", s, s1);
65 
66 	LOG_DBG("d str %s", vs0);
67 	LOG_DBG("mixed str %s %s %s %s %s %s %s", vs0, "---", vs0, "---", vs1, "---", vs1);
68 	LOG_DBG("mixed c/s %c %s %s %s %c", c, s, vs0, s, c);
69 
70 	LOG_DBG("Debug message example, %f", 3.14159265359);
71 
72 	/* hexdump */
73 	frame.rtr = 1U;
74 	frame.id_type = 1U;
75 	frame.std_id = 1234U;
76 	frame.dlc = sizeof(data);
77 	memcpy(frame.data, data, sizeof(data));
78 
79 	LOG_HEXDUMP_ERR((const uint8_t *)&frame, sizeof(frame), "frame");
80 	LOG_HEXDUMP_WRN((const uint8_t *)&frame, sizeof(frame), "frame");
81 	LOG_HEXDUMP_INF((const uint8_t *)&frame, sizeof(frame), "frame");
82 	LOG_HEXDUMP_DBG((const uint8_t *)&frame, sizeof(frame), "frame");
83 
84 	/* raw string */
85 	printk("hello sys-t on board %s\n", CONFIG_BOARD);
86 
87 #if CONFIG_LOG_MODE_DEFERRED
88 	/*
89 	 * When deferred logging is enabled, the work is being performed by
90 	 * another thread. This k_sleep() gives that thread time to process
91 	 * those messages.
92 	 */
93 
94 	k_sleep(K_TICKS(10));
95 #endif
96 }
97 
main(void)98 int main(void)
99 {
100 	log_msgs();
101 
102 	uint32_t log_type = LOG_OUTPUT_TEXT;
103 
104 	log_format_set_all_active_backends(log_type);
105 
106 	log_msgs();
107 
108 	log_type = LOG_OUTPUT_SYST;
109 	log_format_set_all_active_backends(log_type);
110 
111 	log_msgs();
112 
113 	log_type = LOG_OUTPUT_TEXT;
114 	log_format_set_all_active_backends(log_type);
115 
116 	/* raw string */
117 	printk("SYST Sample Execution Completed\n");
118 	return 0;
119 }
120