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