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 <cinttypes>
20#include <cstdio>
21#include "securec.h"
22
23#ifdef PRINT_HILOG
24#include "avcodec_log.h"
25#define UNITTEST_HILOG(fmt, ...) AVCODEC_LOGI(fmt, ##__VA_ARGS__)
26#else
27#define UNITTEST_HILOG(fmt, ...)
28#endif
29
30namespace OHOS {
31#define LOG_MAX_SIZE 400
32
33#ifdef TEST_ID
34#define PRINT_TEST_LOG(ch)                                                                                             \
35    do {                                                                                                               \
36        UNITTEST_HILOG("[%{public}d] %{public}s", TEST_ID, ch);                                                        \
37        (void)printf("[%s:%d][%d] %s", __func__, __LINE__, TEST_ID, ch);                                               \
38    } while (0)
39#else
40#define PRINT_TEST_LOG(ch)                                                                                             \
41    do {                                                                                                               \
42        UNITTEST_HILOG("%{public}s", ch);                                                                              \
43        (void)printf("[%s:%d] %s", __func__, __LINE__, ch);                                                            \
44    } while (0)
45#endif
46
47#define UNITTEST_CHECK_AND_RETURN_RET_LOG(cond, ret, fmt, ...)                                                         \
48    do {                                                                                                               \
49        if (!(cond)) {                                                                                                 \
50            char ch[LOG_MAX_SIZE];                                                                                     \
51            (void)sprintf_s(ch, LOG_MAX_SIZE, fmt, ##__VA_ARGS__);                                                     \
52            PRINT_TEST_LOG(ch);                                                                                        \
53            (void)printf("\n");                                                                                        \
54            return ret;                                                                                                \
55        }                                                                                                              \
56    } while (0)
57
58#define UNITTEST_CHECK_AND_RETURN_LOG(cond, fmt, ...)                                                                  \
59    do {                                                                                                               \
60        if (!(cond)) {                                                                                                 \
61            char ch[LOG_MAX_SIZE];                                                                                     \
62            (void)sprintf_s(ch, LOG_MAX_SIZE, fmt, ##__VA_ARGS__);                                                     \
63            PRINT_TEST_LOG(ch);                                                                                        \
64            (void)printf("\n");                                                                                        \
65            return;                                                                                                    \
66        }                                                                                                              \
67    } while (0)
68
69#define UNITTEST_CHECK_AND_INFO_LOG(cond, fmt, ...)                                                                    \
70    do {                                                                                                               \
71        if (!(cond)) {                                                                                                 \
72            char ch[LOG_MAX_SIZE];                                                                                     \
73            (void)sprintf_s(ch, LOG_MAX_SIZE, fmt, ##__VA_ARGS__);                                                     \
74            PRINT_TEST_LOG(ch);                                                                                        \
75            (void)printf("\n");                                                                                        \
76        }                                                                                                              \
77    } while (0)
78
79#define UNITTEST_CHECK_AND_BREAK_LOG(cond, fmt, ...)                                                                   \
80    if (!(cond)) {                                                                                                     \
81        char ch[LOG_MAX_SIZE];                                                                                         \
82        (void)sprintf_s(ch, LOG_MAX_SIZE, fmt, ##__VA_ARGS__);                                                         \
83        PRINT_TEST_LOG(ch);                                                                                            \
84        (void)printf("\n");                                                                                            \
85        break;                                                                                                         \
86    }
87
88#define UNITTEST_CHECK_AND_CONTINUE_LOG(cond, fmt, ...)                                                                \
89    if (!(cond)) {                                                                                                     \
90        char ch[LOG_MAX_SIZE];                                                                                         \
91        (void)sprintf_s(ch, LOG_MAX_SIZE, fmt, ##__VA_ARGS__);                                                         \
92        PRINT_TEST_LOG(ch);                                                                                            \
93        (void)printf("\n");                                                                                            \
94        continue;                                                                                                      \
95    }
96
97#define UNITTEST_INFO_LOG(fmt, ...)                                                                                    \
98    do {                                                                                                               \
99        char ch[LOG_MAX_SIZE];                                                                                         \
100        (void)sprintf_s(ch, LOG_MAX_SIZE, fmt, ##__VA_ARGS__);                                                         \
101        PRINT_TEST_LOG(ch);                                                                                            \
102        (void)printf("\n");                                                                                            \
103    } while (0)
104} // namespace OHOS
105
106#endif // UNITTEST_LOG_H