• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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 #undef LOG_DOMAIN
28 #define LOG_DOMAIN 0xD002B2B
29 #undef LOG_TAG
30 #define LOG_TAG LABEL.tag
31 
32 #define __FILENAME__ (__builtin_strrchr(__FILE__, '/') ? __builtin_strrchr(__FILE__, '/') + 1 : __FILE__)
33 
34 #define MEDIA_LOG(func, fmt, args...)                                                       \
35     do {                                                                                      \
36         if (LABEL.tag != nullptr) {                                                           \
37             (void)func(LOG_CORE, "{%{public}s():%{public}d} " fmt, __FUNCTION__, __LINE__, ##args);  \
38         }                                                                                   \
39     } while (0)
40 
41 #ifdef OHOS_MEDIA_LOG_DFX
42 #define DUMP_LOG(level, fmt, args...)                                                       \
43     do {                                                                                      \
44         (void)OHOS::Media::DfxLogDump::GetInstance().SaveLog(level, LABEL,                    \
45             "{%s():%d} " fmt, __FUNCTION__, __LINE__, ##args);                                \
46     } while (0);
47 #define MEDIA_LOGD(fmt, ...)                                                                  \
48     DUMP_LOG("LOGD", fmt, ##__VA_ARGS__)                                                    \
49     MEDIA_LOG(HILOG_DEBUG, fmt, ##__VA_ARGS__)
50 #define MEDIA_LOGI(fmt, ...)                                                                  \
51     DUMP_LOG("LOGI", fmt, ##__VA_ARGS__)                                                    \
52     MEDIA_LOG(HILOG_INFO, fmt, ##__VA_ARGS__)
53 #define MEDIA_LOGW(fmt, ...)                                                                  \
54     DUMP_LOG("LOGW", fmt, ##__VA_ARGS__)                                                    \
55     MEDIA_LOG(HILOG_WARN, fmt, ##__VA_ARGS__)
56 #define MEDIA_LOGE(fmt, ...)                                                                  \
57     DUMP_LOG("LOGE", fmt, ##__VA_ARGS__)                                                    \
58     MEDIA_LOG(HILOG_ERROR, fmt, ##__VA_ARGS__)
59 #define MEDIA_LOGF(fmt, ...)                                                                  \
60     DUMP_LOG("LOGF", fmt, ##__VA_ARGS__)                                                    \
61     MEDIA_LOG(HILOG_FATAL, fmt, ##__VA_ARGS__)
62 #else
63 #define MEDIA_LOGD(fmt, ...) MEDIA_LOG(HILOG_DEBUG, fmt, ##__VA_ARGS__)
64 #define MEDIA_LOGI(fmt, ...) MEDIA_LOG(HILOG_INFO, fmt, ##__VA_ARGS__)
65 #define MEDIA_LOGW(fmt, ...) MEDIA_LOG(HILOG_WARN, fmt, ##__VA_ARGS__)
66 #define MEDIA_LOGE(fmt, ...) MEDIA_LOG(HILOG_ERROR, fmt, ##__VA_ARGS__)
67 #define MEDIA_LOGF(fmt, ...) MEDIA_LOG(HILOG_FATAL, fmt, ##__VA_ARGS__)
68 #endif
69 
70 #define CHECK_AND_RETURN(cond)                      \
71     do {                                            \
72         if (!(cond)) {                                \
73             MEDIA_LOGE("%{public}s, check failed!", #cond); \
74             return;                                 \
75         }                                           \
76     } while (0)
77 
78 #define CHECK_AND_RETURN_RET(cond, ret)                           \
79     do {                                                          \
80         if (!(cond)) {                                              \
81             MEDIA_LOGE("%{public}s, check failed! ret = %{public}s", #cond, #ret); \
82             return ret;                                           \
83         }                                                         \
84     } while (0)
85 
86 #define CHECK_AND_RETURN_RET_LOG(cond, ret, fmt, ...)  \
87     do {                                               \
88         if (!(cond)) {                                 \
89             MEDIA_LOGE(fmt, ##__VA_ARGS__);            \
90             return ret;                                \
91         }                                              \
92     } while (0);
93 
94 #define CHECK_AND_RETURN_LOG(cond, fmt, ...)           \
95     do {                                               \
96         if (!(cond)) {                                 \
97             MEDIA_LOGE(fmt, ##__VA_ARGS__);            \
98             return;                                    \
99         }                                              \
100     } while (0);
101 
102 #define CHECK_AND_BREAK_LOG(cond, fmt, ...)            \
103     if (1) {                                           \
104         if (!(cond)) {                                 \
105             MEDIA_LOGE(fmt, ##__VA_ARGS__);            \
106             break;                                     \
107         }                                              \
108     } else void (0)
109 
110 #define CHECK_AND_BREAK(cond)                          \
111     if (1) {                                           \
112         if (!(cond)) {                                 \
113             MEDIA_LOGE("%{public}s, check failed!", #cond); \
114             break;                                     \
115         }                                              \
116     } else void (0)
117 
118 #define CHECK_AND_CONTINUE(cond)                          \
119     if (1) {                                           \
120         if (!(cond)) {                                 \
121             MEDIA_LOGE("%{public}s, check failed!", #cond); \
122             continue;                                     \
123         }                                              \
124     } else void (0)
125 
126 #define POINTER_MASK 0x00FFFFFF
127 #define FAKE_POINTER(addr) (POINTER_MASK & reinterpret_cast<uintptr_t>(addr))
128 } // namespace OHOS
129 #endif // OHOS_MEDIA_LOG_H
130