1 /*
2  * Copyright (C) 2023 Huawei Device 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 
16 #ifndef UNITTEST_LOG_H
17 #define UNITTEST_LOG_H
18 
19 #include <cstdio>
20 #include "securec.h"
21 
22 namespace OHOS {
23 #define LOG_MAX_SIZE 200
24 #define UNITTEST_CHECK_AND_RETURN_RET_LOG(cond, ret, fmt, ...)                                                         \
25     do {                                                                                                               \
26         if (!(cond)) {                                                                                                 \
27             char ch[LOG_MAX_SIZE];                                                                                     \
28             (void)sprintf_s(ch, LOG_MAX_SIZE, fmt, ##__VA_ARGS__);                                                     \
29             (void)printf("[%s] %s", __func__, ch);                                                                     \
30             (void)printf("\n");                                                                                        \
31             return ret;                                                                                                \
32         }                                                                                                              \
33     } while (0)
34 
35 #define UNITTEST_CHECK_AND_RETURN_LOG(cond, fmt, ...)                                                                  \
36     do {                                                                                                               \
37         if (!(cond)) {                                                                                                 \
38             char ch[LOG_MAX_SIZE];                                                                                     \
39             (void)sprintf_s(ch, LOG_MAX_SIZE, fmt, ##__VA_ARGS__);                                                     \
40             (void)printf("[%s] %s", __func__, ch);                                                                     \
41             (void)printf("\n");                                                                                        \
42             return;                                                                                                    \
43         }                                                                                                              \
44     } while (0)
45 
46 #define UNITTEST_CHECK_AND_BREAK_LOG(cond, fmt, ...)                                                                   \
47     if (!(cond)) {                                                                                                     \
48         char ch[LOG_MAX_SIZE];                                                                                         \
49         (void)sprintf_s(ch, LOG_MAX_SIZE, fmt, ##__VA_ARGS__);                                                         \
50         (void)printf("[%s] %s", __func__, ch);                                                                         \
51         (void)printf("\n");                                                                                            \
52         break;                                                                                                         \
53     }
54 
55 #define UNITTEST_CHECK_AND_CONTINUE_LOG(cond, fmt, ...)                                                                \
56     if (!(cond)) {                                                                                                     \
57         char ch[LOG_MAX_SIZE];                                                                                         \
58         (void)sprintf_s(ch, LOG_MAX_SIZE, fmt, ##__VA_ARGS__);                                                         \
59         (void)printf("[%s] %s", __func__, ch);                                                                         \
60         (void)printf("\n");                                                                                            \
61         continue;                                                                                                      \
62     }
63 
64 #define UNITTEST_INFO_LOG(fmt, ...)                                                                                    \
65     do {                                                                                                               \
66         char ch[LOG_MAX_SIZE];                                                                                         \
67         (void)sprintf_s(ch, LOG_MAX_SIZE, fmt, ##__VA_ARGS__);                                                         \
68         (void)printf("[%s] %s", __func__, ch);                                                                         \
69         (void)printf("\n");                                                                                            \
70     } while (0)
71 } // namespace OHOS
72 
73 #endif // UNITTEST_LOG_H