1 /*
2  * Copyright (C) 2021-2024 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 OHOS_MEDIA_LOG_H
17 #define OHOS_MEDIA_LOG_H
18 
19 #include <hilog/log.h>
20 #include <cinttypes>
21 
22 #ifdef OHOS_MEDIA_LOG_DFX
23 #include "dfx_log_dump.h"
24 #endif
25 
26 namespace OHOS {
27 
28 #undef LOG_DOMAIN_PLAYER
29 #define LOG_DOMAIN_PLAYER        0xD002B2B
30 #undef LOG_DOMAIN_RECORDER
31 #define LOG_DOMAIN_RECORDER      0xD002B2C
32 #undef LOG_DOMAIN_METADATA
33 #define LOG_DOMAIN_METADATA      0xD002B2D
34 #undef LOG_DOMAIN_SCREENCAPTURE
35 #define LOG_DOMAIN_SCREENCAPTURE 0xD002B2E
36 #undef LOG_DOMAIN_SOUNDPOOL
37 #define LOG_DOMAIN_SOUNDPOOL     0xD002B2F
38 #undef LOG_DOMAIN_AUDIO_NAPI
39 #define LOG_DOMAIN_AUDIO_NAPI    0xD002B20
40 #undef LOG_DOMAIN_VIDEOEDITOR
41 #define LOG_DOMAIN_VIDEOEDITOR    0xD002B3C
42 
43 #undef LOG_TAG
44 #define LOG_TAG LABEL.tag
45 #undef LOG_DOMAIN
46 #define LOG_DOMAIN LABEL.domain
47 #undef LOG_TYPE
48 #define LOG_TYPE LABEL.type
49 
50 #define __FILENAME__ (__builtin_strrchr(__FILE__, '/') ? __builtin_strrchr(__FILE__, '/') + 1 : __FILE__)
51 
52 #define MEDIA_LOG(func, fmt, args...)                                   \
53     do {                                                                \
54         if (LABEL.tag != nullptr) {                                     \
55             (void)func(LOG_TYPE, "#%{public}d " fmt, __LINE__, ##args); \
56         }                                                               \
57     } while (0)
58 
59 #ifdef OHOS_MEDIA_LOG_DFX
60 #define DUMP_LOG(level, fmt, args...)                                                                                \
61     do {                                                                                                             \
62         (void)OHOS::Media::DfxLogDump::GetInstance().SaveLog(level, LABEL, "{%s():%d} " fmt, __FUNCTION__, __LINE__, \
63                                                              ##args);                                                \
64     } while (0);
65 #define MEDIA_LOGD(fmt, ...)             \
66     DUMP_LOG("LOGD", fmt, ##__VA_ARGS__) \
67     MEDIA_LOG(HILOG_DEBUG, fmt, ##__VA_ARGS__)
68 #define MEDIA_LOGI(fmt, ...)             \
69     DUMP_LOG("LOGI", fmt, ##__VA_ARGS__) \
70     MEDIA_LOG(HILOG_INFO, fmt, ##__VA_ARGS__)
71 #define MEDIA_LOGW(fmt, ...)             \
72     DUMP_LOG("LOGW", fmt, ##__VA_ARGS__) \
73     MEDIA_LOG(HILOG_WARN, fmt, ##__VA_ARGS__)
74 #define MEDIA_LOGE(fmt, ...)             \
75     DUMP_LOG("LOGE", fmt, ##__VA_ARGS__) \
76     MEDIA_LOG(HILOG_ERROR, fmt, ##__VA_ARGS__)
77 #define MEDIA_LOGF(fmt, ...)             \
78     DUMP_LOG("LOGF", fmt, ##__VA_ARGS__) \
79     MEDIA_LOG(HILOG_FATAL, fmt, ##__VA_ARGS__)
80 #else
81 #define MEDIA_LOGD(fmt, ...) MEDIA_LOG(HILOG_DEBUG, fmt, ##__VA_ARGS__)
82 #define MEDIA_LOGI(fmt, ...) MEDIA_LOG(HILOG_INFO, fmt, ##__VA_ARGS__)
83 #define MEDIA_LOGW(fmt, ...) MEDIA_LOG(HILOG_WARN, fmt, ##__VA_ARGS__)
84 #define MEDIA_LOGE(fmt, ...) MEDIA_LOG(HILOG_ERROR, fmt, ##__VA_ARGS__)
85 #define MEDIA_LOGF(fmt, ...) MEDIA_LOG(HILOG_FATAL, fmt, ##__VA_ARGS__)
86 #endif
87 
88 #define MEDIA_LOG_PRERELEASE(op, fmt, args...)                         \
89     do {                                                               \
90         op(LOG_ONLY_PRERELEASE, "#%{public}d " fmt, __LINE__, ##args); \
91     } while (0)
92 
93 #define MEDIA_LOGI_NO_RELEASE(fmt, ...) MEDIA_LOG_PRERELEASE(HILOG_INFO, fmt, ##__VA_ARGS__)
94 #define MEDIA_LOGW_NO_RELEASE(fmt, ...) MEDIA_LOG_PRERELEASE(HILOG_WARN, fmt, ##__VA_ARGS__)
95 #define MEDIA_LOGE_NO_RELEASE(fmt, ...) MEDIA_LOG_PRERELEASE(HILOG_ERROR, fmt, ##__VA_ARGS__)
96 #define MEDIA_LOGF_NO_RELEASE(fmt, ...) MEDIA_LOG_PRERELEASE(HILOG_FATAL, fmt, ##__VA_ARGS__)
97 
98 #define CHECK_AND_RETURN(cond)                                         \
99     do {                                                               \
100         if (!(cond)) {                                                 \
101             MEDIA_LOGE_NO_RELEASE("%{public}s, check failed!", #cond); \
102             return;                                                    \
103         }                                                              \
104     } while (0)
105 
106 #define CHECK_AND_RETURN_RET(cond, ret)                                                       \
107     do {                                                                                      \
108         if (!(cond)) {                                                                        \
109             MEDIA_LOGE_NO_RELEASE("%{public}s, check failed! ret = %{public}s", #cond, #ret); \
110             return ret;                                                                       \
111         }                                                                                     \
112     } while (0)
113 
114 #define CHECK_AND_RETURN_RET_LOG(cond, ret, fmt, ...) \
115     do {                                              \
116         if (!(cond)) {                                \
117             MEDIA_LOGE(fmt, ##__VA_ARGS__);           \
118             return ret;                               \
119         }                                             \
120     } while (0)
121 
122 #define CHECK_AND_RETURN_LOG(cond, fmt, ...) \
123     do {                                     \
124         if (!(cond)) {                       \
125             MEDIA_LOGE(fmt, ##__VA_ARGS__);  \
126             return;                          \
127         }                                    \
128     } while (0)
129 
130 #define CHECK_AND_BREAK_LOG(cond, fmt, ...) \
131     if (1) {                                \
132         if (!(cond)) {                      \
133             MEDIA_LOGE(fmt, ##__VA_ARGS__); \
134             break;                          \
135         }                                   \
136     } else                                  \
137         void(0)
138 
139 #define CHECK_AND_BREAK(cond)                                          \
140     if (1) {                                                           \
141         if (!(cond)) {                                                 \
142             MEDIA_LOGE_NO_RELEASE("%{public}s, check failed!", #cond); \
143             break;                                                     \
144         }                                                              \
145     } else                                                             \
146         void(0)
147 
148 #define CHECK_AND_CONTINUE(cond)                                       \
149     if (1) {                                                           \
150         if (!(cond)) {                                                 \
151             MEDIA_LOGE_NO_RELEASE("%{public}s, check failed!", #cond); \
152             continue;                                                  \
153         }                                                              \
154     } else                                                             \
155         void(0)
156 
157 #define CHECK_AND_CONTINUE_LOG(cond, fmt, ...) \
158     if (1) {                                   \
159         if (!(cond)) {                         \
160             MEDIA_LOGE(fmt, ##__VA_ARGS__);    \
161             continue;                          \
162         }                                      \
163     } else                                     \
164         void(0)
165 
166 #define TRUE_LOG(cond, func, fmt, ...)         \
167     if (1) {                                   \
168         if ((cond)) {                          \
169             func(fmt, ##__VA_ARGS__);          \
170         }                                      \
171     } else                                     \
172         void(0)
173 
174 #define POINTER_MASK 0x00FFFFFF
175 #define FAKE_POINTER(addr) (POINTER_MASK & reinterpret_cast<uintptr_t>(addr))
176 }  // namespace OHOS
177 #endif  // OHOS_MEDIA_LOG_H
178