1/*
2 * Copyright (c) 2022-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 OHOS_DCAMERA_SA_LOG_H
17#define OHOS_DCAMERA_SA_LOG_H
18
19#include "hilog/log.h"
20#include <inttypes.h>
21
22namespace OHOS {
23namespace DistributedHardware {
24#undef LOG_TAG
25#define LOG_TAG "DCAMERA"
26
27#define DCAMERA_FILENAME (__builtin_strrchr(__FILE__, '/') ? __builtin_strrchr(__FILE__, '/') + 1 : __FILE__)
28#define _sl_(x) #x
29#define _strline_(x) _sl_(x)
30#define DCAMERA_STR_LINE _strline_(__LINE__)
31
32#define DHLOGD(fmt, ...) HILOG_DEBUG(LOG_CORE, "[%{public}s][%{public}s][%{public}s:%{public}s]:" fmt, \
33     DH_LOG_TAG, __FUNCTION__, DCAMERA_FILENAME, DCAMERA_STR_LINE, ##__VA_ARGS__)
34
35#define DHLOGI(fmt, ...) HILOG_INFO(LOG_CORE, "[%{public}s][%{public}s][%{public}s:%{public}s]:" fmt, \
36     DH_LOG_TAG, __FUNCTION__, DCAMERA_FILENAME, DCAMERA_STR_LINE, ##__VA_ARGS__)
37
38#define DHLOGW(fmt, ...) HILOG_WARN(LOG_CORE, "[%{public}s][%{public}s][%{public}s:%{public}s]:" fmt, \
39     DH_LOG_TAG, __FUNCTION__, DCAMERA_FILENAME, DCAMERA_STR_LINE, ##__VA_ARGS__)
40
41#define DHLOGE(fmt, ...) HILOG_ERROR(LOG_CORE, "[%{public}s][%{public}s][%{public}s:%{public}s]:" fmt, \
42     DH_LOG_TAG, __FUNCTION__, DCAMERA_FILENAME, DCAMERA_STR_LINE, ##__VA_ARGS__)
43
44#define CHECK_AND_RETURN_RET_LOG(cond, ret, fmt, ...)   \
45    do {                                                \
46        if ((cond)) {                                   \
47            DHLOGE(fmt, ##__VA_ARGS__);                 \
48            return (ret);                               \
49        }                                               \
50    } while (0)
51
52#define CHECK_AND_RETURN_LOG(cond, fmt, ...)   \
53    do {                                       \
54        if ((cond)) {                          \
55            DHLOGE(fmt, ##__VA_ARGS__);        \
56            return;                            \
57        }                                      \
58    } while (0)
59
60#define CHECK_AND_LOG(cond, fmt, ...)          \
61    do {                                       \
62        if ((cond)) {                          \
63            DHLOGE(fmt, ##__VA_ARGS__);        \
64        }                                      \
65    } while (0)
66
67#define CHECK_NULL_RETURN(cond, ret, ...)       \
68    do {                                        \
69        if ((cond)) {                           \
70            return (ret);                       \
71        }                                       \
72    } while (0)
73
74#define CHECK_NULL_FREE_RETURN(ptr, ret, root, ...)    \
75    do {                                               \
76        if ((ptr) == nullptr) {                        \
77            DHLOGE("Address pointer is null");         \
78            cJSON_Delete((root));                      \
79            return (ret);                              \
80        }                                              \
81    } while (0)
82
83#define CHECK_AND_FREE_RETURN_RET_LOG(cond, ret, root, fmt, ...)    \
84    do {                                                            \
85        if ((cond)) {                                               \
86            DHLOGE(fmt, ##__VA_ARGS__);                             \
87            cJSON_Delete((root));                                   \
88            return (ret);                                           \
89        }                                                           \
90    } while (0)
91} // namespace DistributedHardware
92} // namespace OHOS
93#endif // OHOS_DCAMERA_SA_LOG_H
94