1d6aed566Sopenharmony_ci/*
2d6aed566Sopenharmony_ci * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
3d6aed566Sopenharmony_ci * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
4d6aed566Sopenharmony_ci *
5d6aed566Sopenharmony_ci * Redistribution and use in source and binary forms, with or without modification,
6d6aed566Sopenharmony_ci * are permitted provided that the following conditions are met:
7d6aed566Sopenharmony_ci *
8d6aed566Sopenharmony_ci * 1. Redistributions of source code must retain the above copyright notice, this list of
9d6aed566Sopenharmony_ci *    conditions and the following disclaimer.
10d6aed566Sopenharmony_ci *
11d6aed566Sopenharmony_ci * 2. Redistributions in binary form must reproduce the above copyright notice, this list
12d6aed566Sopenharmony_ci *    of conditions and the following disclaimer in the documentation and/or other materials
13d6aed566Sopenharmony_ci *    provided with the distribution.
14d6aed566Sopenharmony_ci *
15d6aed566Sopenharmony_ci * 3. Neither the name of the copyright holder nor the names of its contributors may be used
16d6aed566Sopenharmony_ci *    to endorse or promote products derived from this software without specific prior written
17d6aed566Sopenharmony_ci *    permission.
18d6aed566Sopenharmony_ci *
19d6aed566Sopenharmony_ci * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20d6aed566Sopenharmony_ci * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21d6aed566Sopenharmony_ci * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22d6aed566Sopenharmony_ci * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23d6aed566Sopenharmony_ci * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24d6aed566Sopenharmony_ci * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25d6aed566Sopenharmony_ci * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26d6aed566Sopenharmony_ci * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27d6aed566Sopenharmony_ci * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28d6aed566Sopenharmony_ci * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29d6aed566Sopenharmony_ci * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30d6aed566Sopenharmony_ci */
31d6aed566Sopenharmony_ci
32d6aed566Sopenharmony_ci#include <stdarg.h>
33d6aed566Sopenharmony_ci#include <stdio.h>
34d6aed566Sopenharmony_ci#include "securec.h"
35d6aed566Sopenharmony_ci#include "uart.h"
36d6aed566Sopenharmony_ci#include "los_debug.h"
37d6aed566Sopenharmony_ci#include "los_interrupt.h"
38d6aed566Sopenharmony_ci
39d6aed566Sopenharmony_cistatic void dputs(char const *s, int (*pFputc)(int n, FILE *cookie), void *cookie)
40d6aed566Sopenharmony_ci{
41d6aed566Sopenharmony_ci    unsigned int intSave;
42d6aed566Sopenharmony_ci
43d6aed566Sopenharmony_ci    intSave = LOS_IntLock();
44d6aed566Sopenharmony_ci    while (*s) {
45d6aed566Sopenharmony_ci        pFputc(*s++, cookie);
46d6aed566Sopenharmony_ci    }
47d6aed566Sopenharmony_ci    LOS_IntRestore(intSave);
48d6aed566Sopenharmony_ci}
49d6aed566Sopenharmony_ci
50d6aed566Sopenharmony_ci#ifdef LOSCFG_LIBC_NEWLIB
51d6aed566Sopenharmony_ciint __wrap_printf(char const  *fmt, ...)
52d6aed566Sopenharmony_ci#else /* LOSCFG_LIBC_NEWLIB */
53d6aed566Sopenharmony_ciint printf(char const  *fmt, ...)
54d6aed566Sopenharmony_ci#endif /* LOSCFG_LIBC_NEWLIB */
55d6aed566Sopenharmony_ci{
56d6aed566Sopenharmony_ci#define BUFSIZE 1024  // fit the length of LOG_BUF_SIZE in hiview_log.c
57d6aed566Sopenharmony_ci    char buf[BUFSIZE] = { 0 };
58d6aed566Sopenharmony_ci    va_list ap;
59d6aed566Sopenharmony_ci    va_start(ap, fmt);
60d6aed566Sopenharmony_ci    int len = vsnprintf_s(buf, sizeof(buf), BUFSIZE - 1, fmt, ap);
61d6aed566Sopenharmony_ci    va_end(ap);
62d6aed566Sopenharmony_ci    if (len > 0) {
63d6aed566Sopenharmony_ci        dputs(buf, UartPutc, 0);
64d6aed566Sopenharmony_ci    } else {
65d6aed566Sopenharmony_ci        dputs("printf error!\n", UartPutc, 0);
66d6aed566Sopenharmony_ci    }
67d6aed566Sopenharmony_ci    return len;
68d6aed566Sopenharmony_ci}
69d6aed566Sopenharmony_ci
70d6aed566Sopenharmony_ci#define HDF_KM_LOGV       6
71d6aed566Sopenharmony_ci#define HDF_KM_LOGD       5
72d6aed566Sopenharmony_ci#define HDF_KM_LOGI       4
73d6aed566Sopenharmony_ci#define HDF_KM_LOGW       2
74d6aed566Sopenharmony_ci#define HDF_KM_LOGE       1
75d6aed566Sopenharmony_ci#define HDF_KM_LOG_LEVEL  HDF_KM_LOGW
76d6aed566Sopenharmony_ci
77d6aed566Sopenharmony_ciint hal_trace_printf(int attr, const char *fmt, ...)
78d6aed566Sopenharmony_ci{
79d6aed566Sopenharmony_ci    if (attr > HDF_KM_LOG_LEVEL)
80d6aed566Sopenharmony_ci        return 1;
81d6aed566Sopenharmony_ci
82d6aed566Sopenharmony_ci    char buf[BUFSIZE] = { 0 };
83d6aed566Sopenharmony_ci    va_list ap;
84d6aed566Sopenharmony_ci    va_start(ap, fmt);
85d6aed566Sopenharmony_ci    int len = vsnprintf_s(buf, sizeof(buf), BUFSIZE - 1, fmt, ap);
86d6aed566Sopenharmony_ci    va_end(ap);
87d6aed566Sopenharmony_ci    if (len > 0) {
88d6aed566Sopenharmony_ci        dputs(buf, UartPutc, 0);
89d6aed566Sopenharmony_ci    } else {
90d6aed566Sopenharmony_ci        dputs("printf error!\n", UartPutc, 0);
91d6aed566Sopenharmony_ci    }
92d6aed566Sopenharmony_ci    return len;
93d6aed566Sopenharmony_ci}
94d6aed566Sopenharmony_ci
95d6aed566Sopenharmony_ci/* enable hilog output in LOSCFG_BASE_CORE_HILOG mode */
96d6aed566Sopenharmony_ciint HiLogWriteInternal(const char *buffer, size_t bufLen)
97d6aed566Sopenharmony_ci{
98d6aed566Sopenharmony_ci    const int BUFF_TAIL_LEN = 2;
99d6aed566Sopenharmony_ci
100d6aed566Sopenharmony_ci    if (!buffer) {
101d6aed566Sopenharmony_ci        return -1;
102d6aed566Sopenharmony_ci    }
103d6aed566Sopenharmony_ci
104d6aed566Sopenharmony_ci    // because it's called as HiLogWriteInternal(buf, strlen(buf) + 1)
105d6aed566Sopenharmony_ci    if (bufLen < BUFF_TAIL_LEN) {
106d6aed566Sopenharmony_ci        return 0;
107d6aed566Sopenharmony_ci    }
108d6aed566Sopenharmony_ci
109d6aed566Sopenharmony_ci    if (buffer[bufLen - BUFF_TAIL_LEN] != '\n') {
110d6aed566Sopenharmony_ci        printf("%s\n", buffer);
111d6aed566Sopenharmony_ci    } else {
112d6aed566Sopenharmony_ci        dputs(buffer, UartPutc, 0);
113d6aed566Sopenharmony_ci    }
114d6aed566Sopenharmony_ci    return 0;
115d6aed566Sopenharmony_ci}
116