1/*
2 * Copyright (c) 2022 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 */
15
16#ifndef __HI_DEBUG_H__
17#define __HI_DEBUG_H__
18
19#ifndef __KERNEL__
20#include <stdio.h>
21#include <stdarg.h>
22#include <assert.h>
23#endif
24
25#include "hi_type.h"
26#include "hi_common.h"
27
28#ifdef __cplusplus
29#if __cplusplus
30extern "C" {
31#endif
32#endif /* __cplusplus */
33
34#define _EX__FILE_LINE(fxx, lxx) "[File]:" fxx"\n[Line]:"#lxx"\n[Info]:"
35#define EX__FILE_LINE(fxx, lxx) _EX__FILE_LINE(fxx, lxx)
36#define __FILE_LINE__ EX__FILE_LINE(__FILE__, __LINE__)
37
38#define HI_DBG_EMERG      0   /* system is unusable                   */
39#define HI_DBG_ALERT      1   /* action must be taken immediately     */
40#define HI_DBG_CRIT       2   /* critical conditions                  */
41#define HI_DBG_ERR        3   /* error conditions                     */
42#define HI_DBG_WARN       4   /* warning conditions                   */
43#define HI_DBG_NOTICE     5   /* normal but significant condition     */
44#define HI_DBG_INFO       6   /* informational                        */
45#define HI_DBG_DEBUG      7   /* debug-level messages                 */
46
47typedef struct hiLOG_LEVEL_CONF_S {
48    MOD_ID_E  enModId;
49    HI_S32    s32Level;
50    HI_CHAR   cModName[16];
51} LOG_LEVEL_CONF_S;
52
53#ifndef __KERNEL__
54
55/* For User Mode : HI_PRINT, HI_ASSERT, HI_TRACE */
56#define HI_PRINT      (hi_void)printf
57
58/* #ifdef HI_DEBUG */
59#ifdef CONFIG_HI_LOG_TRACE_SUPPORT
60    /* Using samples:   HI_ASSERT(x>y); */
61    #define HI_ASSERT(expr)               \
62    do {                                   \
63        if (!(expr)) {                    \
64            (hi_void)printf("\nASSERT at:\n"       \
65                   "  >Function : %s\n"   \
66                   "  >Line No. : %d\n"   \
67                   "  >Condition: %s\n",  __FUNCTION__, __LINE__, #expr); \
68            assert(0); \
69        } \
70    } while (0)
71
72    /* Using samples:
73     * HI_TRACE(HI_DBG_DEBUG, HI_ID_CMPI, "Test %d, %s\n", 12, "Test");
74     */
75    #define HI_TRACE(level, enModId, fmt...) \
76        do {                                 \
77            if (level <= HI_DBG_ERR)         \
78                (hi_void)fprintf(stderr, ##fmt);       \
79        } while (0)
80
81#else
82    #define HI_ASSERT(expr)
83    #define HI_TRACE(level, enModId, fmt...)
84#endif
85
86#else
87
88/* For Linux Kernel : HI_PRINT, HI_ASSERT, HI_TRACE */
89#define HI_PRINT      (hi_void)osal_printk
90
91int HI_LOG(HI_S32 level, MOD_ID_E enModId, const char *fmt, ...) __attribute__((format(printf, 3, 4)));
92
93/* #ifdef HI_DEBUG */
94#ifdef CONFIG_HI_LOG_TRACE_SUPPORT
95    /* Using samples:   HI_ASSERT(x>y); */
96    #define HI_ASSERT(expr)               \
97    do {                                   \
98        if (!(expr)) {                    \
99            osal_panic("\nASSERT at:\n"   \
100                  "  >Function : %s\n"    \
101                  "  >Line No. : %d\n"    \
102                  "  >Condition: %s\n",   __FUNCTION__, __LINE__, #expr); \
103        } \
104    } while (0)
105
106    /* Using samples:
107     * HI_TRACE(HI_DBG_DEBUG, HI_ID_CMPI, "Test %d, %s\n", 12, "Test");
108     */
109    #define HI_TRACE (hi_void)HI_LOG
110#else
111    #define HI_ASSERT(expr)
112    #define HI_TRACE(level, enModId, fmt...)
113#endif
114
115#endif /* end of __KERNEL__ */
116
117#if (CONFIG_HI_LOG_TRACE_LEVEL >= HI_DBG_EMERG)
118#define HI_EMERG_TRACE(mod, fmt...) HI_TRACE(HI_DBG_EMERG, mod, fmt)
119#else
120#define HI_EMERG_TRACE(mod, fmt...)
121#endif
122
123#if (CONFIG_HI_LOG_TRACE_LEVEL >= HI_DBG_ALERT)
124#define HI_ALERT_TRACE(mod, fmt...) HI_TRACE(HI_DBG_ALERT, mod, fmt)
125#else
126#define HI_ALERT_TRACE(mod, fmt...)
127#endif
128
129#if (CONFIG_HI_LOG_TRACE_LEVEL >= HI_DBG_CRIT)
130#define HI_CRIT_TRACE(mod, fmt...) HI_TRACE(HI_DBG_CRIT, mod, fmt)
131#else
132#define HI_CRIT_TRACE(mod, fmt...)
133#endif
134
135#if (CONFIG_HI_LOG_TRACE_LEVEL >= HI_DBG_ERR)
136#define HI_ERR_TRACE(mod, fmt...) HI_TRACE(HI_DBG_ERR, mod, fmt)
137#else
138#define HI_ERR_TRACE(mod, fmt...)
139#endif
140
141#if (CONFIG_HI_LOG_TRACE_LEVEL >= HI_DBG_WARN)
142#define HI_WARN_TRACE(mod, fmt...) HI_TRACE(HI_DBG_WARN, mod, fmt)
143#else
144#define HI_WARN_TRACE(mod, fmt...)
145#endif
146
147#if (CONFIG_HI_LOG_TRACE_LEVEL >= HI_DBG_NOTICE)
148#define HI_NOTICE_TRACE(mod, fmt...) HI_TRACE(HI_DBG_NOTICE, mod, fmt)
149#else
150#define HI_NOTICE_TRACE(mod, fmt...)
151#endif
152
153#if (CONFIG_HI_LOG_TRACE_LEVEL >= HI_DBG_INFO)
154#define HI_INFO_TRACE(mod, fmt...) HI_TRACE(HI_DBG_INFO, mod, fmt)
155#else
156#define HI_INFO_TRACE(mod, fmt...)
157#endif
158
159#if (CONFIG_HI_LOG_TRACE_LEVEL >= HI_DBG_DEBUG)
160#define HI_DEBUG_TRACE(mod, fmt...) HI_TRACE(HI_DBG_DEBUG, mod, fmt)
161#else
162#define HI_DEBUG_TRACE(mod, fmt...)
163#endif
164
165
166#ifdef __cplusplus
167#if __cplusplus
168}
169#endif
170#endif /* __cplusplus */
171
172#endif /* __HI_DEBUG_H__ */
173
174