1 /*
2  * Copyright (C) 2015-2017 Alibaba Group Holding Limited
3  */
4 
5 #ifndef AOS_DEBUG_H
6 #define AOS_DEBUG_H
7 
8 #ifdef __cplusplus
9 extern "C" {
10 #endif
11 
12 #define SHORT_FILE __FILENAME__
13 
14 #define debug_print_assert(A,B,C,D,E,F)
15 
16 #if (!defined(unlikely))
17 #define unlikely(EXPRESSSION) !!(EXPRESSSION)
18 #endif
19 
20 /*
21  * Check that an expression is true (non-zero).
22  * If expression evalulates to false, this prints debugging information (actual expression string, file, line number,
23  * function name, etc.) using the default debugging output method.
24  *
25  * @param[in]  X  expression to be evaluated.
26  */
27 #if (!defined(check))
28 #define check(X)                                                                            \
29         do {                                                                                \
30             if (unlikely(!(X))) {                                                           \
31                 debug_print_assert(0, #X, NULL, SHORT_FILE, __LINE__, __PRETTY_FUNCTION__); \
32             }                                                                               \
33         } while(1 == 0)
34 #endif
35 
36 /*
37  * Check that an expression is true (non-zero) with an explanation.
38  * If expression evalulates to false, this prints debugging information (actual expression string, file, line number,
39  * function name, etc.) using the default debugging output method.
40  *
41  * @param[in]  X       expression to be evaluated.
42  * @param[in]  STR     If the expression evaluate to false, custom string to print.
43  */
44 #if (!defined(check_string))
45 #define check_string(X, STR)                                                               \
46         do {                                                                               \
47             if (unlikely(!(X))) {                                                          \
48                 debug_print_assert(0, #X, STR, SHORT_FILE, __LINE__, __PRETTY_FUNCTION__); \
49                 AOS_ASSERTION_FAIL_ACTION();                                               \
50             }                                                                              \
51         } while(1 == 0)
52 #endif
53 
54 /*
55  * Requires that an expression evaluate to true.
56  * If expression evalulates to false, this prints debugging information (actual expression string, file, line number,
57  * function name, etc.) using the default debugging output method then jumps to a label.
58  *
59  * @param[in]  X      expression to be evalulated.
60  * @param[in]  LABEL  if expression evaluate to false,jumps to the LABEL.
61  */
62 #if (!defined(require))
63 #define require(X, LABEL)                                                                     \
64         do {                                                                                  \
65             if (unlikely(!(X))) {                                                             \
66                 debug_print_assert( 0, #X, NULL, SHORT_FILE, __LINE__, __PRETTY_FUNCTION__ ); \
67                 goto LABEL;                                                                   \
68             }                                                                                 \
69         } while(1 == 0)
70 #endif
71 
72 /*
73  * Requires that an expression evaluate to true with an explanation.
74  * If expression evalulates to false, this prints debugging information (actual expression string, file, line number,
75  * function name, etc.) and a custom explanation string using the default debugging output method then jumps to a label.
76  *
77  * @param[in]  X      expression to be evalulated.
78  * @param[in]  LABEL  if expression evaluate to false,jumps to the LABEL.
79  * @param[in]  STR    if expression evaluate to false,custom explanation string to print.
80  */
81 #if (!defined(require_string))
82 #define require_string(X, LABEL, STR)                                                      \
83         do {                                                                               \
84             if (unlikely(!(X))) {                                                          \
85                 debug_print_assert(0, #X, STR, SHORT_FILE, __LINE__, __PRETTY_FUNCTION__); \
86                 goto LABEL;                                                                \
87             }                                                                              \
88         } while(1 == 0)
89 #endif
90 
91 /*
92  * Requires that an expression evaluate to true.
93  * If expression evalulates to false, this jumps to a label. No debugging information is printed.
94  *
95  * @param[in]  X      expression to be evalulated
96  * @param[in]  LABEL  if expression evaluate to false,jumps to the LABEL.
97  */
98 #if (!defined(require_quiet))
99 #define require_quiet(X, LABEL)   \
100         do {                      \
101             if (unlikely(!(X))) { \
102                 goto LABEL;       \
103             }                     \
104         } while(1 == 0)
105 #endif
106 
107 /*
108  * Require that an error code is noErr (0).
109  * If the error code is non-0, this prints debugging information (actual expression string, file, line number,
110  * function name, etc.) using the default debugging output method then jumps to a label.
111  *
112  * @param[in]  ERR    error to be evaluated
113  * @param[in]  LABEL  If the error code is non-0,jumps to the LABEL.
114  */
115 #if (!defined(require_noerr))
116 #define require_noerr(ERR, LABEL)                                                                    \
117         do {                                                                                         \
118             int localErr;                                                                            \
119                                                                                                      \
120             localErr = (int)(ERR);                                                                   \
121             if (unlikely(localErr != 0)) {                                                           \
122                 debug_print_assert(localErr, NULL, NULL, SHORT_FILE, __LINE__, __PRETTY_FUNCTION__); \
123                 goto LABEL;                                                                          \
124             }                                                                                        \
125                                                                                                      \
126         } while(1 == 0)
127 #endif
128 
129 /*
130  * Require that an error code is noErr (0) with an explanation.
131  * If the error code is non-0, this prints debugging information (actual expression string, file, line number,
132  * function name, etc.), and a custom explanation string using the default debugging output method using the
133  * default debugging output method then jumps to a label.
134  *
135  * @param[in]  ERR    error to be evaluated
136  * @param[in]  LABEL  If the error code is non-0, jumps to the LABEL.
137  * @param[in]  STR    If the error code is non-0, custom explanation string to print
138  */
139 #if (!defined(require_noerr_string))
140 #define require_noerr_string(ERR, LABEL, STR)                                                       \
141         do {                                                                                        \
142             int localErr;                                                                           \
143                                                                                                     \
144             localErr = (int)(ERR);                                                                  \
145             if (unlikely(localErr != 0)) {                                                          \
146                 debug_print_assert(localErr, NULL, STR, SHORT_FILE, __LINE__, __PRETTY_FUNCTION__); \
147                 goto LABEL;                                                                         \
148             }                                                                                       \
149         } while(1 == 0)
150 #endif
151 
152 /*
153  * Require that an error code is noErr (0)  with an explanation and action to execute otherwise.
154  * If the error code is non-0, this prints debugging information (actual expression string, file, line number,
155  * function name, etc.), and a custom explanation string using the default debugging output method using the
156  * default debugging output method then executes an action and jumps to a label.
157  *
158  * @param[in]  ERR     error to be evaluated.
159  * @param[in]  LABEL   If the error code is non-0, jumps to the LABEL.
160  * @param[in]  ACTION  If the error code is non-0, custom code to executes.
161  * @param[in]  STR     If the error code is non-0, custom explanation string to print.
162  */
163 #if (!defined(require_noerr_action_string))
164 #define require_noerr_action_string(ERR, LABEL, ACTION, STR)                                        \
165         do {                                                                                        \
166             int localErr;                                                                           \
167                                                                                                     \
168             localErr = (int)(ERR);                                                                  \
169             if (unlikely(localErr != 0)) {                                                          \
170                 debug_print_assert(localErr, NULL, STR, SHORT_FILE, __LINE__, __PRETTY_FUNCTION__); \
171                 { ACTION; }                                                                         \
172                 goto LABEL;                                                                         \
173             }                                                                                       \
174         } while(1 == 0)
175 #endif
176 
177 /*
178  * Require that an error code is noErr (0).
179  * If the error code is non-0, this jumps to a label. No debugging information is printed.
180  *
181  * @param[in]  ERR    error to be evaluated.
182  * @param[in]  LABEL  If the error code is non-0, jumps to the LABEL.
183  */
184 #if (!defined(require_noerr_quiet))
185 #define require_noerr_quiet(ERR, LABEL) \
186         do {                            \
187             if (unlikely((ERR) != 0)) { \
188                 goto LABEL;             \
189             }                           \
190         } while(1 == 0)
191 #endif
192 
193 /*
194  * Require that an error code is noErr (0) with an action to execute otherwise.
195  * If the error code is non-0, this prints debugging information (actual expression string, file, line number,
196  * function name, etc.) using the default debugging output method then executes an action and jumps to a label.
197  *
198  * @param[in]  ERR     error to be evaluated.
199  * @param[in]  LABEL   If the error code is non-0, jumps to the LABEL.
200  * @param[in]  ACTION  If the error code is non-0, custom code to executes.
201  */
202 #if (!defined(require_noerr_action))
203 #define require_noerr_action(ERR, LABEL, ACTION)                                                     \
204         do {                                                                                         \
205             int localErr;                                                                            \
206                                                                                                      \
207             localErr = (int)(ERR);                                                                   \
208             if (unlikely(localErr != 0)) {                                                           \
209                 debug_print_assert(localErr, NULL, NULL, SHORT_FILE, __LINE__, __PRETTY_FUNCTION__); \
210                 { ACTION; }                                                                          \
211                 goto LABEL;                                                                          \
212             }                                                                                        \
213         } while(1 == 0)
214 #endif
215 
216 /*
217  * Require that an error code is noErr (0) with an action to execute otherwise.
218  * If the error code is non-0, this executes an action and jumps to a label. No debugging information is printed.
219  *
220  * @param[in]  ERR     error to be evaluated.
221  * @param[in]  LABEL   If the error code is non-0, jumps to the LABEL.
222  * @param[in]  ACTION  If the error code is non-0, custom code to executes.
223  */
224 #if (!defined(require_noerr_action_quiet))
225 #define require_noerr_action_quiet(ERR, LABEL, ACTION) \
226         do {                                           \
227             if (unlikely((ERR) != 0)) {                \
228                 { ACTION; }                            \
229                 goto LABEL;                            \
230             }                                          \
231         } while(1 == 0)
232 #endif
233 
234 /*
235  * Requires that an expression evaluate to true with an action to execute otherwise.
236  * If expression evalulates to false, this prints debugging information (actual expression string, file, line number,
237  * function name, etc.) using the default debugging output method then executes an action and jumps to a label.
238  *
239  * @param[in]  X       expression to be evaluated.
240  * @param[in]  LABEL   If the expression evaluate to false, jumps to the LABEL.
241  * @param[in]  ACTION  If the expression evaluate to false, custom code to executes.
242  */
243 #if (!defined(require_action))
244 #define require_action(X, LABEL, ACTION)                                                    \
245         do {                                                                                \
246             if (unlikely(!(X))) {                                                           \
247                 debug_print_assert(0, #X, NULL, SHORT_FILE, __LINE__, __PRETTY_FUNCTION__); \
248                 { ACTION; }                                                                 \
249                 goto LABEL;                                                                 \
250             }                                                                               \
251         } while (1 == 0)
252 #endif
253 
254 /*
255  * Requires that an expression evaluate to true with an explanation and action to execute otherwise.
256  * If expression evalulates to false, this prints debugging information (actual expression string, file, line number,
257  * function name, etc.) and a custom explanation string using the default debugging output method then executes an
258  * action and jumps to a label.
259  *
260  * @param[in]  X       expression to be evaluated.
261  * @param[in]  LABEL   If the expression evaluate to false, jumps to the LABEL.
262  * @param[in]  ACTION  If the expression evaluate to false, custom code to executes.
263  * @param[in]  STR     If the expression evaluate to false, custom string to print.
264  */
265 #if (!defined(require_action_string))
266 #define require_action_string(X, LABEL, ACTION, STR)                                       \
267         do {                                                                               \
268             if (unlikely(!(X))) {                                                          \
269                 debug_print_assert(0, #X, STR, SHORT_FILE, __LINE__, __PRETTY_FUNCTION__); \
270                 { ACTION; }                                                                \
271                 goto LABEL;                                                                \
272             }                                                                              \
273         } while (1 == 0)
274 #endif
275 
276 /*
277  * Requires that an expression evaluate to true with an action to execute otherwise.
278  * If expression evalulates to false, this executes an action and jumps to a label.
279  * No debugging information is printed.
280  *
281  * @param[in]  X       expression to be evaluated.
282  * @param[in]  LABEL   If the expression evaluate to false, jumps to the LABEL.
283  * @param[in]  ACTION  If the expression evaluate to false, custom code to executes.
284  */
285 #if (!defined(require_action_quiet))
286 #define require_action_quiet(X, LABEL, ACTION) \
287         do {                                   \
288             if (unlikely(!(X))) {              \
289                 { ACTION; }                    \
290                 goto LABEL;                    \
291             }                                  \
292                                                \
293         } while(1 == 0)
294 #endif
295 
296 #ifdef __cplusplus
297 }
298 #endif
299 
300 #endif /* AOS_DEBUG_H */
301 
302