1 /* 2 * Copyright (c) 2025 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 AVCODEC_DFX_COMPONENT_H 17 #define AVCODEC_DFX_COMPONENT_H 18 19 #include <atomic> 20 #include <cinttypes> 21 #include <memory> 22 #include <mutex> 23 #include <set> 24 #include <string> 25 #include "avcodec_log.h" 26 27 namespace OHOS { 28 namespace Media { 29 class Meta; 30 } 31 namespace MediaAVCodec { 32 class AVCodecDfxComponent { 33 public: 34 AVCodecDfxComponent(); 35 ~AVCodecDfxComponent(); 36 void SetTag(const std::string &str); 37 const std::string &GetTag(); 38 std::atomic<const char *> tag_; 39 40 private: 41 std::string tagContent_ = ""; 42 }; 43 44 std::string CreateVideoLogTag(const OHOS::Media::Meta &meta); 45 46 #define AVCODEC_LOG_WITH_TAG(level, fmt, args...) \ 47 do { \ 48 (void)HILOG_IMPL(LABEL.type, level, LABEL.domain, LABEL.tag, \ 49 "%{public}s{%{public}s():" STRINGFY(__LINE__) "} " fmt, tag_.load(), __FUNCTION__, ##args); \ 50 } while (0) 51 #define AVCODEC_LOGF_WITH_TAG(fmt, ...) AVCODEC_LOG_WITH_TAG(LOG_FATAL, fmt, ##__VA_ARGS__) 52 #define AVCODEC_LOGE_WITH_TAG(fmt, ...) AVCODEC_LOG_WITH_TAG(LOG_ERROR, fmt, ##__VA_ARGS__) 53 #define AVCODEC_LOGW_WITH_TAG(fmt, ...) AVCODEC_LOG_WITH_TAG(LOG_WARN, fmt, ##__VA_ARGS__) 54 #define AVCODEC_LOGI_WITH_TAG(fmt, ...) AVCODEC_LOG_WITH_TAG(LOG_INFO, fmt, ##__VA_ARGS__) 55 #define AVCODEC_LOGD_WITH_TAG(fmt, ...) AVCODEC_LOG_WITH_TAG(LOG_DEBUG, fmt, ##__VA_ARGS__) 56 57 #define CHECK_AND_RETURN_RET_LOG_WITH_TAG(cond, ret, fmt, ...) \ 58 do { \ 59 if (!(cond)) { \ 60 AVCODEC_LOGE_WITH_TAG(fmt, ##__VA_ARGS__); \ 61 return ret; \ 62 } \ 63 } while (0) 64 65 #define CHECK_AND_RETURN_RET_LOGW_WITH_TAG(cond, ret, fmt, ...) \ 66 do { \ 67 if (!(cond)) { \ 68 AVCODEC_LOGW_WITH_TAG(fmt, ##__VA_ARGS__); \ 69 return ret; \ 70 } \ 71 } while (0) 72 73 #define CHECK_AND_RETURN_RET_LOGD_WITH_TAG(cond, ret, fmt, ...) \ 74 do { \ 75 if (!(cond)) { \ 76 AVCODEC_LOGD_WITH_TAG(fmt, ##__VA_ARGS__); \ 77 return ret; \ 78 } \ 79 } while (0) 80 81 #define EXPECT_AND_LOGW_WITH_TAG(cond, fmt, ...) \ 82 do { \ 83 if ((cond)) { \ 84 AVCODEC_LOGW_WITH_TAG(fmt, ##__VA_ARGS__); \ 85 } \ 86 } while (0) 87 88 #define EXPECT_AND_LOGI_WITH_TAG(cond, fmt, ...) \ 89 do { \ 90 if ((cond)) { \ 91 AVCODEC_LOGI_WITH_TAG(fmt, ##__VA_ARGS__); \ 92 } \ 93 } while (0) 94 95 #define EXPECT_AND_LOGD_WITH_TAG(cond, fmt, ...) \ 96 do { \ 97 if ((cond)) { \ 98 AVCODEC_LOGD_WITH_TAG(fmt, ##__VA_ARGS__); \ 99 } \ 100 } while (0) 101 102 #define EXPECT_AND_LOGE_WITH_TAG(cond, fmt, ...) \ 103 do { \ 104 if ((cond)) { \ 105 AVCODEC_LOGE_WITH_TAG(fmt, ##__VA_ARGS__); \ 106 } \ 107 } while (0) 108 109 #define CHECK_AND_RETURN_LOG_WITH_TAG(cond, fmt, ...) \ 110 do { \ 111 if (!(cond)) { \ 112 AVCODEC_LOGE_WITH_TAG(fmt, ##__VA_ARGS__); \ 113 return; \ 114 } \ 115 } while (0) 116 117 #define CHECK_AND_BREAK_LOG_WITH_TAG(cond, fmt, ...) \ 118 if (1) { \ 119 if (!(cond)) { \ 120 AVCODEC_LOGW_WITH_TAG(fmt, ##__VA_ARGS__); \ 121 break; \ 122 } \ 123 } else \ 124 void(0) 125 126 #define CHECK_AND_CONTINUE_LOG_WITH_TAG(cond, fmt, ...) \ 127 if (1) { \ 128 if (!(cond)) { \ 129 AVCODEC_LOGW_WITH_TAG(fmt, ##__VA_ARGS__); \ 130 continue; \ 131 } \ 132 } else \ 133 void(0) 134 } // namespace MediaAVCodec 135 } // namespace OHOS 136 #endif // AVCODEC_DFX_COMPONENT_H 137