1 /*
2 * Copyright (c) 2020 HiSilicon (Shanghai) Technologies CO., LIMITED.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 * Description: SOC lOG
15 */
16
17 #include "soc_log.h"
18 #include "soc_log_strategy.h"
19 #include "dfx_adapt_layer.h"
20
21 #if (CONFIG_DFX_SUPPORT_SOC_LOG == DFX_YES)
22
23 #define MAX_PRINT_STR_SIZE 256
24
25 static dfx_write_data_interface_t g_write_impl = {0};
26
soc_param_init(soc_log_param_t * param)27 STATIC void soc_param_init(soc_log_param_t *param)
28 {
29 param->level = SOC_LOG_DEFAULT_LEVEL;
30 param->fn_name = NULL;
31 param->line_num = SOC_LOG_INVALID_LINE;
32 param->module_id = SOC_LOG_DEFAULT_MODULE_ID;
33 param->type = SOC_LOG_TYPE_INVALID;
34 }
35
soc_log_print(uint32_t level,uint32_t module_id,const char * fn_name,uint32_t line_num,const char * format,...)36 void soc_log_print(uint32_t level, uint32_t module_id, const char *fn_name, uint32_t line_num, const char *format, ...)
37 {
38 va_list args;
39 uint32_t lock_state;
40 int format_len;
41 uint8_t *buf = NULL;
42 soc_log_param_t param;
43 dfx_write_data_interface_t impl;
44 uint8_t *data[2];
45 uint16_t len[2];
46
47 soc_param_init(¶m);
48 param.type = SOC_LOG_TYPE_PRINT;
49 param.level = (uint8_t)level;
50 param.module_id = (uint16_t)module_id;
51 param.fn_name = fn_name;
52 param.line_num = line_num;
53
54 buf = dfx_malloc(0, MAX_PRINT_STR_SIZE);
55 if (buf == NULL) {
56 goto end;
57 }
58
59 va_start(args, format);
60 format_len = vsprintf_s((char*)buf, MAX_PRINT_STR_SIZE, format, args);
61 if (format_len < 0) {
62 va_end(args);
63 goto end;
64 }
65 va_end(args);
66
67 lock_state = dfx_int_lock();
68 impl.write = g_write_impl.write;
69 impl.fd = g_write_impl.fd;
70 dfx_int_restore(lock_state);
71
72 data[0] = (uint8_t*)¶m;
73 len[0] = (uint16_t)sizeof(soc_log_param_t);
74 data[1] = (uint8_t*)buf;
75 len[1] = (uint16_t)format_len;
76 if (impl.write) {
77 impl.write(impl.fd, DFX_DATA_TYPE_UAPI_LOG, data, len, 2); /* 2 是 data 和 len 的数组大小 */
78 }
79
80 end:
81 if (buf) {
82 dfx_free(0, buf);
83 }
84 }
85
soc_log_simple_print(const char * format,...)86 void soc_log_simple_print(const char *format, ...)
87 {
88 uint8_t *buf = NULL;
89 va_list args;
90 int format_len;
91 uint32_t lock_state;
92 soc_log_param_t param = { 0 };
93 dfx_write_data_interface_t impl;
94 uint8_t *data[2];
95 uint16_t len[2];
96
97 soc_param_init(¶m);
98 param.type = SOC_LOG_TYPE_SIMPLE_PRINT;
99
100 buf = dfx_malloc(0, MAX_PRINT_STR_SIZE);
101 if (buf == NULL) {
102 goto end;
103 }
104
105 va_start(args, format);
106 format_len = vsprintf_s((char*)buf, MAX_PRINT_STR_SIZE, format, args);
107 if (format_len < 0) {
108 va_end(args);
109 goto end;
110 }
111 va_end(args);
112
113 lock_state = dfx_int_lock();
114 impl.write = g_write_impl.write;
115 impl.fd = g_write_impl.fd;
116 dfx_int_restore(lock_state);
117
118 data[0] = (uint8_t *)¶m;
119 len[0] = (uint16_t)sizeof(soc_log_param_t);
120 data[1] = buf;
121 len[1] = (uint16_t)format_len;
122 if (impl.write) {
123 impl.write(impl.fd, DFX_DATA_TYPE_UAPI_LOG, data, len, 2); /* 2 是 data 和 len 的数组大小 */
124 }
125 end:
126 if (buf) {
127 dfx_free(0, buf);
128 }
129 }
130
soc_log_register_write_impl(dfx_write_data_interface_t * impl)131 errcode_t soc_log_register_write_impl(dfx_write_data_interface_t *impl)
132 {
133 uint32_t lock_state = dfx_int_lock();
134 g_write_impl.write = impl->write;
135 g_write_impl.fd = impl->fd;
136 dfx_int_restore(lock_state);
137 return ERRCODE_SUCC;
138 }
139
140 #endif /* CONFIG_DFX_SUPPORT_SOC_LOG */