1 /*
2 * Copyright (c) 2018 Nordic Semiconductor
3 * Copyright (c) 2024 Vogl Electronic GmbH
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7
8 /**
9 * @file
10 * @brief Test log message
11 */
12
13 #include <zephyr/logging/log.h>
14 #include <zephyr/logging/log_output.h>
15
16 #include <zephyr/tc_util.h>
17 #include <stdbool.h>
18 #include <zephyr/kernel.h>
19 #include <zephyr/ztest.h>
20
21 #define LOG_MODULE_NAME test
22 LOG_MODULE_REGISTER(LOG_MODULE_NAME);
23
24 #define SNAME "src"
25 #define DNAME "domain"
26 #define TEST_STR "test"
27
28 static uint8_t mock_buffer[512];
29 static uint8_t log_output_buf[4];
30 static uint32_t mock_len;
31
reset_mock_buffer(void)32 static void reset_mock_buffer(void)
33 {
34 mock_len = 0U;
35 memset(mock_buffer, 0, sizeof(mock_buffer));
36 }
37
mock_output_func(uint8_t * buf,size_t size,void * ctx)38 static int mock_output_func(uint8_t *buf, size_t size, void *ctx)
39 {
40 memcpy(&mock_buffer[mock_len], buf, size);
41 mock_len += size;
42
43 return size;
44 }
45
46 LOG_OUTPUT_DEFINE(log_output, mock_output_func, log_output_buf, sizeof(log_output_buf));
47
48 BUILD_ASSERT(IS_ENABLED(CONFIG_LOG_BACKEND_NET), "syslog backend not enabled");
49
ZTEST(test_log_output_net,test_format)50 ZTEST(test_log_output_net, test_format)
51 {
52 char package[256];
53
54 static const char *exp_str =
55 "<134>1 1970-01-01T00:00:01.000000Z zephyr - - - - " DNAME "/" SNAME ": " TEST_STR;
56 uint32_t flags = LOG_OUTPUT_FLAG_TIMESTAMP | LOG_OUTPUT_FLAG_FORMAT_TIMESTAMP |
57 LOG_OUTPUT_FLAG_FORMAT_SYSLOG;
58 int err;
59
60 log_output_timestamp_freq_set(1000000);
61
62 err = cbprintf_package(package, sizeof(package), 0, TEST_STR);
63 zassert_true(err > 0);
64
65 log_output_process(&log_output, 1000000, DNAME, SNAME, NULL, LOG_LEVEL_INF, package, NULL,
66 0, flags);
67
68 mock_buffer[mock_len] = '\0';
69 zassert_str_equal(exp_str, mock_buffer, "expected: %s, is: %s", exp_str, mock_buffer);
70 }
71
before(void * notused)72 static void before(void *notused)
73 {
74 reset_mock_buffer();
75 }
76
77 ZTEST_SUITE(test_log_output_net, NULL, NULL, before, NULL, NULL);
78