1 /*
2 * Copyright (c) 2022 Hunan OpenValley Digital Industry Development Co., Ltd.
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 */
15 #include <stdarg.h>
16 #include <unistd.h>
17 #include "cmsis_os2.h"
18 #include "los_compiler.h"
19 #include "los_debug.h"
20 #include "los_task.h"
21 #include "los_sched.h"
22 #include "nvs.h"
23 #include "ohos_init.h"
24 #include "ohos_types.h"
25 #include "samgr_lite.h"
26 #include "stdio.h"
27 #include "hal/uart_ll.h"
28 #include "hiview_def.h"
29 #include "hiview_output_log.h"
30
31 #define NUM_2 2
32 #define SAFE_OFFSET 4
33 #define BUFF_MAX_LEN 512
34
s_vprintf(const char * fmt,va_list ap)35 static void s_vprintf(const char *fmt, va_list ap)
36 {
37 int len;
38 static char buf[NUM_2][BUFF_MAX_LEN];
39 static char volatile bufLock = 0;
40 char *pbuf;
41 if (xPortInterruptedFromISRContext()) {
42 pbuf = buf[1];
43 } else {
44 pbuf = buf[0];
45 while (bufLock) {
46 if (g_taskScheduled) {
47 LOS_TaskDelay(0);
48 }
49 }
50 bufLock = (char)TRUE;
51 }
52 len = vsnprintf_s(pbuf, BUFF_MAX_LEN - SAFE_OFFSET, BUFF_MAX_LEN - SAFE_OFFSET, fmt, ap);
53 if (len > 0) {
54 uint16_t fill_len;
55 for (fill_len = uart_ll_get_txfifo_len(&UART0); fill_len < len;) {
56 if (fill_len) {
57 uart_ll_write_txfifo(&UART0, (uint8_t *)pbuf, fill_len);
58 len -= fill_len;
59 pbuf += fill_len;
60 }
61 if (bufLock && g_taskScheduled) {
62 LOS_TaskDelay(0);
63 }
64 fill_len = uart_ll_get_txfifo_len(&UART0);
65 }
66 if (len > 0)
67 uart_ll_write_txfifo(&UART0, (uint8_t *)pbuf, len);
68 }
69 if (!xPortInterruptedFromISRContext()) {
70 bufLock = (char)FALSE;
71 }
72 }
73
74 // Liteos_m的打印
printf(const char * __restrict __format,...)75 int printf(const char *__restrict __format, ...)
76 {
77 va_list ap;
78 va_start(ap, __format);
79 s_vprintf(__format, ap);
80 va_end(ap);
81 return 0;
82 }
83
hal_trace_printf(int level,const char * fmt,...)84 int hal_trace_printf(int level, const char *fmt, ...)
85 {
86 if (level <= PRINT_LEVEL) {
87 va_list ap;
88 va_start(ap, fmt);
89 s_vprintf(fmt, ap);
90 va_end(ap);
91 }
92 return 0;
93 }
94
HilogProc_Impl(const HiLogContent * hilogContent,uint32_t len)95 bool HilogProc_Impl(const HiLogContent *hilogContent, uint32_t len)
96 {
97 char tempOutStr[LOG_FMT_MAX_LEN];
98 tempOutStr[0] = 0, tempOutStr[1] = 0;
99 if (LogContentFmt(tempOutStr, sizeof(tempOutStr), hilogContent) > 0) {
100 printf("%s", tempOutStr);
101 }
102 return true;
103 }
104
HiLogWriteInternal(const char * buffer,size_t bufLen)105 int HiLogWriteInternal(const char *buffer, size_t bufLen)
106 {
107 if (!buffer) {
108 return -1;
109 }
110 if (bufLen < NUM_2) {
111 return 0;
112 }
113 if (buffer[bufLen - NUM_2] != '\n') {
114 *((char *)buffer + bufLen - 1) = '\n';
115 }
116 printf("%s\n", buffer);
117 return 0;
118 }
119
init_trace_system(void)120 int init_trace_system(void)
121 {
122 int ret = 1;
123 HiviewRegisterHilogProc(HilogProc_Impl);
124 return ret;
125 }
126