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 #ifndef PASTEBOARD_HILOG_H
16 #define PASTEBOARD_HILOG_H
17 
18 #include "hilog/log.h"
19 
20 namespace OHOS {
21 namespace MiscServices {
22 // param of log interface, such as PASTEBOARD_HILOGF.
23 enum PasteboardSubModule {
24     PASTEBOARD_MODULE_INNERKIT = 0,
25     PASTEBOARD_MODULE_CLIENT,
26     PASTEBOARD_MODULE_SERVICE,
27     PASTEBOARD_MODULE_JAVAKIT, // java kit, defined to avoid repeated use of domain.
28     PASTEBOARD_MODULE_JNI,
29     PASTEBOARD_MODULE_COMMON,
30     PASTEBOARD_MODULE_JS_NAPI,
31     PASTEBOARD_MODULE_CAPI,
32     PASTEBOARD_MODULE_BUTT,
33 };
34 
35 // 0xD001C00: subsystem:PASTEBOARD module:PasteboardManager, 8 bits reserved.
36 static constexpr unsigned int BASE_PASTEBOARD_DOMAIN_ID = 0xD001C00;
37 
38 enum PasteboardDomainId {
39     PASTEBOARD_INNERKIT_DOMAIN = BASE_PASTEBOARD_DOMAIN_ID + PASTEBOARD_MODULE_INNERKIT,
40     PASTEBOARD_CLIENT_DOMAIN,
41     PASTEBOARD_SERVICE_DOMAIN,
42     PASTEBOARD_JAVAKIT_DOMAIN,
43     PASTEBOARD_JNI_DOMAIN,
44     PASTEBOARD_COMMON_DOMAIN,
45     PASTEBOARD_JS_NAPI,
46     PASTEBOARD_CAPI,
47     PASTEBOARD_BUTT,
48 };
49 
50 static constexpr OHOS::HiviewDFX::HiLogLabel PASTEBOARD[PASTEBOARD_MODULE_BUTT] = {
51     { LOG_CORE, PASTEBOARD_INNERKIT_DOMAIN, "PBIK" },
52     { LOG_CORE, PASTEBOARD_CLIENT_DOMAIN, "PBC" },
53     { LOG_CORE, PASTEBOARD_SERVICE_DOMAIN, "PBS" },
54     { LOG_CORE, PASTEBOARD_JAVAKIT_DOMAIN, "PBJK" },
55     { LOG_CORE, PASTEBOARD_JNI_DOMAIN, "PBJN" },
56     { LOG_CORE, PASTEBOARD_COMMON_DOMAIN, "PBCM" },
57     { LOG_CORE, PASTEBOARD_JS_NAPI, "PBJS" },
58     { LOG_CORE, PASTEBOARD_CAPI, "PBCA" },
59 };
60 
61 // In order to improve performance, do not check the module range.
62 // Besides, make sure module is less than PASTEBOARD_MODULE_BUTT.
63 #define PASTEBOARD_HILOGF(module, ...)                                                                    \
64     do {                                                                                                  \
65         if (HiLogIsLoggable(PASTEBOARD[module].domain, PASTEBOARD[module].tag, LOG_FATAL)) {              \
66             ((void)HILOG_IMPL(LOG_CORE, LOG_FATAL, PASTEBOARD[module].domain, PASTEBOARD[module].tag,     \
67                               "%{public}s# " fmt, __FUNCTION__, ##__VA_ARGS__));                          \
68         }                                                                                                 \
69     } while (0)
70 #define PASTEBOARD_HILOGE(module, fmt, ...)                                                               \
71     do {                                                                                                  \
72         if (HiLogIsLoggable(PASTEBOARD[module].domain, PASTEBOARD[module].tag, LOG_ERROR)) {              \
73             ((void)HILOG_IMPL(LOG_CORE, LOG_ERROR, PASTEBOARD[module].domain, PASTEBOARD[module].tag,     \
74                               "%{public}s# " fmt, __FUNCTION__, ##__VA_ARGS__));                          \
75         }                                                                                                 \
76     } while (0)
77 #define PASTEBOARD_HILOGW(module, fmt, ...)                                                               \
78     do {                                                                                                  \
79         if (HiLogIsLoggable(PASTEBOARD[module].domain, PASTEBOARD[module].tag, LOG_WARN)) {               \
80             ((void)HILOG_IMPL(LOG_CORE, LOG_WARN, PASTEBOARD[module].domain, PASTEBOARD[module].tag,      \
81                               "%{public}s# " fmt, __FUNCTION__, ##__VA_ARGS__));                          \
82         }                                                                                                 \
83     } while (0)
84 #define PASTEBOARD_HILOGI(module, fmt, ...)                                                               \
85     do {                                                                                                  \
86         if (HiLogIsLoggable(PASTEBOARD[module].domain, PASTEBOARD[module].tag, LOG_INFO)) {               \
87             ((void)HILOG_IMPL(LOG_CORE, LOG_INFO, PASTEBOARD[module].domain, PASTEBOARD[module].tag,      \
88                               "%{public}s# " fmt, __FUNCTION__, ##__VA_ARGS__));                          \
89         }                                                                                                 \
90     } while (0)
91 #define PASTEBOARD_HILOGD(module, fmt, ...)                                                               \
92     do {                                                                                                  \
93         if (HiLogIsLoggable(PASTEBOARD[module].domain, PASTEBOARD[module].tag, LOG_DEBUG)) {              \
94             ((void)HILOG_IMPL(LOG_CORE, LOG_DEBUG, PASTEBOARD[module].domain, PASTEBOARD[module].tag,     \
95                               "%{public}s# " fmt, __FUNCTION__, ##__VA_ARGS__));                          \
96         }                                                                                                 \
97     } while (0)
98 
99 // check then return ret and print log
100 #define CHECK_AND_RETURN_RET_LOG_INNER(cond, ret, log, label, fmt, ...) \
101     do {                                                                \
102         if (!(cond)) {                                                  \
103             log(label, fmt, ##__VA_ARGS__);                             \
104             return ret;                                                 \
105         }                                                               \
106     } while (0)
107 
108 // check then return and print log
109 #define CHECK_AND_RETURN_LOG_INNER(cond, log, label, fmt, ...) \
110     do {                                                       \
111         if (!(cond)) {                                         \
112             log(label, fmt, ##__VA_ARGS__);                    \
113             return;                                            \
114         }                                                      \
115     } while (0)
116 
117 #define PASTEBOARD_CHECK_AND_RETURN_RET_LOGD(cond, ret, label, fmt, ...) \
118     CHECK_AND_RETURN_RET_LOG_INNER(cond, ret, PASTEBOARD_HILOGD, label, fmt, ##__VA_ARGS__)
119 #define PASTEBOARD_CHECK_AND_RETURN_RET_LOGW(cond, ret, label, fmt, ...) \
120     CHECK_AND_RETURN_RET_LOG_INNER(cond, ret, PASTEBOARD_HILOGW, label, fmt, ##__VA_ARGS__)
121 #define PASTEBOARD_CHECK_AND_RETURN_RET_LOGE(cond, ret, label, fmt, ...) \
122     CHECK_AND_RETURN_RET_LOG_INNER(cond, ret, PASTEBOARD_HILOGE, label, fmt, ##__VA_ARGS__)
123 #define PASTEBOARD_CHECK_AND_RETURN_LOGD(cond, label, fmt, ...) \
124     CHECK_AND_RETURN_LOG_INNER(cond, PASTEBOARD_HILOGD, label, fmt, ##__VA_ARGS__)
125 #define PASTEBOARD_CHECK_AND_RETURN_LOGW(cond, label, fmt, ...) \
126     CHECK_AND_RETURN_LOG_INNER(cond, PASTEBOARD_HILOGW, label, fmt, ##__VA_ARGS__)
127 #define PASTEBOARD_CHECK_AND_RETURN_LOGE(cond, label, fmt, ...) \
128     CHECK_AND_RETURN_LOG_INNER(cond, PASTEBOARD_HILOGE, label, fmt, ##__VA_ARGS__)
129 
130 } // namespace MiscServices
131 } // namespace OHOS
132 
133 #endif // PASTEBOARD_HILOG_H
134