1 /*
2 * Copyright (c) 2022 Intel Corporation
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include "mock_backend.h"
8 #include <zephyr/ztest.h>
9 #include <zephyr/logging/log_core.h>
10 #include <zephyr/logging/log_backend_std.h>
11 #include <stdlib.h>
12
13 static uint32_t log_format_current = CONFIG_LOG_BACKEND_MOCK_OUTPUT_DEFAULT;
14 union log_msg_generic *test_msg;
15
16 extern struct k_sem my_sem;
17 static uint8_t mock_output_buf[1];
18 uint8_t test_output_buf[256];
19 int pos_in_buf;
20
21 /**
22 * @brief This function takes in the characters and copies
23 * them in the temporary buffer.
24 *
25 * @return No. of characters copied to the temporary buffer.
26 */
char_out(uint8_t * data,size_t length,void * ctx)27 static int char_out(uint8_t *data, size_t length, void *ctx)
28 {
29 ARG_UNUSED(ctx);
30 size_t output_length = length;
31
32 while (length > 0) {
33 test_output_buf[pos_in_buf] = *data;
34 pos_in_buf++;
35 if (*data == '\n') {
36 k_sem_give(&my_sem);
37 }
38 data++;
39 length--;
40
41 zassert_not_equal(pos_in_buf, sizeof(test_output_buf)-1,
42 "Increase the size of test_output_buf");
43 }
44 return output_length;
45 }
46
47 LOG_OUTPUT_DEFINE(log_output_mock, char_out, mock_output_buf, sizeof(mock_output_buf));
48
process(const struct log_backend * const backend,union log_msg_generic * msg)49 static void process(const struct log_backend *const backend,
50 union log_msg_generic *msg)
51 {
52 uint32_t flags = log_backend_std_get_flags();
53
54 test_msg = msg;
55
56 log_format_func_t log_output_func = log_format_func_t_get(log_format_current);
57
58 log_output_func(&log_output_mock, &msg->log, flags);
59 }
60
remove_timestamp(const char * raw_string,int timestamp_start)61 int remove_timestamp(const char *raw_string, int timestamp_start)
62 {
63 int timestamp_end = timestamp_start;
64 int count = 1;
65
66 while (count > 0) {
67
68 char c = raw_string[++timestamp_end];
69
70 if (c == '[') {
71 count++;
72 } else if (c == '<') {
73 count--;
74 }
75 }
76 return timestamp_end;
77 }
78
validate_log_type(const char * raw_data_str,uint32_t log_type)79 void validate_log_type(const char *raw_data_str, uint32_t log_type)
80 {
81 const char *output_str = test_output_buf;
82
83 if (log_type == LOG_OUTPUT_TEXT) {
84
85 int pos = remove_timestamp(output_str, 0);
86
87 /* Skip comparing Timestamp */
88 output_str = output_str+pos;
89 }
90
91 /* Validate raw_data_str prefix in the output_str */
92 zassert_mem_equal(raw_data_str, output_str, strlen(raw_data_str),
93 "Incorrect Format comparison %s vs %s", output_str, raw_data_str);
94
95 memset(test_output_buf, 0, sizeof(test_output_buf));
96 pos_in_buf = 0;
97 }
98
format_set(const struct log_backend * const backend,uint32_t log_type)99 static int format_set(const struct log_backend *const backend, uint32_t log_type)
100 {
101 log_format_current = log_type;
102 return 0;
103 }
104
mock_init(struct log_backend const * const backend)105 static void mock_init(struct log_backend const *const backend)
106 {
107
108 }
109
panic(struct log_backend const * const backend)110 static void panic(struct log_backend const *const backend)
111 {
112
113 }
114
115 const struct log_backend_api mock_log_backend_api = {
116 .process = process,
117 .init = mock_init,
118 .format_set = format_set,
119 .panic = panic
120 };
121
122 LOG_BACKEND_DEFINE(log_backend_mock, mock_log_backend_api, true);
123