1 /*
2  * Copyright (c) 2022-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 TELEPHONY_LOG_WRAPPER_H
17 #define TELEPHONY_LOG_WRAPPER_H
18 
19 #include <string>
20 
21 #include "hilog/log.h"
22 
23 namespace OHOS {
24 namespace Telephony {
25 #ifndef LOG_DOMAIN
26 #define LOG_DOMAIN 0xD001F00
27 #endif
28 #ifndef TELEPHONY_LOG_TAG
29 #define TELEPHONY_LOG_TAG "TelephonySubsystem"
30 #endif
31 
32 #define OHOS_DEBUG
33 #ifndef OHOS_DEBUG
34 #define TELEPHONY_LOGE(fmt, ...) \
35     (void)HILOG_IMPL(LOG_CORE, LOG_ERROR, LOG_DOMAIN, TELEPHONY_LOG_TAG, fmt, ##__VA_ARGS__)
36 #define TELEPHONY_LOGW(fmt, ...) \
37     (void)HILOG_IMPL(LOG_CORE, LOG_WARN, LOG_DOMAIN, TELEPHONY_LOG_TAG, fmt, ##__VA_ARGS__)
38 #define TELEPHONY_LOGI(fmt, ...) \
39     (void)HILOG_IMPL(LOG_CORE, LOG_INFO, LOG_DOMAIN, TELEPHONY_LOG_TAG, fmt, ##__VA_ARGS__)
40 #define TELEPHONY_LOGF(fmt, ...) \
41     (void)HILOG_IMPL(LOG_CORE, LOG_FATAL, LOG_DOMAIN, TELEPHONY_LOG_TAG, fmt, ##__VA_ARGS__)
42 #define TELEPHONY_LOGD(fmt, ...) \
43     (void)HILOG_IMPL(LOG_CORE, LOG_DEBUG, LOG_DOMAIN, TELEPHONY_LOG_TAG, fmt, ##__VA_ARGS__)
44 #else
45 // Gets the raw file name of the file.
46 // This function is a function executed by the compiler, that is,
47 // it has been executed at compile time. When the program runs,
48 // it directly refers to the value calculated by this function
49 // and does not consume CPU for calculation.
50 inline constexpr const char *GetRawFileName(const char *path)
51 {
52     char ch = '/';
53     const char *start = path;
54     // get the end of the string
55     while (*start++) {
56         ;
57     }
58     while (--start != path && *start != ch) {
59         ;
60     }
61 
62     return (*start == ch) ? ++start : path;
63 }
64 
65 #define TELEPHONY_LOGE(fmt, ...)                                                                                      \
66     (void)HILOG_IMPL(LOG_CORE, LOG_ERROR, LOG_DOMAIN, TELEPHONY_LOG_TAG, "[%{public}s-(%{public}s:%{public}d)] " fmt, \
67     __FUNCTION__, GetRawFileName(__FILE__), __LINE__, ##__VA_ARGS__)
68 #define TELEPHONY_LOGW(fmt, ...)                                                                                     \
69     (void)HILOG_IMPL(LOG_CORE, LOG_WARN, LOG_DOMAIN, TELEPHONY_LOG_TAG, "[%{public}s-(%{public}s:%{public}d)] " fmt, \
70     __FUNCTION__, GetRawFileName(__FILE__), __LINE__, ##__VA_ARGS__)
71 #define TELEPHONY_LOGI(fmt, ...)                                                                                     \
72     (void)HILOG_IMPL(LOG_CORE, LOG_INFO, LOG_DOMAIN, TELEPHONY_LOG_TAG, "[%{public}s-(%{public}s:%{public}d)] " fmt, \
73     __FUNCTION__, GetRawFileName(__FILE__), __LINE__, ##__VA_ARGS__)
74 #define TELEPHONY_LOGF(fmt, ...)                                                                                      \
75     (void)HILOG_IMPL(LOG_CORE, LOG_FATAL, LOG_DOMAIN, TELEPHONY_LOG_TAG, "[%{public}s-(%{public}s:%{public}d)] " fmt, \
76     __FUNCTION__, GetRawFileName(__FILE__), __LINE__, ##__VA_ARGS__)
77 #define TELEPHONY_LOGD(fmt, ...)                                                                                      \
78     (void)HILOG_IMPL(LOG_CORE, LOG_DEBUG, LOG_DOMAIN, TELEPHONY_LOG_TAG, "[%{public}s-(%{public}s:%{public}d)] " fmt, \
79     __FUNCTION__, GetRawFileName(__FILE__), __LINE__, ##__VA_ARGS__)
80 #endif
81 } // namespace Telephony
82 } // namespace OHOS
83 #endif // OHOS_TELEPHONY_LOG_WRAPPER_H
84