summaryrefslogtreecommitdiff
path: root/bsp/bl808/m0/libraries/utils/src/utils_log.c
blob: 73fbeec762c0ae85d2956a5879cc7bb96d04fcd6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*
 * Copyright (c) 2016-2022 Bouffalolab.
 *
 * This file is part of
 *     *** Bouffalolab Software Dev Kit ***
 *      (see www.bouffalolab.com).
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *   1. Redistributions of source code must retain the above copyright notice,
 *      this list of conditions and the following disclaimer.
 *   2. Redistributions in binary form must reproduce the above copyright notice,
 *      this list of conditions and the following disclaimer in the documentation
 *      and/or other materials provided with the distribution.
 *   3. Neither the name of Bouffalo Lab nor the names of its contributors
 *      may be used to endorse or promote products derived from this software
 *      without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
#include <stdio.h>
#include <stdarg.h>
#include <stdint.h>

#include "utils_log.h"

#ifdef __cplusplus
extern "C" {
#endif

#define ONE_LINE_MAX_NUM        (50)/* for print_buf log length */
#define MODULE_LOG_LOCK_LOCK    /* reserved */
#define MODULE_LOG_LOCK_UNLOCK  /* reserved */
static char log_buf[512];
int log_buf_out(const char *file, int line, const void *inbuf, int len, LOG_BUF_OUT_DATA_TYPE_T type)
{
    char *buf = (char *)inbuf;
    char *pbuffer = NULL;

#if defined(BL702) || defined(BL702L)
    pbuffer = (char *)pvPortMalloc(sizeof(log_buf));
    if(pbuffer == NULL){
        return -1;
    }
#else
    pbuffer = (char *)log_buf;
#endif
    int m = 0, n = 0;
    int j = 0, k = 0, tmp = 0;

    MODULE_LOG_LOCK_LOCK;

    tmp = (sizeof(log_buf))/3;/* 数组最大长度 */
    if ((ONE_LINE_MAX_NUM > tmp) || (len < 1))
    {
        MODULE_LOG_LOCK_UNLOCK;
        return -1;
    }

    m = len / ONE_LINE_MAX_NUM;
    n = len % ONE_LINE_MAX_NUM;
    if (n > 0) {
        m++;
    }


    if (n > 0) {
        /* 非整数倍 */
        for (k = 0; k < m; k++) {
            if ((k+1) == m) {
                /* 最后一帧数据 */
                tmp = 0;
                for (j = 0; j < n; j++) {
                    switch (type) {
                        case LOG_BUF_OUT_DATA_TYPE_INT8:
                        {
                            tmp += sprintf(pbuffer + tmp, "%3d ", (int8_t)buf[k*ONE_LINE_MAX_NUM+j]);
                        }
                        break;
                        case LOG_BUF_OUT_DATA_TYPE_UNT8:
                        {
                            tmp += sprintf(pbuffer + tmp, "%3u ", (uint8_t)buf[k*ONE_LINE_MAX_NUM+j]);
                        }
                        break;
                        case LOG_BUF_OUT_DATA_TYPE_HEX:
                        default:
                        {
                            tmp += sprintf(pbuffer + tmp, "%02x ", (uint8_t)buf[k*ONE_LINE_MAX_NUM+j]);
                        }
                    }
                }
                log_buf_pri(file, line, "%.*s\r\n", tmp, pbuffer);
            } else {
                tmp = 0;
                for (j = 0; j < ONE_LINE_MAX_NUM; j++) {
                    switch (type) {
                        case LOG_BUF_OUT_DATA_TYPE_INT8:
                        {
                            tmp += sprintf(pbuffer + tmp, "%3d ", (int8_t)buf[k*ONE_LINE_MAX_NUM+j]);
                        }
                        break;
                        case LOG_BUF_OUT_DATA_TYPE_UNT8:
                        {
                            tmp += sprintf(pbuffer + tmp, "%3u ", (uint8_t)buf[k*ONE_LINE_MAX_NUM+j]);
                        }
                        break;
                        case LOG_BUF_OUT_DATA_TYPE_HEX:
                        default:
                        {
                            tmp += sprintf(pbuffer + tmp, "%02x ", (uint8_t)buf[k*ONE_LINE_MAX_NUM+j]);
                        }
                    }
                }
                log_buf_pri(file, line, "%.*s\r\n", tmp, pbuffer);
            }
        }
    } else {
        /* 整数倍 */
        for (k = 0; k < m; k++) {
            tmp = 0;
            for (j = 0; j < ONE_LINE_MAX_NUM; j++) {
                switch (type) {
                    case LOG_BUF_OUT_DATA_TYPE_INT8:
                    {
                        tmp += sprintf(pbuffer + tmp, "%3d ", (int8_t)buf[k*ONE_LINE_MAX_NUM+j]);
                    }
                    break;
                    case LOG_BUF_OUT_DATA_TYPE_UNT8:
                    {
                        tmp += sprintf(pbuffer + tmp, "%3u ", (uint8_t)buf[k*ONE_LINE_MAX_NUM+j]);
                    }
                    break;
                    case LOG_BUF_OUT_DATA_TYPE_HEX:
                    default:
                    {
                        tmp += sprintf(pbuffer + tmp, "%02x ", (uint8_t)buf[k*ONE_LINE_MAX_NUM+j]);
                    }
                }
            }
            log_buf_pri(file, line, "%.*s\r\n", tmp, pbuffer);
        }
    }

    MODULE_LOG_LOCK_UNLOCK;

#if defined(BL702) || defined(BL702L)
    vPortFree(pbuffer);
#endif

    return 0;
}

extern void vprint(const char *fmt, va_list argp);

extern volatile bool sys_log_all_enable;

void bl_printk(const char *format, ...)
{
    va_list args;

    if (sys_log_all_enable) {
        /* args point to the first variable parameter */
        va_start(args, format);

        /* You can add your code under here. */
        vprint(format, args);

        va_end(args);
    }
}

#ifdef __cplusplus
}
#endif