1 /* 2 * Copyright (C) 2021-2024 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 28 #undef LOG_DOMAIN_PLAYER 29 #define LOG_DOMAIN_PLAYER 0xD002B2B 30 #undef LOG_DOMAIN_RECORDER 31 #define LOG_DOMAIN_RECORDER 0xD002B2C 32 #undef LOG_DOMAIN_METADATA 33 #define LOG_DOMAIN_METADATA 0xD002B2D 34 #undef LOG_DOMAIN_SCREENCAPTURE 35 #define LOG_DOMAIN_SCREENCAPTURE 0xD002B2E 36 #undef LOG_DOMAIN_SOUNDPOOL 37 #define LOG_DOMAIN_SOUNDPOOL 0xD002B2F 38 #undef LOG_DOMAIN_AUDIO_NAPI 39 #define LOG_DOMAIN_AUDIO_NAPI 0xD002B20 40 #undef LOG_DOMAIN_VIDEOEDITOR 41 #define LOG_DOMAIN_VIDEOEDITOR 0xD002B3C 42 43 #undef LOG_TAG 44 #define LOG_TAG LABEL.tag 45 #undef LOG_DOMAIN 46 #define LOG_DOMAIN LABEL.domain 47 #undef LOG_TYPE 48 #define LOG_TYPE LABEL.type 49 50 #define __FILENAME__ (__builtin_strrchr(__FILE__, '/') ? __builtin_strrchr(__FILE__, '/') + 1 : __FILE__) 51 52 #define MEDIA_LOG(func, fmt, args...) \ 53 do { \ 54 if (LABEL.tag != nullptr) { \ 55 (void)func(LOG_TYPE, "#%{public}d " fmt, __LINE__, ##args); \ 56 } \ 57 } while (0) 58 59 #ifdef OHOS_MEDIA_LOG_DFX 60 #define DUMP_LOG(level, fmt, args...) \ 61 do { \ 62 (void)OHOS::Media::DfxLogDump::GetInstance().SaveLog(level, LABEL, "{%s():%d} " fmt, __FUNCTION__, __LINE__, \ 63 ##args); \ 64 } while (0); 65 #define MEDIA_LOGD(fmt, ...) \ 66 DUMP_LOG("LOGD", fmt, ##__VA_ARGS__) \ 67 MEDIA_LOG(HILOG_DEBUG, fmt, ##__VA_ARGS__) 68 #define MEDIA_LOGI(fmt, ...) \ 69 DUMP_LOG("LOGI", fmt, ##__VA_ARGS__) \ 70 MEDIA_LOG(HILOG_INFO, fmt, ##__VA_ARGS__) 71 #define MEDIA_LOGW(fmt, ...) \ 72 DUMP_LOG("LOGW", fmt, ##__VA_ARGS__) \ 73 MEDIA_LOG(HILOG_WARN, fmt, ##__VA_ARGS__) 74 #define MEDIA_LOGE(fmt, ...) \ 75 DUMP_LOG("LOGE", fmt, ##__VA_ARGS__) \ 76 MEDIA_LOG(HILOG_ERROR, fmt, ##__VA_ARGS__) 77 #define MEDIA_LOGF(fmt, ...) \ 78 DUMP_LOG("LOGF", fmt, ##__VA_ARGS__) \ 79 MEDIA_LOG(HILOG_FATAL, fmt, ##__VA_ARGS__) 80 #else 81 #define MEDIA_LOGD(fmt, ...) MEDIA_LOG(HILOG_DEBUG, fmt, ##__VA_ARGS__) 82 #define MEDIA_LOGI(fmt, ...) MEDIA_LOG(HILOG_INFO, fmt, ##__VA_ARGS__) 83 #define MEDIA_LOGW(fmt, ...) MEDIA_LOG(HILOG_WARN, fmt, ##__VA_ARGS__) 84 #define MEDIA_LOGE(fmt, ...) MEDIA_LOG(HILOG_ERROR, fmt, ##__VA_ARGS__) 85 #define MEDIA_LOGF(fmt, ...) MEDIA_LOG(HILOG_FATAL, fmt, ##__VA_ARGS__) 86 #endif 87 88 #define MEDIA_LOG_PRERELEASE(op, fmt, args...) \ 89 do { \ 90 op(LOG_ONLY_PRERELEASE, "{%{public}s():%{public}d} " fmt, __FUNCTION__, __LINE__, ##args); \ 91 } while (0) 92 93 #define MEDIA_LOGI_NO_RELEASE(fmt, ...) MEDIA_LOG_PRERELEASE(HILOG_INFO, fmt, ##__VA_ARGS__) 94 #define MEDIA_LOGW_NO_RELEASE(fmt, ...) MEDIA_LOG_PRERELEASE(HILOG_WARN, fmt, ##__VA_ARGS__) 95 #define MEDIA_LOGE_NO_RELEASE(fmt, ...) MEDIA_LOG_PRERELEASE(HILOG_ERROR, fmt, ##__VA_ARGS__) 96 #define MEDIA_LOGF_NO_RELEASE(fmt, ...) MEDIA_LOG_PRERELEASE(HILOG_FATAL, fmt, ##__VA_ARGS__) 97 98 #define CHECK_AND_RETURN(cond) \ 99 do { \ 100 if (!(cond)) { \ 101 MEDIA_LOGE_NO_RELEASE("%{public}s, check failed!", #cond); \ 102 return; \ 103 } \ 104 } while (0) 105 106 #define CHECK_AND_RETURN_RET(cond, ret) \ 107 do { \ 108 if (!(cond)) { \ 109 MEDIA_LOGE_NO_RELEASE("%{public}s, check failed! ret = %{public}s", #cond, #ret); \ 110 return ret; \ 111 } \ 112 } while (0) 113 114 #define CHECK_AND_RETURN_RET_NOLOG(cond, ret) \ 115 do { \ 116 if (!(cond)) { \ 117 return ret; \ 118 } \ 119 } while (0) 120 121 #define CHECK_AND_RETURN_RET_LOG(cond, ret, fmt, ...) \ 122 do { \ 123 if (!(cond)) { \ 124 MEDIA_LOGE(fmt, ##__VA_ARGS__); \ 125 return ret; \ 126 } \ 127 } while (0) 128 129 #define CHECK_AND_RETURN_LOG(cond, fmt, ...) \ 130 do { \ 131 if (!(cond)) { \ 132 MEDIA_LOGE(fmt, ##__VA_ARGS__); \ 133 return; \ 134 } \ 135 } while (0) 136 137 #define CHECK_AND_RETURN_NOLOG(cond, ...) \ 138 do { \ 139 if (!(cond)) { \ 140 return; \ 141 } \ 142 } while (0) 143 144 #define CHECK_AND_BREAK_LOG(cond, fmt, ...) \ 145 if (1) { \ 146 if (!(cond)) { \ 147 MEDIA_LOGE(fmt, ##__VA_ARGS__); \ 148 break; \ 149 } \ 150 } else \ 151 void(0) 152 153 #define CHECK_AND_BREAK(cond) \ 154 if (1) { \ 155 if (!(cond)) { \ 156 MEDIA_LOGE_NO_RELEASE("%{public}s, check failed!", #cond); \ 157 break; \ 158 } \ 159 } else \ 160 void(0) 161 162 #define CHECK_AND_CONTINUE(cond) \ 163 if (1) { \ 164 if (!(cond)) { \ 165 continue; \ 166 } \ 167 } else \ 168 void(0) 169 170 #define CHECK_AND_CONTINUE_LOG(cond, fmt, ...) \ 171 if (1) { \ 172 if (!(cond)) { \ 173 MEDIA_LOGE(fmt, ##__VA_ARGS__); \ 174 continue; \ 175 } \ 176 } else \ 177 void(0) 178 179 #define TRUE_LOG(cond, func, fmt, ...) \ 180 if (1) { \ 181 if ((cond)) { \ 182 func(fmt, ##__VA_ARGS__); \ 183 } \ 184 } else \ 185 void(0) 186 187 #define POINTER_MASK 0x00FFFFFF 188 #define FAKE_POINTER(addr) (POINTER_MASK & reinterpret_cast<uintptr_t>(addr)) 189 } // namespace OHOS 190 #endif // OHOS_MEDIA_LOG_H 191