1 /*
2  * Copyright (c) 2018 Nordic Semiconductor
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 /**
8  * @file
9  * @brief Test Custom Log Header
10  */
11 
12 #include <zephyr/kernel.h>
13 #include <zephyr/toolchain.h>
14 #include <zephyr/ztest.h>
15 #include <zephyr/logging/log_backend.h>
16 #include <zephyr/logging/log_ctrl.h>
17 #include <zephyr/logging/log.h>
18 
19 LOG_MODULE_REGISTER(test, LOG_LEVEL_DBG);
20 
21 static uint32_t count;
22 static char output[512];
23 
cbprintf_callback(int c,void * ctx)24 static int cbprintf_callback(int c, void *ctx)
25 {
26 	char **p = ctx;
27 	**p = c;
28 	(*p)++;
29 	return c;
30 }
31 
backend_process(const struct log_backend * const backend,union log_msg_generic * msg)32 static void backend_process(const struct log_backend *const backend, union log_msg_generic *msg)
33 {
34 	char *pstr = output;
35 	size_t len;
36 	uint8_t *p = log_msg_get_package(&msg->log, &len);
37 
38 	int slen = cbpprintf(cbprintf_callback, &pstr, p);
39 
40 	output[slen] = '\0';
41 
42 	count += 1;
43 }
44 
45 static const struct log_backend_api backend_api = {
46 	.process = backend_process,
47 };
48 
49 LOG_BACKEND_DEFINE(backend, backend_api, false);
50 
ZTEST(log_custom_header,test_macro_prefix)51 ZTEST(log_custom_header, test_macro_prefix)
52 {
53 	zassert_equal(count, 0);
54 
55 	LOG_DBG("DBG %d", 0);
56 	LOG_PROCESS();
57 	zassert_equal(count, 1);
58 	zassert_mem_equal(output, CUSTOM_LOG_PREFIX "DBG 0", strlen(output));
59 
60 	LOG_INF("INF %s", "foo");
61 	LOG_PROCESS();
62 	zassert_equal(count, 2);
63 	zassert_mem_equal(output, CUSTOM_LOG_PREFIX "INF foo", strlen(output));
64 
65 	LOG_WRN("WRN %x", 0xff);
66 	LOG_PROCESS();
67 	zassert_equal(count, 3);
68 	zassert_mem_equal(output, CUSTOM_LOG_PREFIX "WRN ff", strlen(output));
69 
70 	LOG_ERR("ERR %d %d %d", 1, 2, 3);
71 	LOG_PROCESS();
72 	zassert_equal(count, 4);
73 	zassert_mem_equal(output, CUSTOM_LOG_PREFIX "ERR 1 2 3", strlen(output));
74 }
75 
setup(void)76 static void *setup(void)
77 {
78 	log_init();
79 	return NULL;
80 }
81 
before(void * notused)82 static void before(void *notused)
83 {
84 	count = 0;
85 	output[0] = '\0';
86 
87 	log_backend_enable(&backend, NULL, LOG_LEVEL_DBG);
88 }
89 
after(void * notused)90 static void after(void *notused)
91 {
92 	log_backend_disable(&backend);
93 }
94 
95 ZTEST_SUITE(log_custom_header, NULL, setup, before, after, NULL);
96