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 24 #define LOG_DOMAIN 0xD002B30 25 26 #define AVCODEC_LOG(level, fmt, args...) \ 27 do { \ 28 (void)HILOG_IMPL(LABEL.type, level, LABEL.domain, LABEL.tag, "{%{public}s():%{public}d} " \ 29 fmt, __FUNCTION__, __LINE__, ##args); \ 30 } while (0) 31 32 #define AVCODEC_LOG_LIMIT(logger, frequency, fmt, ...) \ 33 do { \ 34 static uint32_t currentTimes = 0; \ 35 if (currentTimes++ % ((uint32_t)(frequency)) != 0) { \ 36 break; \ 37 } \ 38 logger("[R: %{public}u] " fmt, currentTimes, ##__VA_ARGS__); \ 39 } while (0) 40 41 #define AVCODEC_LOGF(fmt, ...) AVCODEC_LOG(LOG_FATAL, fmt, ##__VA_ARGS__) 42 #define AVCODEC_LOGE(fmt, ...) AVCODEC_LOG(LOG_ERROR, fmt, ##__VA_ARGS__) 43 #define AVCODEC_LOGW(fmt, ...) AVCODEC_LOG(LOG_WARN, fmt, ##__VA_ARGS__) 44 #define AVCODEC_LOGI(fmt, ...) AVCODEC_LOG(LOG_INFO, fmt, ##__VA_ARGS__) 45 #define AVCODEC_LOGD(fmt, ...) AVCODEC_LOG(LOG_DEBUG, fmt, ##__VA_ARGS__) 46 47 #define AVCODEC_LOGE_LIMIT(frequency, fmt, ...) AVCODEC_LOG_LIMIT(AVCODEC_LOGE, frequency, fmt, ##__VA_ARGS__) 48 #define AVCODEC_LOGW_LIMIT(frequency, fmt, ...) AVCODEC_LOG_LIMIT(AVCODEC_LOGW, frequency, fmt, ##__VA_ARGS__) 49 #define AVCODEC_LOGI_LIMIT(frequency, fmt, ...) AVCODEC_LOG_LIMIT(AVCODEC_LOGI, frequency, fmt, ##__VA_ARGS__) 50 #define AVCODEC_LOGD_LIMIT(frequency, fmt, ...) AVCODEC_LOG_LIMIT(AVCODEC_LOGD, frequency, fmt, ##__VA_ARGS__) 51 52 #define CHECK_AND_RETURN_RET_LOG(cond, ret, fmt, ...) \ 53 do { \ 54 if (!(cond)) { \ 55 AVCODEC_LOGE(fmt, ##__VA_ARGS__); \ 56 return ret; \ 57 } \ 58 } while (0) 59 60 #define CHECK_AND_RETURN_RET_LOG_LIMIT(cond, ret, frequency, fmt, ...) \ 61 do { \ 62 if (!(cond)) { \ 63 AVCODEC_LOGE_LIMIT(frequency, fmt, ##__VA_ARGS__); \ 64 return ret; \ 65 } \ 66 } while (0) 67 68 #define EXPECT_AND_LOGW(cond, fmt, ...) \ 69 do { \ 70 if ((cond)) { \ 71 AVCODEC_LOGW(fmt, ##__VA_ARGS__); \ 72 } \ 73 } while (0) 74 75 #define EXPECT_AND_LOGI(cond, fmt, ...) \ 76 do { \ 77 if ((cond)) { \ 78 AVCODEC_LOGI(fmt, ##__VA_ARGS__); \ 79 } \ 80 } while (0) 81 82 #define EXPECT_AND_LOGD(cond, fmt, ...) \ 83 do { \ 84 if ((cond)) { \ 85 AVCODEC_LOGD(fmt, ##__VA_ARGS__); \ 86 } \ 87 } while (0) 88 89 #define CHECK_AND_RETURN_LOG(cond, fmt, ...) \ 90 do { \ 91 if (!(cond)) { \ 92 AVCODEC_LOGE(fmt, ##__VA_ARGS__); \ 93 return; \ 94 } \ 95 } while (0) 96 97 #define CHECK_AND_RETURN_LOG_LIMIT(cond, frequency, fmt, ...) \ 98 do { \ 99 if (!(cond)) { \ 100 AVCODEC_LOGE_LIMIT(frequency, fmt, ##__VA_ARGS__); \ 101 return; \ 102 } \ 103 } while (0) 104 105 #define CHECK_AND_BREAK_LOG(cond, fmt, ...) \ 106 if (1) { \ 107 if (!(cond)) { \ 108 AVCODEC_LOGW(fmt, ##__VA_ARGS__); \ 109 break; \ 110 } \ 111 } else void (0) 112 113 #define CHECK_AND_CONTINUE_LOG(cond, fmt, ...) \ 114 if (1) { \ 115 if (!(cond)) { \ 116 AVCODEC_LOGW(fmt, ##__VA_ARGS__); \ 117 continue; \ 118 } \ 119 } else void (0) 120 121 #define POINTER_MASK 0x00FFFFFF 122 #define FAKE_POINTER(addr) (POINTER_MASK & reinterpret_cast<uintptr_t>(addr)) 123 } // namespace MediaAVCodec 124 } // namespace OHOS 125 #endif // AVCODEC_LOG_H