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#ifdef MEDIA_OHOS 23#include "hilog/log.h" 24#else 25#include "log_adapter.h" 26#endif 27 28// If file name and line number is need, #define HST_DEBUG at the beginning of the cpp file. 29#define HST_DEBUG 30 31#ifdef MEDIA_OHOS 32#undef LOG_DOMAIN_SYSTEM_PLAYER 33#define LOG_DOMAIN_SYSTEM_PLAYER 0xD002B22 34#undef LOG_DOMAIN_STREAM_SOURCE 35#define LOG_DOMAIN_STREAM_SOURCE 0xD002B23 36#undef LOG_DOMAIN_FOUNDATION 37#define LOG_DOMAIN_FOUNDATION 0xD002B24 38#undef LOG_DOMAIN_DEMUXER 39#define LOG_DOMAIN_DEMUXER 0xD002B3A 40#undef LOG_DOMAIN_MUXER 41#define LOG_DOMAIN_MUXER 0xD002B3B 42#undef LOG_DOMAIN_AUDIO 43#define LOG_DOMAIN_AUDIO 0xD002B31 44#undef LOG_DOMAIN_PLAYER 45#define LOG_DOMAIN_PLAYER 0xD002B2B 46#undef LOG_DOMAIN_RECORDER 47#define LOG_DOMAIN_RECORDER 0xD002B2C 48#undef LOG_DOMAIN_SCREENCAPTURE 49#define LOG_DOMAIN_SCREENCAPTURE 0xD002B2E 50#undef LOG_DOMAIN_HIPLAYER 51#define LOG_DOMAIN_HIPLAYER 0xD002B2D 52#undef LOG_DOMAIN_METADATA 53#define LOG_DOMAIN_METADATA 0xD002B2C 54#define PUBLIC_LOG "%{public}" 55#else 56#define PUBLIC_LOG "%" 57#endif 58 59#ifndef HST_LOG_TAG 60#define HST_LOG_TAG "NULL" 61#endif 62 63#define PUBLIC_LOG_C PUBLIC_LOG "c" 64#define PUBLIC_LOG_S PUBLIC_LOG "s" 65#define PUBLIC_LOG_D8 PUBLIC_LOG PRId8 66#define PUBLIC_LOG_D16 PUBLIC_LOG PRId16 67#define PUBLIC_LOG_D32 PUBLIC_LOG PRId32 68#define PUBLIC_LOG_D64 PUBLIC_LOG PRId64 69#define PUBLIC_LOG_U8 PUBLIC_LOG PRIu8 70#define PUBLIC_LOG_U16 PUBLIC_LOG PRIu16 71#define PUBLIC_LOG_U32 PUBLIC_LOG PRIu32 72#define PUBLIC_LOG_U64 PUBLIC_LOG PRIu64 73#define PUBLIC_LOG_F PUBLIC_LOG "f" 74#define PUBLIC_LOG_P PUBLIC_LOG "p" 75#define PUBLIC_LOG_ZU PUBLIC_LOG "zu" 76 77#undef LOG_TAG 78#define LOG_TAG LABEL.tag 79#undef LOG_DOMAIN 80#define LOG_DOMAIN LABEL.domain 81#undef LOG_TYPE 82#define LOG_TYPE LABEL.type 83 84 85#ifdef MEDIA_OHOS 86#ifndef HST_DEBUG 87#define HST_HILOG(op, fmt, args...) \ 88 do { \ 89 op(LOG_TYPE, PUBLIC_LOG_S ":" fmt, HST_LOG_TAG, ##args); \ 90 } while (0) 91#else 92#define HST_HILOG(op, fmt, args...) \ 93 do { \ 94 op(LOG_TYPE, "(" PUBLIC_LOG_S "(), " PUBLIC_LOG_D32 "): " fmt, __FUNCTION__, __LINE__, ##args); \ 95 } while (0) 96#define HST_HILOG_SHORT(op, fmt, args...) \ 97 do { \ 98 op(LOG_TYPE, "#" PUBLIC_LOG_D32 " " fmt, __LINE__, ##args); \ 99 } while (0) 100#define HST_HILOG_NO_RELEASE(op, fmt, args...) \ 101 do { \ 102 op(LOG_ONLY_PRERELEASE, "(" PUBLIC_LOG_S "(), " PUBLIC_LOG_D32 "): " fmt, __FUNCTION__, __LINE__, ##args); \ 103 } while (0) 104 105#define HST_HILOG_TAG(op, fmt, args...) \ 106 do { \ 107 op(LOG_TYPE, "[" PUBLIC_LOG_S "]:" fmt, HST_LOG_TAG, ##args); \ 108 } while (0) 109 110#define HST_HILOG_WITH_LEVEL_JUDGE(op1, op2, con, fmt, args...) \ 111 do { \ 112 if (!con) { \ 113 op2(LOG_TYPE, "(" PUBLIC_LOG_S "(), " PUBLIC_LOG_D32 "): " fmt, __FUNCTION__, __LINE__, ##args); \ 114 } else { \ 115 op1(LOG_TYPE, "(" PUBLIC_LOG_S "(), " PUBLIC_LOG_D32 "): " fmt, __FUNCTION__, __LINE__, ##args); \ 116 } \ 117 } while (0) 118#endif 119 120#define MEDIA_LOG_D(fmt, ...) HST_HILOG(HILOG_DEBUG, fmt, ##__VA_ARGS__) 121#define MEDIA_LOG_I(fmt, ...) HST_HILOG(HILOG_INFO, fmt, ##__VA_ARGS__) 122#define MEDIA_LOG_W(fmt, ...) HST_HILOG(HILOG_WARN, fmt, ##__VA_ARGS__) 123#define MEDIA_LOG_E(fmt, ...) HST_HILOG(HILOG_ERROR, fmt, ##__VA_ARGS__) 124#define MEDIA_LOG_F(fmt, ...) HST_HILOG(HILOG_FATAL, fmt, ##__VA_ARGS__) 125#define MEDIA_LOG_I_NO_RELEASE(fmt, ...) HST_HILOG_NO_RELEASE(HILOG_INFO, fmt, ##__VA_ARGS__) 126#define MEDIA_LOG_W_NO_RELEASE(fmt, ...) HST_HILOG_NO_RELEASE(HILOG_WARN, fmt, ##__VA_ARGS__) 127#define MEDIA_LOG_E_NO_RELEASE(fmt, ...) HST_HILOG_NO_RELEASE(HILOG_ERROR, fmt, ##__VA_ARGS__) 128#define MEDIA_LOG_F_NO_RELEASE(fmt, ...) HST_HILOG_NO_RELEASE(HILOG_FATAL, fmt, ##__VA_ARGS__) 129#define MEDIA_LOG_D_SHORT(fmt, ...) HST_HILOG_SHORT(HILOG_DEBUG, fmt, ##__VA_ARGS__) 130#define MEDIA_LOG_I_SHORT(fmt, ...) HST_HILOG_SHORT(HILOG_INFO, fmt, ##__VA_ARGS__) 131#define MEDIA_LOG_W_SHORT(fmt, ...) HST_HILOG_SHORT(HILOG_WARN, fmt, ##__VA_ARGS__) 132#define MEDIA_LOG_E_SHORT(fmt, ...) HST_HILOG_SHORT(HILOG_ERROR, fmt, ##__VA_ARGS__) 133#define MEDIA_LOG_F_SHORT(fmt, ...) HST_HILOG_SHORT(HILOG_FATAL, fmt, ##__VA_ARGS__) 134#define MEDIA_LOG_I_FALSE_D(con, fmt, ...) \ 135 HST_HILOG_WITH_LEVEL_JUDGE(HILOG_INFO, HILOG_DEBUG, con, fmt, ##__VA_ARGS__) 136 137#define HST_HILOG_T_WITH_LEVEL_JUDGE(op1, op2, con, fmt, args...) \ 138 do { \ 139 if (!con) { \ 140 op2(LOG_TYPE, "[" PUBLIC_LOG_S "]:" fmt, HST_LOG_TAG, ##args); \ 141 } else { \ 142 op1(LOG_TYPE, "[" PUBLIC_LOG_S "]:" fmt, HST_LOG_TAG, ##args); \ 143 } \ 144 } while (0) 145 146#define MEDIA_LOG_D_T(fmt, ...) HST_HILOG_TAG(HILOG_DEBUG, fmt, ##__VA_ARGS__) 147#define MEDIA_LOG_I_T(fmt, ...) HST_HILOG_TAG(HILOG_INFO, fmt, ##__VA_ARGS__) 148#define MEDIA_LOG_W_T(fmt, ...) HST_HILOG_TAG(HILOG_WARN, fmt, ##__VA_ARGS__) 149#define MEDIA_LOG_E_T(fmt, ...) HST_HILOG_TAG(HILOG_ERROR, fmt, ##__VA_ARGS__) 150#define MEDIA_LOG_F_T(fmt, ...) HST_HILOG_TAG(HILOG_FATAL, fmt, ##__VA_ARGS__) 151#define MEDIA_LOG_I_FALSE_D_T(con, fmt, ...) \ 152 HST_HILOG_T_WITH_LEVEL_JUDGE(HILOG_INFO, HILOG_DEBUG, con, fmt, ##__VA_ARGS__) 153 154#define MEDIA_LOG_LIMIT(op, frequency, fmt, ...) \ 155 do { \ 156 static uint64_t currentTimes = 0; \ 157 if (currentTimes++ % ((uint32_t)(frequency)) == 0) { \ 158 op(fmt, ##__VA_ARGS__); \ 159 } \ 160 } while (0) 161 162#define MEDIA_LOGE_LIMIT(frequency, fmt, ...) MEDIA_LOG_LIMIT(MEDIA_LOG_E, frequency, fmt, ##__VA_ARGS__) 163#define MEDIA_LOGW_LIMIT(frequency, fmt, ...) MEDIA_LOG_LIMIT(MEDIA_LOG_W, frequency, fmt, ##__VA_ARGS__) 164#define MEDIA_LOGI_LIMIT(frequency, fmt, ...) MEDIA_LOG_LIMIT(MEDIA_LOG_I, frequency, fmt, ##__VA_ARGS__) 165#define MEDIA_LOGD_LIMIT(frequency, fmt, ...) MEDIA_LOG_LIMIT(MEDIA_LOG_D, frequency, fmt, ##__VA_ARGS__) 166#endif 167 168// Control the MEDIA_LOG_D. 169// If MEDIA_LOG_D is needed, #define MEDIA_LOG_DEBUG 1 at the beginning of the cpp file. 170#ifndef MEDIA_LOG_DEBUG 171#define MEDIA_LOG_DEBUG 1 172#endif 173 174#if !MEDIA_LOG_DEBUG 175#undef MEDIA_LOG_D 176#define MEDIA_LOG_D(msg, ...) ((void)0) 177#endif 178 179// Control the debug detail logs MEDIA_LOG_DD. 180// If MEDIA_LOG_DD is needed, #define MEDIA_LOG_DEBUG_DETAIL 1 at the beginning of the cpp file. 181#ifndef MEDIA_LOG_DEBUG_DETAIL 182#define MEDIA_LOG_DEBUG_DETAIL 0 183#endif 184 185#if !MEDIA_LOG_DEBUG_DETAIL 186#undef MEDIA_LOG_DD 187#define MEDIA_LOG_DD(msg, ...) ((void)0) 188#else 189#undef MEDIA_LOG_DD 190#define MEDIA_LOG_DD MEDIA_LOG_D 191#endif 192 193#ifndef NOK_RETURN 194#define NOK_RETURN(exec) \ 195 do { \ 196 Status returnValue = (exec); \ 197 if (returnValue != Status::OK) { \ 198 MEDIA_LOG_E("NOK_RETURN on Status(" PUBLIC_LOG_D32 ").", returnValue); \ 199 return returnValue; \ 200 } \ 201 } while (0) 202#endif 203 204#ifndef NOK_LOG 205#define NOK_LOG(exec) \ 206 do { \ 207 Status returnValue = (exec); \ 208 if (returnValue != Status::OK) { \ 209 MEDIA_LOG_E("NOK_LOG on Status(" PUBLIC_LOG_D32 ").", returnValue); \ 210 } \ 211 } while (0) 212#endif 213 214// If exec not return zero, then record the error code, especially when call system C function. 215#ifndef NZERO_LOG 216#define NZERO_LOG(exec) \ 217 do { \ 218 int returnValue = (exec); \ 219 if (returnValue != 0) { \ 220 MEDIA_LOG_E("NZERO_LOG when call (" #exec "), return " PUBLIC_LOG_D32, returnValue); \ 221 } \ 222 } while (0) 223#endif 224 225#ifndef NZERO_RETURN 226#define NZERO_RETURN(exec) \ 227 do { \ 228 int returnValue = (exec); \ 229 if (returnValue != 0) { \ 230 MEDIA_LOG_E("NZERO_RETURN when call (" #exec "), return " PUBLIC_LOG_D32, returnValue); \ 231 return returnValue; \ 232 } \ 233 } while (0) 234#endif 235 236#ifndef NZERO_RETURN_V 237#define NZERO_RETURN_V(exec, ret) \ 238 do { \ 239 int returnValue = (exec); \ 240 if (returnValue != 0) { \ 241 MEDIA_LOG_E("NZERO_RETURN_V when call (" #exec "), return " PUBLIC_LOG_D32, returnValue); \ 242 return ret; \ 243 } \ 244 } while (0) 245#endif 246 247#ifndef FALSE_RETURN 248#define FALSE_RETURN(exec) \ 249 do { \ 250 bool returnValue = (exec); \ 251 if (!returnValue) { \ 252 MEDIA_LOG_E_NO_RELEASE("FALSE_RETURN " #exec); \ 253 return; \ 254 } \ 255 } while (0) 256#endif 257 258#ifndef FALSE_RETURN_NOLOG 259#define FALSE_RETURN_NOLOG(exec) \ 260 do { \ 261 bool returnValue = (exec); \ 262 if (!returnValue) { \ 263 return; \ 264 } \ 265 } while (0) 266#endif 267 268#ifndef FALSE_RETURN_W 269#define FALSE_RETURN_W(exec) \ 270 do { \ 271 bool returnValue = (exec); \ 272 if (!returnValue) { \ 273 MEDIA_LOG_W("FALSE_RETURN " #exec); \ 274 return; \ 275 } \ 276 } while (0) 277#endif 278 279#ifndef FALSE_RETURN_V 280#define FALSE_RETURN_V(exec, ret) \ 281 do { \ 282 bool returnValue = (exec); \ 283 if (!returnValue) { \ 284 MEDIA_LOG_E_NO_RELEASE("FALSE_RETURN_V " #exec); \ 285 return ret; \ 286 } \ 287 } while (0) 288#endif 289 290#ifndef FALSE_RETURN_V_NOLOG 291#define FALSE_RETURN_V_NOLOG(exec, ret) \ 292 do { \ 293 bool returnValue = (exec); \ 294 if (!returnValue) { \ 295 return ret; \ 296 } \ 297 } while (0) 298#endif 299 300#ifndef FALSE_RETURN_V_W 301#define FALSE_RETURN_V_W(exec, ret) \ 302 do { \ 303 bool returnValue = (exec); \ 304 if (!returnValue) { \ 305 MEDIA_LOG_W("FALSE_RETURN_V_W " #exec); \ 306 return ret; \ 307 } \ 308 } while (0) 309#endif 310 311#ifndef FALSE_RETURN_MSG 312#define FALSE_RETURN_MSG(exec, fmt, args...) \ 313 do { \ 314 bool returnValue = (exec); \ 315 if (!returnValue) { \ 316 MEDIA_LOG_E(fmt, ##args); \ 317 return; \ 318 } \ 319 } while (0) 320#endif 321 322#ifndef FALSE_RETURN_V_MSG_IMPL 323#define FALSE_RETURN_V_MSG_IMPL(loglevel, exec, ret, fmt, args...) \ 324 do { \ 325 bool returnValue = (exec); \ 326 if (!returnValue) { \ 327 loglevel(fmt, ##args); \ 328 return ret; \ 329 } \ 330 } while (0) 331#endif 332 333#ifndef FALSE_RETURN_V_MSG 334#define FALSE_RETURN_V_MSG(exec, ret, fmt, args...) FALSE_RETURN_V_MSG_IMPL(MEDIA_LOG_E, exec, ret, fmt, ##args) 335#endif 336 337#ifndef FALSE_RETURN_V_MSG_D 338#define FALSE_RETURN_V_MSG_D(exec, ret, fmt, args...) FALSE_RETURN_V_MSG_IMPL(MEDIA_LOG_D, exec, ret, fmt, ##args) 339#endif 340 341#ifndef FALSE_RETURN_V_MSG_W 342#define FALSE_RETURN_V_MSG_W(exec, ret, fmt, args...) FALSE_RETURN_V_MSG_IMPL(MEDIA_LOG_W, exec, ret, fmt, ##args) 343#endif 344 345#ifndef FALSE_RETURN_V_MSG_E 346#define FALSE_RETURN_V_MSG_E(exec, ret, fmt, args...) FALSE_RETURN_V_MSG_IMPL(MEDIA_LOG_E, exec, ret, fmt, ##args) 347#endif 348 349#ifndef FALSE_LOG 350#define FALSE_LOG(exec) \ 351 do { \ 352 bool returnValue = (exec); \ 353 if (!returnValue) { \ 354 MEDIA_LOG_E("FALSE_LOG: " #exec); \ 355 } \ 356 } while (0) 357#endif 358 359#ifndef FALSE_LOG_MSG_IMPL 360#define FALSE_LOG_MSG_IMPL(loglevel, exec, fmt, args...) \ 361 do { \ 362 bool returnValue = (exec); \ 363 if (!returnValue) { \ 364 loglevel(fmt, ##args); \ 365 } \ 366 } while (0) 367#endif 368 369#ifndef FALSE_LOG_MSG 370#define FALSE_LOG_MSG(exec, fmt, args...) FALSE_LOG_MSG_IMPL(MEDIA_LOG_E, exec, fmt, ##args) 371#endif 372 373#ifndef FALSE_LOG_MSG_W 374#define FALSE_LOG_MSG_W(exec, fmt, args...) FALSE_LOG_MSG_IMPL(MEDIA_LOG_W, exec, fmt, ##args) 375#endif 376 377#define POINTER_MASK 0x00FFFFFF 378#define FAKE_POINTER(addr) (POINTER_MASK & reinterpret_cast<uintptr_t>(addr)) 379#endif // HISTREAMER_FOUNDATION_LOG_H