1 /* 2 * Copyright (C) 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 #ifndef AVCODEC_LOG_H 16 #define AVCODEC_LOG_H 17 18 #include <hilog/log.h> 19 #include <cinttypes> 20 21 namespace OHOS { 22 namespace MediaAVCodec { 23 #undef LOG_DOMAIN_FRAMEWORK 24 #define LOG_DOMAIN_FRAMEWORK 0xD002B30 25 #undef LOG_DOMAIN_AUDIO 26 #define LOG_DOMAIN_AUDIO 0xD002B31 27 #undef LOG_DOMAIN_HCODEC 28 #define LOG_DOMAIN_HCODEC 0xD002B32 29 #undef LOG_DOMAIN_TEST 30 #define LOG_DOMAIN_TEST 0xD002B36 31 #undef LOG_DOMAIN_DEMUXER 32 #define LOG_DOMAIN_DEMUXER 0xD002B3A 33 #undef LOG_DOMAIN_MUXER 34 #define LOG_DOMAIN_MUXER 0xD002B3B 35 36 #define STRINGFY_INNER(x) #x 37 #define STRINGFY(x) STRINGFY_INNER(x) 38 #define AVCODEC_LOG(level, fmt, args...) \ 39 do { \ 40 (void)HILOG_IMPL(LABEL.type, level, LABEL.domain, LABEL.tag, "{%{public}s():" STRINGFY(__LINE__) "} " fmt, \ 41 __FUNCTION__, ##args); \ 42 } while (0) 43 44 #define AVCODEC_LOGF(fmt, ...) AVCODEC_LOG(LOG_FATAL, fmt, ##__VA_ARGS__) 45 #define AVCODEC_LOGE(fmt, ...) AVCODEC_LOG(LOG_ERROR, fmt, ##__VA_ARGS__) 46 #define AVCODEC_LOGW(fmt, ...) AVCODEC_LOG(LOG_WARN, fmt, ##__VA_ARGS__) 47 #define AVCODEC_LOGI(fmt, ...) AVCODEC_LOG(LOG_INFO, fmt, ##__VA_ARGS__) 48 #define AVCODEC_LOGD(fmt, ...) AVCODEC_LOG(LOG_DEBUG, fmt, ##__VA_ARGS__) 49 50 #define AVCODEC_LOG_LIMIT(logger, frequency, fmt, ...) \ 51 do { \ 52 static uint32_t currentTimes = 0; \ 53 if (currentTimes++ % ((uint32_t)(frequency)) != 0) { \ 54 break; \ 55 } \ 56 logger("[R: %{public}u] " fmt, currentTimes, ##__VA_ARGS__); \ 57 } while (0) 58 59 #define AVCODEC_LOGE_LIMIT(frequency, fmt, ...) AVCODEC_LOG_LIMIT(AVCODEC_LOGE, frequency, fmt, ##__VA_ARGS__) 60 #define AVCODEC_LOGW_LIMIT(frequency, fmt, ...) AVCODEC_LOG_LIMIT(AVCODEC_LOGW, frequency, fmt, ##__VA_ARGS__) 61 #define AVCODEC_LOGI_LIMIT(frequency, fmt, ...) AVCODEC_LOG_LIMIT(AVCODEC_LOGI, frequency, fmt, ##__VA_ARGS__) 62 #define AVCODEC_LOGD_LIMIT(frequency, fmt, ...) AVCODEC_LOG_LIMIT(AVCODEC_LOGD, frequency, fmt, ##__VA_ARGS__) 63 64 #define CHECK_AND_RETURN_RET_LOG(cond, ret, fmt, ...) \ 65 do { \ 66 if (!(cond)) { \ 67 AVCODEC_LOGE(fmt, ##__VA_ARGS__); \ 68 return ret; \ 69 } \ 70 } while (0) 71 72 #define CHECK_AND_RETURN_RET_LOGD(cond, ret, fmt, ...) \ 73 do { \ 74 if (!(cond)) { \ 75 AVCODEC_LOGD(fmt, ##__VA_ARGS__); \ 76 return ret; \ 77 } \ 78 } while (0) 79 80 #define CHECK_AND_RETURN_RET_LOGW(cond, ret, fmt, ...) \ 81 do { \ 82 if (!(cond)) { \ 83 AVCODEC_LOGW(fmt, ##__VA_ARGS__); \ 84 return ret; \ 85 } \ 86 } while (0) 87 88 #define CHECK_AND_RETURN_RET_LOG_LIMIT(cond, ret, frequency, fmt, ...) \ 89 do { \ 90 if (!(cond)) { \ 91 AVCODEC_LOGE_LIMIT(frequency, fmt, ##__VA_ARGS__); \ 92 return ret; \ 93 } \ 94 } while (0) 95 96 #define EXPECT_AND_LOGW(cond, fmt, ...) \ 97 do { \ 98 if ((cond)) { \ 99 AVCODEC_LOGW(fmt, ##__VA_ARGS__); \ 100 } \ 101 } while (0) 102 103 #define EXPECT_AND_LOGI(cond, fmt, ...) \ 104 do { \ 105 if ((cond)) { \ 106 AVCODEC_LOGI(fmt, ##__VA_ARGS__); \ 107 } \ 108 } while (0) 109 110 #define EXPECT_AND_LOGD(cond, fmt, ...) \ 111 do { \ 112 if ((cond)) { \ 113 AVCODEC_LOGD(fmt, ##__VA_ARGS__); \ 114 } \ 115 } while (0) 116 117 #define EXPECT_AND_LOGE(cond, fmt, ...) \ 118 do { \ 119 if ((cond)) { \ 120 AVCODEC_LOGE(fmt, ##__VA_ARGS__); \ 121 } \ 122 } while (0) 123 124 #define CHECK_AND_RETURN_LOG(cond, fmt, ...) \ 125 do { \ 126 if (!(cond)) { \ 127 AVCODEC_LOGE(fmt, ##__VA_ARGS__); \ 128 return; \ 129 } \ 130 } while (0) 131 132 #define CHECK_AND_RETURN_LOGD(cond, fmt, ...) \ 133 do { \ 134 if (!(cond)) { \ 135 AVCODEC_LOGD(fmt, ##__VA_ARGS__); \ 136 return; \ 137 } \ 138 } while (0) 139 140 #define CHECK_AND_RETURN_LOG_LIMIT(cond, frequency, fmt, ...) \ 141 do { \ 142 if (!(cond)) { \ 143 AVCODEC_LOGE_LIMIT(frequency, fmt, ##__VA_ARGS__); \ 144 return; \ 145 } \ 146 } while (0) 147 148 #define CHECK_AND_BREAK_LOG(cond, fmt, ...) \ 149 if (1) { \ 150 if (!(cond)) { \ 151 AVCODEC_LOGW(fmt, ##__VA_ARGS__); \ 152 break; \ 153 } \ 154 } else void (0) 155 156 #define CHECK_AND_CONTINUE_LOG(cond, fmt, ...) \ 157 if (1) { \ 158 if (!(cond)) { \ 159 AVCODEC_LOGW(fmt, ##__VA_ARGS__); \ 160 continue; \ 161 } \ 162 } else void (0) 163 #define POINTER_MASK 0x00FFFFFF 164 #define FAKE_POINTER(addr) (POINTER_MASK & reinterpret_cast<uintptr_t>(addr)) 165 } // namespace MediaAVCodec 166 } // namespace OHOS 167 #endif // AVCODEC_LOG_H