• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 AVCODEC_LOG_LIMIT_POW2(logger, pow2, fmt, ...)                      \
65     do {                                                                    \
66         static uint32_t currentTimes = 0;                                   \
67         if (((currentTimes++) & ((1 << (uint32_t)(pow2)) - 1)) != 0) {      \
68             break;                                                          \
69         }                                                                   \
70         logger("[R: %{public}u] " fmt, currentTimes, ##__VA_ARGS__);        \
71     } while (0)
72 
73 #define AVCODEC_LOGE_LIMIT_POW2(pow2, fmt, ...) AVCODEC_LOG_LIMIT_POW2(AVCODEC_LOGE, pow2, fmt, ##__VA_ARGS__)
74 #define AVCODEC_LOGW_LIMIT_POW2(pow2, fmt, ...) AVCODEC_LOG_LIMIT_POW2(AVCODEC_LOGW, pow2, fmt, ##__VA_ARGS__)
75 #define AVCODEC_LOGI_LIMIT_POW2(pow2, fmt, ...) AVCODEC_LOG_LIMIT_POW2(AVCODEC_LOGI, pow2, fmt, ##__VA_ARGS__)
76 #define AVCODEC_LOGD_LIMIT_POW2(pow2, fmt, ...) AVCODEC_LOG_LIMIT_POW2(AVCODEC_LOGD, pow2, fmt, ##__VA_ARGS__)
77 
78 #define CHECK_AND_RETURN_RET_LOG(cond, ret, fmt, ...)                       \
79     do {                                                                    \
80         if (!(cond)) {                                                      \
81             AVCODEC_LOGE(fmt, ##__VA_ARGS__);                               \
82             return ret;                                                     \
83         }                                                                   \
84     } while (0)
85 
86 #define CHECK_AND_RETURN_RET_LOGD(cond, ret, fmt, ...)                      \
87     do {                                                                    \
88         if (!(cond)) {                                                      \
89             AVCODEC_LOGD(fmt, ##__VA_ARGS__);                               \
90             return ret;                                                     \
91         }                                                                   \
92     } while (0)
93 
94 #define CHECK_AND_RETURN_RET_LOGW(cond, ret, fmt, ...)                      \
95     do {                                                                    \
96         if (!(cond)) {                                                      \
97             AVCODEC_LOGW(fmt, ##__VA_ARGS__);                               \
98             return ret;                                                     \
99         }                                                                   \
100     } while (0)
101 
102 #define CHECK_AND_RETURN_RET_LOG_LIMIT(cond, ret, frequency, fmt, ...)      \
103     do {                                                                    \
104         if (!(cond)) {                                                      \
105             AVCODEC_LOGE_LIMIT(frequency, fmt, ##__VA_ARGS__);              \
106             return ret;                                                     \
107         }                                                                   \
108     } while (0)
109 
110 #define EXPECT_AND_LOGW(cond, fmt, ...)                                     \
111     do {                                                                    \
112         if ((cond)) {                                                       \
113             AVCODEC_LOGW(fmt, ##__VA_ARGS__);                               \
114         }                                                                   \
115     } while (0)
116 
117 #define EXPECT_AND_LOGI(cond, fmt, ...)                                     \
118     do {                                                                    \
119         if ((cond)) {                                                       \
120             AVCODEC_LOGI(fmt, ##__VA_ARGS__);                               \
121         }                                                                   \
122     } while (0)
123 
124 #define EXPECT_AND_LOGD(cond, fmt, ...)                                     \
125     do {                                                                    \
126         if ((cond)) {                                                       \
127             AVCODEC_LOGD(fmt, ##__VA_ARGS__);                               \
128         }                                                                   \
129     } while (0)
130 
131 #define EXPECT_AND_LOGE(cond, fmt, ...)                                     \
132     do {                                                                    \
133         if ((cond)) {                                                       \
134             AVCODEC_LOGE(fmt, ##__VA_ARGS__);                               \
135         }                                                                   \
136     } while (0)
137 
138 #define CHECK_AND_RETURN_LOG(cond, fmt, ...)                                \
139     do {                                                                    \
140         if (!(cond)) {                                                      \
141             AVCODEC_LOGE(fmt, ##__VA_ARGS__);                               \
142             return;                                                         \
143         }                                                                   \
144     } while (0)
145 
146 #define CHECK_AND_RETURN_LOGD(cond, fmt, ...)                               \
147     do {                                                                    \
148         if (!(cond)) {                                                      \
149             AVCODEC_LOGD(fmt, ##__VA_ARGS__);                               \
150             return;                                                         \
151         }                                                                   \
152     } while (0)
153 
154 #define CHECK_AND_RETURN_LOG_LIMIT(cond, frequency, fmt, ...)               \
155     do {                                                                    \
156         if (!(cond)) {                                                      \
157             AVCODEC_LOGE_LIMIT(frequency, fmt, ##__VA_ARGS__);              \
158             return;                                                         \
159         }                                                                   \
160     } while (0)
161 
162 #define CHECK_AND_BREAK_LOG(cond, fmt, ...)                                 \
163     if (1) {                                                                \
164         if (!(cond)) {                                                      \
165             AVCODEC_LOGW(fmt, ##__VA_ARGS__);                               \
166             break;                                                          \
167         }                                                                   \
168     } else void (0)
169 
170 #define CHECK_AND_BREAK_LOG_LIMIT_POW2(cond, pow2, fmt, ...)                \
171     if (!(cond)) {                                                          \
172         AVCODEC_LOGI_LIMIT_POW2(pow2, fmt, ##__VA_ARGS__);                  \
173         break;                                                              \
174     } else void (0)
175 
176 #define CHECK_AND_CONTINUE_LOG(cond, fmt, ...)                              \
177     if (1) {                                                                \
178         if (!(cond)) {                                                      \
179             AVCODEC_LOGW(fmt, ##__VA_ARGS__);                               \
180             continue;                                                       \
181         }                                                                   \
182     } else void (0)
183 #define POINTER_MASK 0x00FFFFFF
184 #define FAKE_POINTER(addr) (POINTER_MASK & reinterpret_cast<uintptr_t>(addr))
185 } // namespace MediaAVCodec
186 } // namespace OHOS
187 #endif // AVCODEC_LOG_H