1cf69771bSopenharmony_ci/*
2cf69771bSopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd.
3cf69771bSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4cf69771bSopenharmony_ci * you may not use this file except in compliance with the License.
5cf69771bSopenharmony_ci * You may obtain a copy of the License at
6cf69771bSopenharmony_ci *
7cf69771bSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8cf69771bSopenharmony_ci *
9cf69771bSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10cf69771bSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11cf69771bSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12cf69771bSopenharmony_ci * See the License for the specific language governing permissions and
13cf69771bSopenharmony_ci * limitations under the License.
14cf69771bSopenharmony_ci */
15cf69771bSopenharmony_ci
16cf69771bSopenharmony_ci#ifndef TIME_HILOG_WRAPPER_H
17cf69771bSopenharmony_ci#define TIME_HILOG_WRAPPER_H
18cf69771bSopenharmony_ci
19cf69771bSopenharmony_ci#include "hilog/log.h"
20cf69771bSopenharmony_ci
21cf69771bSopenharmony_cinamespace OHOS {
22cf69771bSopenharmony_cinamespace MiscServices {
23cf69771bSopenharmony_ci// param of log interface, such as TIME_HILOGF.
24cf69771bSopenharmony_cienum TimeSubModule {
25cf69771bSopenharmony_ci    TIME_MODULE_INNERKIT = 0,
26cf69771bSopenharmony_ci    TIME_MODULE_CLIENT,
27cf69771bSopenharmony_ci    TIME_MODULE_SERVICE,
28cf69771bSopenharmony_ci    TIME_MODULE_JAVAKIT, // java kit, defined to avoid repeated use of domain.
29cf69771bSopenharmony_ci    TIME_MODULE_JNI,
30cf69771bSopenharmony_ci    TIME_MODULE_COMMON,
31cf69771bSopenharmony_ci    TIME_MODULE_JS_NAPI,
32cf69771bSopenharmony_ci    TIME_MODULE_BUTT,
33cf69771bSopenharmony_ci};
34cf69771bSopenharmony_ci
35cf69771bSopenharmony_cistatic constexpr unsigned int BASE_TIME_DOMAIN_ID = 0xD001C40;
36cf69771bSopenharmony_ci
37cf69771bSopenharmony_cienum TimeDomainId {
38cf69771bSopenharmony_ci    TIME_INNERKIT_DOMAIN = BASE_TIME_DOMAIN_ID + TIME_MODULE_INNERKIT,
39cf69771bSopenharmony_ci    TIME_CLIENT_DOMAIN,
40cf69771bSopenharmony_ci    TIME_SERVICE_DOMAIN,
41cf69771bSopenharmony_ci    TIME_JAVAKIT_DOMAIN,
42cf69771bSopenharmony_ci    TIME_JNI_DOMAIN,
43cf69771bSopenharmony_ci    TIME_COMMON_DOMAIN,
44cf69771bSopenharmony_ci    TIME_JS_NAPI,
45cf69771bSopenharmony_ci    TIME_BUTT,
46cf69771bSopenharmony_ci};
47cf69771bSopenharmony_ci
48cf69771bSopenharmony_cistatic constexpr OHOS::HiviewDFX::HiLogLabel TIME_MODULE_LABEL[TIME_MODULE_BUTT] = {
49cf69771bSopenharmony_ci    { LOG_CORE, TIME_INNERKIT_DOMAIN, "TimeInnerKit" },
50cf69771bSopenharmony_ci    { LOG_CORE, TIME_CLIENT_DOMAIN, "TimeClient" },
51cf69771bSopenharmony_ci    { LOG_CORE, TIME_SERVICE_DOMAIN, "TimeService" },
52cf69771bSopenharmony_ci    { LOG_CORE, TIME_JAVAKIT_DOMAIN, "TimeJavaKit" },
53cf69771bSopenharmony_ci    { LOG_CORE, TIME_JNI_DOMAIN, "TimeJni" },
54cf69771bSopenharmony_ci    { LOG_CORE, TIME_COMMON_DOMAIN, "TimeCommon" },
55cf69771bSopenharmony_ci    { LOG_CORE, TIME_JS_NAPI, "TimeJSNAPI" },
56cf69771bSopenharmony_ci};
57cf69771bSopenharmony_ci
58cf69771bSopenharmony_ci#define R_FORMATED(fmt, ...) "%{public}s# " fmt, __FUNCTION__, ##__VA_ARGS__
59cf69771bSopenharmony_ci
60cf69771bSopenharmony_ci// In order to improve performance, do not check the module range.
61cf69771bSopenharmony_ci// Besides, make sure module is less than TIME_MODULE_BUTT.
62cf69771bSopenharmony_ci#define TIME_HILOGF(module, fmt, ...) (void)HILOG_IMPL(LOG_CORE, LOG_FATAL, TIME_MODULE_LABEL[module].domain, \
63cf69771bSopenharmony_ci    TIME_MODULE_LABEL[module].tag, "%{public}s# " fmt, __FUNCTION__, ##__VA_ARGS__)
64cf69771bSopenharmony_ci#define TIME_HILOGE(module, fmt, ...) (void)HILOG_IMPL(LOG_CORE, LOG_ERROR, TIME_MODULE_LABEL[module].domain, \
65cf69771bSopenharmony_ci    TIME_MODULE_LABEL[module].tag, "%{public}s# " fmt, __FUNCTION__, ##__VA_ARGS__)
66cf69771bSopenharmony_ci#define TIME_HILOGW(module, fmt, ...) (void)HILOG_IMPL(LOG_CORE, LOG_WARN, TIME_MODULE_LABEL[module].domain, \
67cf69771bSopenharmony_ci    TIME_MODULE_LABEL[module].tag, "%{public}s# " fmt, __FUNCTION__, ##__VA_ARGS__)
68cf69771bSopenharmony_ci#define TIME_HILOGI(module, fmt, ...) (void)HILOG_IMPL(LOG_CORE, LOG_INFO, TIME_MODULE_LABEL[module].domain, \
69cf69771bSopenharmony_ci    TIME_MODULE_LABEL[module].tag, "%{public}s# " fmt, __FUNCTION__, ##__VA_ARGS__)
70cf69771bSopenharmony_ci#define TIME_HILOGD(module, fmt, ...) (void)HILOG_IMPL(LOG_CORE, LOG_DEBUG, TIME_MODULE_LABEL[module].domain, \
71cf69771bSopenharmony_ci    TIME_MODULE_LABEL[module].tag, "%{public}s# " fmt, __FUNCTION__, ##__VA_ARGS__)
72cf69771bSopenharmony_ci#define TIME_SIMPLIFY_HILOGI(module, fmt, ...) (void)HILOG_IMPL(LOG_CORE, LOG_INFO, TIME_MODULE_LABEL[module].domain, \
73cf69771bSopenharmony_ci    TIME_MODULE_LABEL[module].tag, fmt, ##__VA_ARGS__)
74cf69771bSopenharmony_ci
75cf69771bSopenharmony_ci#define CHECK_AND_RETURN_RET_LOG(module, cond, ret, ...)  \
76cf69771bSopenharmony_ci    do {                                                  \
77cf69771bSopenharmony_ci        if (!(cond)) {                                    \
78cf69771bSopenharmony_ci            TIME_HILOGE(module, R_FORMATED(__VA_ARGS__)); \
79cf69771bSopenharmony_ci            return ret;                                   \
80cf69771bSopenharmony_ci        }                                                 \
81cf69771bSopenharmony_ci    } while (0)
82cf69771bSopenharmony_ci
83cf69771bSopenharmony_ci#define CHECK_AND_RETURN_LOG(module, cond, ...)           \
84cf69771bSopenharmony_ci    do {                                                  \
85cf69771bSopenharmony_ci        if (!(cond)) {                                    \
86cf69771bSopenharmony_ci            TIME_HILOGE(module, R_FORMATED(__VA_ARGS__)); \
87cf69771bSopenharmony_ci            return;                                       \
88cf69771bSopenharmony_ci        }                                                 \
89cf69771bSopenharmony_ci    } while (0)
90cf69771bSopenharmony_ci
91cf69771bSopenharmony_ci} // namespace MiscServices
92cf69771bSopenharmony_ci} // namespace OHOS
93cf69771bSopenharmony_ci#endif // TIME_HILOG_WRAPPER_H