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