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