1 /* 2 * Copyright (C) 2021 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_MEDIA_LOG_H 17 #define OHOS_MEDIA_LOG_H 18 19 #include <hilog/log.h> 20 #include <cinttypes> 21 22 #ifdef OHOS_MEDIA_LOG_DFX 23 #include "dfx_log_dump.h" 24 #endif 25 26 namespace OHOS { 27 #undef LOG_DOMAIN 28 #define LOG_DOMAIN 0xD002B2B 29 30 #define __FILENAME__ (__builtin_strrchr(__FILE__, '/') ? __builtin_strrchr(__FILE__, '/') + 1 : __FILE__) 31 32 #define MEDIA_LOG(func, fmt, args...) \ 33 do { \ 34 (void)func(LABEL, "{%{public}s():%{public}d} " fmt, __FUNCTION__, __LINE__, ##args); \ 35 } while (0) 36 37 #ifdef OHOS_MEDIA_LOG_DFX 38 #define DUMP_LOG(level, fmt, args...) \ 39 do { \ 40 (void)OHOS::Media::DfxLogDump::GetInstance().SaveLog(level, LABEL, \ 41 "{%s():%d} " fmt, __FUNCTION__, __LINE__, ##args); \ 42 } while (0); 43 #define MEDIA_LOGD(fmt, ...) \ 44 DUMP_LOG("LOGD", fmt, ##__VA_ARGS__) \ 45 MEDIA_LOG(::OHOS::HiviewDFX::HiLog::Debug, fmt, ##__VA_ARGS__) 46 #define MEDIA_LOGI(fmt, ...) \ 47 DUMP_LOG("LOGI", fmt, ##__VA_ARGS__) \ 48 MEDIA_LOG(::OHOS::HiviewDFX::HiLog::Info, fmt, ##__VA_ARGS__) 49 #define MEDIA_LOGW(fmt, ...) \ 50 DUMP_LOG("LOGW", fmt, ##__VA_ARGS__) \ 51 MEDIA_LOG(::OHOS::HiviewDFX::HiLog::Warn, fmt, ##__VA_ARGS__) 52 #define MEDIA_LOGE(fmt, ...) \ 53 DUMP_LOG("LOGE", fmt, ##__VA_ARGS__) \ 54 MEDIA_LOG(::OHOS::HiviewDFX::HiLog::Error, fmt, ##__VA_ARGS__) 55 #define MEDIA_LOGF(fmt, ...) \ 56 DUMP_LOG("LOGF", fmt, ##__VA_ARGS__) \ 57 MEDIA_LOG(::OHOS::HiviewDFX::HiLog::Fatal, fmt, ##__VA_ARGS__) 58 #else 59 #define MEDIA_LOGD(fmt, ...) MEDIA_LOG(::OHOS::HiviewDFX::HiLog::Debug, fmt, ##__VA_ARGS__) 60 #define MEDIA_LOGI(fmt, ...) MEDIA_LOG(::OHOS::HiviewDFX::HiLog::Info, fmt, ##__VA_ARGS__) 61 #define MEDIA_LOGW(fmt, ...) MEDIA_LOG(::OHOS::HiviewDFX::HiLog::Warn, fmt, ##__VA_ARGS__) 62 #define MEDIA_LOGE(fmt, ...) MEDIA_LOG(::OHOS::HiviewDFX::HiLog::Error, fmt, ##__VA_ARGS__) 63 #define MEDIA_LOGF(fmt, ...) MEDIA_LOG(::OHOS::HiviewDFX::HiLog::Fatal, fmt, ##__VA_ARGS__) 64 #endif 65 66 #define CHECK_AND_RETURN(cond) \ 67 do { \ 68 if (!(cond)) { \ 69 MEDIA_LOGE("%{public}s, check failed!", #cond); \ 70 return; \ 71 } \ 72 } while (0) 73 74 #define CHECK_AND_RETURN_RET(cond, ret) \ 75 do { \ 76 if (!(cond)) { \ 77 MEDIA_LOGE("%{public}s, check failed! ret = %{public}s", #cond, #ret); \ 78 return ret; \ 79 } \ 80 } while (0) 81 82 #define CHECK_AND_RETURN_RET_LOG(cond, ret, fmt, ...) \ 83 do { \ 84 if (!(cond)) { \ 85 MEDIA_LOGE(fmt, ##__VA_ARGS__); \ 86 return ret; \ 87 } \ 88 } while (0); 89 90 #define CHECK_AND_RETURN_LOG(cond, fmt, ...) \ 91 do { \ 92 if (!(cond)) { \ 93 MEDIA_LOGE(fmt, ##__VA_ARGS__); \ 94 return; \ 95 } \ 96 } while (0); 97 98 #define CHECK_AND_BREAK_LOG(cond, fmt, ...) \ 99 if (1) { \ 100 if (!(cond)) { \ 101 MEDIA_LOGE(fmt, ##__VA_ARGS__); \ 102 break; \ 103 } \ 104 } else void (0) 105 106 #define CHECK_AND_BREAK(cond) \ 107 if (1) { \ 108 if (!(cond)) { \ 109 MEDIA_LOGE("%{public}s, check failed!", #cond); \ 110 break; \ 111 } \ 112 } else void (0) 113 114 #define CHECK_AND_CONTINUE(cond) \ 115 if (1) { \ 116 if (!(cond)) { \ 117 MEDIA_LOGE("%{public}s, check failed!", #cond); \ 118 continue; \ 119 } \ 120 } else void (0) 121 122 #define POINTER_MASK 0x00FFFFFF 123 #define FAKE_POINTER(addr) (POINTER_MASK & reinterpret_cast<uintptr_t>(addr)) 124 } // namespace OHOS 125 #endif // OHOS_MEDIA_LOG_H 126