• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-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 HISTREAMER_FOUNDATION_LOG_H
17 #define HISTREAMER_FOUNDATION_LOG_H
18 
19 #include <cinttypes>
20 #include <string>
21 
22 // If file name and line number is need, #define HST_DEBUG at the beginning of the cpp file.
23 #define HST_DEBUG
24 #ifdef HST_DEBUG
HstGetFileName(const std::string & file)25 inline std::string HstGetFileName(const std::string& file)
26 {
27     if (file == "") {
28         return "Unknown File";
29     }
30     return file.substr(file.find_last_of("/\\") + 1);
31 }
32 #endif
33 
34 #ifdef MEDIA_OHOS
35 #ifndef LOG_DOMAIN
36 #define LOG_DOMAIN 0xD002B00
37 #endif
38 #ifndef LOG_TAG
39 #define LOG_TAG "HiStreamer"
40 #endif
41 #include "hilog/log.h"
42 #else
43 #include "log_adapter.h"
44 #endif
45 
46 #ifndef HST_LOG_TAG
47 #define HST_LOG_TAG "NULL"
48 #endif
49 
50 #if defined(MEDIA_OHOS)
51 #define PUBLIC_LOG "%{public}"
52 #else
53 #define PUBLIC_LOG "%"
54 #endif
55 
56 #define PUBLIC_LOG_C PUBLIC_LOG "c"
57 #define PUBLIC_LOG_S PUBLIC_LOG "s"
58 #define PUBLIC_LOG_D8 PUBLIC_LOG PRId8
59 #define PUBLIC_LOG_D16 PUBLIC_LOG PRId16
60 #define PUBLIC_LOG_D32 PUBLIC_LOG PRId32
61 #define PUBLIC_LOG_D64 PUBLIC_LOG PRId64
62 #define PUBLIC_LOG_U8 PUBLIC_LOG PRIu8
63 #define PUBLIC_LOG_U16 PUBLIC_LOG PRIu16
64 #define PUBLIC_LOG_U32 PUBLIC_LOG PRIu32
65 #define PUBLIC_LOG_U64 PUBLIC_LOG PRIu64
66 #define PUBLIC_LOG_F PUBLIC_LOG "f"
67 #define PUBLIC_LOG_P PUBLIC_LOG "p"
68 #define PUBLIC_LOG_ZU PUBLIC_LOG "zu"
69 
70 #ifdef MEDIA_OHOS
71 #ifndef HST_DEBUG
72 #define HST_DECORATOR_HILOG(op, fmt, args...)                                               \
73     do {                                                                                    \
74         op(LOG_CORE, PUBLIC_LOG_S ":" fmt, HST_LOG_TAG, ##args);                            \
75     } while (0)
76 #else
77 #define HST_DECORATOR_HILOG(op, fmt, args...)                                                                          \
78     do {                                                                                                               \
79         op(LOG_CORE, "(" PUBLIC_LOG_S ", " PUBLIC_LOG_D32 "): " fmt,                                                   \
80 		    HstGetFileName(std::string(__FILE__)).c_str(), __LINE__, ##args);                                          \
81     } while (0)
82 #endif
83 
84 #define MEDIA_LOG_D(fmt, ...) HST_DECORATOR_HILOG(HILOG_DEBUG, fmt, ##__VA_ARGS__)
85 #define MEDIA_LOG_I(fmt, ...) HST_DECORATOR_HILOG(HILOG_INFO, fmt, ##__VA_ARGS__)
86 #define MEDIA_LOG_W(fmt, ...) HST_DECORATOR_HILOG(HILOG_WARN, fmt, ##__VA_ARGS__)
87 #define MEDIA_LOG_E(fmt, ...) HST_DECORATOR_HILOG(HILOG_ERROR, fmt, ##__VA_ARGS__)
88 #define MEDIA_LOG_F(fmt, ...) HST_DECORATOR_HILOG(HILOG_FATAL, fmt, ##__VA_ARGS__)
89 #endif
90 
91 
92 // Control the MEDIA_LOG_D.
93 // If MEDIA_LOG_D is needed, #define MEDIA_LOG_DEBUG 1 at the beginning of the cpp file.
94 #ifndef MEDIA_LOG_DEBUG
95 #define MEDIA_LOG_DEBUG 1
96 #endif
97 
98 #if !MEDIA_LOG_DEBUG
99 #undef MEDIA_LOG_D
100 #define MEDIA_LOG_D(msg, ...) ((void)0)
101 #endif
102 
103 // Control the debug detail logs MEDIA_LOG_DD.
104 // If MEDIA_LOG_DD is needed, #define MEDIA_LOG_DEBUG_DETAIL 1 at the beginning of the cpp file.
105 #ifndef MEDIA_LOG_DEBUG_DETAIL
106 #define MEDIA_LOG_DEBUG_DETAIL 0
107 #endif
108 
109 #if !MEDIA_LOG_DEBUG_DETAIL
110 #undef MEDIA_LOG_DD
111 #define MEDIA_LOG_DD(msg, ...) ((void)0)
112 #else
113 #undef MEDIA_LOG_DD
114 #define MEDIA_LOG_DD MEDIA_LOG_D
115 #endif
116 
117 #ifndef NOK_RETURN
118 #define NOK_RETURN(exec)                                                                                               \
119     do {                                                                                                               \
120         Status returnValue = (exec);                                                                                   \
121         if (returnValue != Status::OK) {                                                                               \
122             MEDIA_LOG_E("NOK_RETURN on Status(" PUBLIC_LOG_D32 ").", returnValue);                                     \
123             return returnValue;                                                                                        \
124         }                                                                                                              \
125     } while (0)
126 #endif
127 
128 #ifndef NOK_LOG
129 #define NOK_LOG(exec)                                                                                                  \
130     do {                                                                                                               \
131         Status returnValue = (exec);                                                                                   \
132         if (returnValue != Status::OK) {                                                                               \
133             MEDIA_LOG_E("NOK_LOG on Status(" PUBLIC_LOG_D32 ").", returnValue);                                        \
134         }                                                                                                              \
135     } while (0)
136 #endif
137 
138 // If exec not return zero, then record the error code, especially when call system C function.
139 #ifndef NZERO_LOG
140 #define NZERO_LOG(exec)                                                                                                \
141     do {                                                                                                               \
142         int returnValue = (exec);                                                                                      \
143         if (returnValue != 0) {                                                                                        \
144             MEDIA_LOG_E("NZERO_LOG when call (" #exec "), return " PUBLIC_LOG_D32, returnValue);                       \
145         }                                                                                                              \
146     } while (0)
147 #endif
148 
149 #ifndef NZERO_RETURN
150 #define NZERO_RETURN(exec)                                                                                             \
151     do {                                                                                                               \
152         int returnValue = (exec);                                                                                      \
153         if (returnValue != 0) {                                                                                        \
154             MEDIA_LOG_E("NZERO_RETURN when call (" #exec "), return " PUBLIC_LOG_D32, returnValue);                    \
155             return returnValue;                                                                                        \
156         }                                                                                                              \
157     } while (0)
158 #endif
159 
160 #ifndef NZERO_RETURN_V
161 #define NZERO_RETURN_V(exec, ret)                                                                                      \
162     do {                                                                                                               \
163         int returnValue = (exec);                                                                                      \
164         if (returnValue != 0) {                                                                                        \
165             MEDIA_LOG_E("NZERO_RETURN_V when call (" #exec "), return " PUBLIC_LOG_D32, returnValue);                  \
166             return ret;                                                                                                \
167         }                                                                                                              \
168     } while (0)
169 #endif
170 
171 #ifndef FALSE_RETURN
172 #define FALSE_RETURN(exec)                                                                                             \
173     do {                                                                                                               \
174         bool returnValue = (exec);                                                                                     \
175         if (!returnValue) {                                                                                            \
176             MEDIA_LOG_E("FALSE_RETURN " #exec);                                                                        \
177             return;                                                                                                    \
178         }                                                                                                              \
179     } while (0)
180 #endif
181 
182 #ifndef FALSE_RETURN_W
183 #define FALSE_RETURN_W(exec)                                                                                           \
184     do {                                                                                                               \
185         bool returnValue = (exec);                                                                                     \
186         if (!returnValue) {                                                                                            \
187             MEDIA_LOG_W("FALSE_RETURN " #exec);                                                                        \
188             return;                                                                                                    \
189         }                                                                                                              \
190     } while (0)
191 #endif
192 
193 #ifndef FALSE_RETURN_V
194 #define FALSE_RETURN_V(exec, ret)                                                                                      \
195     do {                                                                                                               \
196         bool returnValue = (exec);                                                                                     \
197         if (!returnValue) {                                                                                            \
198             MEDIA_LOG_E("FALSE_RETURN_V " #exec);                                                                      \
199             return ret;                                                                                                \
200         }                                                                                                              \
201     } while (0)
202 #endif
203 
204 #ifndef FALSE_RETURN_V_W
205 #define FALSE_RETURN_V_W(exec, ret)                                                                                    \
206     do {                                                                                                               \
207         bool returnValue = (exec);                                                                                     \
208         if (!returnValue) {                                                                                            \
209             MEDIA_LOG_W("FALSE_RETURN_V_W " #exec);                                                                    \
210             return ret;                                                                                                \
211         }                                                                                                              \
212     } while (0)
213 #endif
214 
215 #ifndef FALSE_RETURN_MSG
216 #define FALSE_RETURN_MSG(exec, fmt, args...)                                                                           \
217     do {                                                                                                               \
218         bool returnValue = (exec);                                                                                     \
219         if (!returnValue) {                                                                                            \
220             MEDIA_LOG_E(fmt, ##args);                                                                                  \
221             return;                                                                                                    \
222         }                                                                                                              \
223     } while (0)
224 #endif
225 
226 #ifndef FALSE_RETURN_V_MSG_IMPL
227 #define FALSE_RETURN_V_MSG_IMPL(loglevel, exec, ret, fmt, args...)                                                     \
228     do {                                                                                                               \
229         bool returnValue = (exec);                                                                                     \
230         if (!returnValue) {                                                                                            \
231             loglevel(fmt, ##args);                                                                                     \
232             return ret;                                                                                                \
233         }                                                                                                              \
234     } while (0)
235 #endif
236 
237 #ifndef FALSE_RETURN_V_MSG
238 #define FALSE_RETURN_V_MSG(exec, ret, fmt, args...) FALSE_RETURN_V_MSG_IMPL(MEDIA_LOG_E, exec, ret, fmt, ##args)
239 #endif
240 
241 #ifndef FALSE_RETURN_V_MSG_W
242 #define FALSE_RETURN_V_MSG_W(exec, ret, fmt, args...) FALSE_RETURN_V_MSG_IMPL(MEDIA_LOG_W, exec, ret, fmt, ##args)
243 #endif
244 
245 #ifndef FALSE_RETURN_V_MSG_E
246 #define FALSE_RETURN_V_MSG_E(exec, ret, fmt, args...) FALSE_RETURN_V_MSG_IMPL(MEDIA_LOG_E, exec, ret, fmt, ##args)
247 #endif
248 
249 #ifndef FALSE_LOG
250 #define FALSE_LOG(exec)                                                                                                \
251     do {                                                                                                               \
252         bool returnValue = (exec);                                                                                     \
253         if (!returnValue) {                                                                                            \
254             MEDIA_LOG_E("FALSE_LOG: " #exec);                                                                          \
255         }                                                                                                              \
256     } while (0)
257 #endif
258 
259 #ifndef FALSE_LOG_MSG_IMPL
260 #define FALSE_LOG_MSG_IMPL(loglevel, exec, fmt, args...)                                                               \
261     do {                                                                                                               \
262         bool returnValue = (exec);                                                                                     \
263         if (!returnValue) {                                                                                            \
264             loglevel(fmt, ##args);                                                                                     \
265         }                                                                                                              \
266     } while (0)
267 #endif
268 
269 #ifndef FALSE_LOG_MSG
270 #define FALSE_LOG_MSG(exec, fmt, args...) FALSE_LOG_MSG_IMPL(MEDIA_LOG_E, exec, fmt, ##args)
271 #endif
272 
273 #ifndef FALSE_LOG_MSG_W
274 #define FALSE_LOG_MSG_W(exec, fmt, args...) FALSE_LOG_MSG_IMPL(MEDIA_LOG_W, exec, fmt, ##args)
275 #endif
276 
277 #define POINTER_MASK 0x00FFFFFF
278 #define FAKE_POINTER(addr) (POINTER_MASK & reinterpret_cast<uintptr_t>(addr))
279 #endif // HISTREAMER_FOUNDATION_LOG_H