• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 FRAMEWORK_DFX_LOG_H
17 #define FRAMEWORK_DFX_LOG_H
18 
19 #include <cinttypes>
20 #include <sstream>
21 #include <type_traits>
22 #include <vector>
23 
24 #include "hilog/log.h"
25 
26 namespace OHOS {
27 namespace Media {
28 namespace VideoProcessingEngine {
29 namespace VPELogFlag {
30 extern const bool VERBOSE_LOG;
31 } // VPELogFlag
32 
33 #undef LOG_DOMAIN
34 #define LOG_DOMAIN 0xD002B3F
35 
36 #undef LOG_TAG
37 #define LOG_TAG "VIDEOPROCESSENGINE"
38 #undef LOG_FMT
39 #define LOG_FMT "[%{public}s][%{public}s %{public}d] "
40 
41 #define VPE_LOGF(fmt, ...) HILOG_FATAL(LOG_CORE, LOG_FMT fmt, __FILE_NAME__, __FUNCTION__, __LINE__, ##__VA_ARGS__)
42 #define VPE_LOGE(fmt, ...) HILOG_ERROR(LOG_CORE, LOG_FMT fmt, __FILE_NAME__, __FUNCTION__, __LINE__, ##__VA_ARGS__)
43 #define VPE_LOGW(fmt, ...) HILOG_WARN(LOG_CORE, LOG_FMT fmt, __FILE_NAME__, __FUNCTION__, __LINE__, ##__VA_ARGS__)
44 #define VPE_LOGI(fmt, ...) HILOG_INFO(LOG_CORE, LOG_FMT fmt, __FILE_NAME__, __FUNCTION__, __LINE__, ##__VA_ARGS__)
45 #define VPE_LOGD(fmt, ...) HILOG_DEBUG(LOG_CORE, LOG_FMT fmt, __FILE_NAME__, __FUNCTION__, __LINE__, ##__VA_ARGS__)
46 
47 struct LogInfo {
48     const char* function;
49     uint32_t line;
50 };
51 
52 using LogInfoEx = std::vector<LogInfo>;
53 
GetLogInfoEx(const LogInfo & info)54 inline LogInfoEx GetLogInfoEx(const LogInfo& info)
55 {
56     return std::vector<LogInfo>(1, info);
57 }
58 
AddLogInfoEx(const LogInfoEx & infos,const LogInfo & info)59 inline LogInfoEx AddLogInfoEx(const LogInfoEx& infos, const LogInfo& info)
60 {
61     std::vector<LogInfo> newInfos(infos);
62     newInfos.push_back(info);
63     return newInfos;
64 }
65 
GetLogStack(const LogInfoEx & infos,const LogInfo & info)66 inline std::string GetLogStack(const LogInfoEx& infos, const LogInfo& info)
67 {
68     std::stringstream stream;
69     for (auto& item : infos) {
70         stream << "[" << item.function << " " << item.line << "]";
71     }
72     stream << "[" << info.function << " " << info.line << "]";
73     return stream.str();
74 }
75 
76 #define VPE_LOG_INFO LogInfo{ __FUNCTION__, __LINE__ }
77 #define VPE_LOG_INFO_EX GetLogInfoEx(VPE_LOG_INFO)
78 #define ADD_VPE_LOG_INFO(vec) AddLogInfoEx(vec, VPE_LOG_INFO)
79 
80 #define VPE_ORG_LOG(type, info, fmt, ...) \
81     do { \
82         static_assert(std::is_same<decltype(info), const LogInfo&>::value, "Type mismatch!"); \
83         HiLogPrint(LOG_CORE, type, LOG_DOMAIN, LOG_TAG, LOG_FMT fmt, __FILE_NAME__, info.function, info.line, \
84             ##__VA_ARGS__); \
85     } while (0)
86 
87 #define VPE_EX_LOG(type, infos, info, fmt, ...) \
88     do { \
89         static_assert(std::is_same<decltype(infos), const LogInfoEx&>::value, "Type mismatch!"); \
90         HiLogPrint(LOG_CORE, type, LOG_DOMAIN, LOG_TAG, "[%{public}s]%{public}s" fmt, __FILE_NAME__, \
91             GetLogStack(infos, info).c_str(), ##__VA_ARGS__); \
92     } while (0)
93 
94 #define VPE_ORG_LOGF(info, fmt, ...) VPE_ORG_LOG(LOG_FATAL, info, fmt, ##__VA_ARGS__)
95 #define VPE_ORG_LOGE(info, fmt, ...) VPE_ORG_LOG(LOG_ERROR, info, fmt, ##__VA_ARGS__)
96 #define VPE_ORG_LOGW(info, fmt, ...) VPE_ORG_LOG(LOG_WARN,  info, fmt, ##__VA_ARGS__)
97 #define VPE_ORG_LOGI(info, fmt, ...) VPE_ORG_LOG(LOG_INFO,  info, fmt, ##__VA_ARGS__)
98 #define VPE_ORG_LOGD(info, fmt, ...) VPE_ORG_LOG(LOG_DEBUG, info, fmt, ##__VA_ARGS__)
99 
100 #define VPE_EX_LOGF(infos, fmt, ...) VPE_EX_LOG(LOG_FATAL, infos, VPE_LOG_INFO, fmt, ##__VA_ARGS__)
101 #define VPE_EX_LOGE(infos, fmt, ...) VPE_EX_LOG(LOG_ERROR, infos, VPE_LOG_INFO, fmt, ##__VA_ARGS__)
102 #define VPE_EX_LOGW(infos, fmt, ...) VPE_EX_LOG(LOG_WARN,  infos, VPE_LOG_INFO, fmt, ##__VA_ARGS__)
103 #define VPE_EX_LOGI(infos, fmt, ...) VPE_EX_LOG(LOG_INFO,  infos, VPE_LOG_INFO, fmt, ##__VA_ARGS__)
104 #define VPE_EX_LOGD(infos, fmt, ...) VPE_EX_LOG(LOG_DEBUG, infos, VPE_LOG_INFO, fmt, ##__VA_ARGS__)
105 
106 #define VPE_LOGD_LIMIT(frequency, fmt, ...)                  \
107     do {                                                     \
108         thread_local uint64_t currentTimes = 0;              \
109         if (currentTimes++ % ((uint64_t)(frequency)) == 0) { \
110             VPE_LOGD(fmt, ##__VA_ARGS__);                    \
111         }                                                    \
112     } while (0)
113 
114 #define VPE_LOGV(fmt, ...)            \
115     if (VPELogFlag::VERBOSE_LOG) {    \
116         VPE_LOGD(fmt, ##__VA_ARGS__); \
117     }
118 
119 #define CHECK_AND_RETURN_RET_LOG(cond, ret, fmt, ...) \
120     do {                                              \
121         if (!(cond)) {                                \
122             VPE_LOGE(fmt, ##__VA_ARGS__);             \
123             return ret;                               \
124         }                                             \
125     } while (0)
126 
127 #define CHECK_AND_RETURN_LOG(cond, fmt, ...) \
128     do {                                     \
129         if (!(cond)) {                       \
130             VPE_LOGE(fmt, ##__VA_ARGS__);    \
131             return;                          \
132         }                                    \
133     } while (0)
134 
135 #define CHECK_AND_BREAK_LOG(cond, fmt, ...) \
136     if (1) {                                \
137         if (!(cond)) {                      \
138             VPE_LOGE(fmt, ##__VA_ARGS__);   \
139             break;                          \
140         }                                   \
141     } else                                  \
142         void(0)
143 
144 #define CHECK_AND_CONTINUE_LOG(cond, fmt, ...) \
145     if (1) {                                   \
146         if (!(cond)) {                         \
147             VPE_LOGE(fmt, ##__VA_ARGS__);      \
148             continue;                          \
149         }                                      \
150     } else                                     \
151         void(0)
152 
153 #define CHECK_AND_LOG(cond, fmt, ...) \
154     do {                                     \
155         if (!(cond)) {                       \
156             VPE_LOGE(fmt, ##__VA_ARGS__);    \
157         }                                    \
158     } while (0)
159 
160 #define VPE_LOG_PRINT_COLOR_SPACE_CAPBILITY(CSDesc, pixelFormat)                                           \
161     if (VPELogFlag::VERBOSE_LOG) {                                                                         \
162         VPE_LOGD("Cap: (pri=%{public}3d,trans=%{public}3d,mat=%{public}3d,range=%{public}3d,pixel=%{public}3d)", \
163             (CSDesc).primaries, (CSDesc).transfunc, (CSDesc).matrix, (CSDesc).range, (pixelFormat));             \
164     }
165 
166 #define VPE_LOG_PRINT_METADATA_GEN_CAPBILITY(CSDesc, pixelFormat, algoType)                                    \
167     if (VPELogFlag::VERBOSE_LOG) {                                                                             \
168         VPE_LOGI("Cap: (%{public}3d,%{public}3d,%{public}3d,%{public}3d,%{public}3d, %{public}3d)",            \
169             (CSDesc).primaries, (CSDesc).transfunc, (CSDesc).matrix, (CSDesc).range, (pixelFormat), algoType); \
170     }
171 
172 #define POINTER_MASK 0x00FFFFFF
173 #define FAKE_POINTER(addr) (POINTER_MASK & reinterpret_cast<uintptr_t>(addr))
174 
175 } // namespace VideoProcessingEngine
176 } // namespace Media
177 } // namespace OHOS
178 #endif // FRAMEWORK_DFX_LOG_H
179