1 /*
2  * Copyright (c) 2022 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_DAUDIO_LOG_H
17 #define OHOS_DAUDIO_LOG_H
18 #include "cJSON.h"
19 
20 #include "hilog/log.h"
21 #include <inttypes.h>
22 
23 namespace OHOS {
24 namespace DistributedHardware {
25 #undef LOG_TAG
26 #define LOG_TAG "DAUDIO"
27 
28 #define DHLOGD(fmt, ...) HILOG_DEBUG(LOG_CORE, \
29     "[%{public}s][%{public}s]:" fmt, DH_LOG_TAG, __FUNCTION__, ##__VA_ARGS__)
30 
31 #define DHLOGI(fmt, ...) HILOG_INFO(LOG_CORE, \
32     "[%{public}s][%{public}s]:" fmt, DH_LOG_TAG, __FUNCTION__, ##__VA_ARGS__)
33 
34 #define DHLOGW(fmt, ...) HILOG_WARN(LOG_CORE, \
35     "[%{public}s][%{public}s]:" fmt, DH_LOG_TAG, __FUNCTION__, ##__VA_ARGS__)
36 
37 #define DHLOGE(fmt, ...) HILOG_ERROR(LOG_CORE, \
38     "[%{public}s][%{public}s]:" fmt, DH_LOG_TAG, __FUNCTION__, ##__VA_ARGS__)
39 
40 #define CHECK_NULL_VOID(ptr)                    \
41     do {                                        \
42         if ((ptr) == nullptr) {                 \
43             DHLOGE("Address pointer is null");  \
44             return;                             \
45         }                                       \
46     } while (0)
47 
48 #define CHECK_NULL_AND_FREE_VOID(ptr, root, ...)     \
49     do {                                             \
50         if ((ptr) == nullptr) {                      \
51             DHLOGE("Address pointer is null");       \
52             cJSON_Delete(root);                      \
53             return;                                  \
54         }                                            \
55     } while (0)
56 
57 #define CHECK_NULL_RETURN(ptr, ret)             \
58     do {                                        \
59         if ((ptr) == nullptr) {                 \
60             DHLOGE("Address pointer is null");  \
61             return (ret);                       \
62         }                                       \
63     } while (0)
64 
65 #define CHECK_NULL_FREE_RETURN(ptr, ret, root, ...)    \
66     do {                                               \
67         if ((ptr) == nullptr) {                        \
68             DHLOGE("Address pointer is null");         \
69             cJSON_Delete(root);                        \
70             return (ret);                              \
71         }                                              \
72     } while (0)
73 
74 #define CHECK_AND_RETURN_RET_LOG(cond, ret, fmt, ...)   \
75     do {                                                \
76         if ((cond)) {                                   \
77             DHLOGE(fmt, ##__VA_ARGS__);                 \
78             return (ret);                               \
79         }                                               \
80     } while (0)
81 
82 #define CHECK_AND_FREE_RETURN_RET_LOG(cond, ret, root, fmt, ...)    \
83     do {                                                            \
84         if ((cond)) {                                               \
85             DHLOGE(fmt, ##__VA_ARGS__);                             \
86             cJSON_Delete(root);                                     \
87             return (ret);                                           \
88         }                                                           \
89     } while (0)
90 
91 #define CHECK_AND_FREECHAR_RETURN_RET_LOG(cond, ret, data, fmt, ...)    \
92     do {                                                                \
93         if ((cond)) {                                                   \
94             DHLOGE(fmt, ##__VA_ARGS__);                                 \
95             cJSON_free(data);                                           \
96             return (ret);                                               \
97         }                                                               \
98     } while (0)
99 
100 #define CHECK_AND_RETURN_LOG(cond, fmt, ...)   \
101     do {                                       \
102         if ((cond)) {                          \
103             DHLOGE(fmt, ##__VA_ARGS__);        \
104             return;                            \
105         }                                      \
106     } while (0)
107 
108 #define CHECK_AND_FREE_RETURN_LOG(cond, root, fmt, ...)   \
109     do {                                                  \
110         if ((cond)) {                                     \
111             DHLOGE(fmt, ##__VA_ARGS__);                   \
112             cJSON_Delete(root);                           \
113             return;                                       \
114         }                                                 \
115     } while (0)
116 
117 #define CHECK_AND_LOG(cond, fmt, ...)          \
118     do {                                       \
119         if ((cond)) {                          \
120             DHLOGE(fmt, ##__VA_ARGS__);        \
121         }                                      \
122     } while (0)
123 } // namespace DistributedHardware
124 } // namespace OHOS
125 #endif // OHOS_DAUDIO_LOG_H
126