1 /*
2 * Copyright (c) 2022 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 static constexpr OHOS::HiviewDFX::HiLogLabel TELEPHONY_LABEL = {LOG_CORE, LOG_DOMAIN, TELEPHONY_LOG_TAG};
33
34 #define OHOS_DEBUG
35 #ifndef OHOS_DEBUG
36 #define PRINT_TELEPHONY_LOG(op, fmt, ...) (void)OHOS::HiviewDFX::HiLog::op(TELEPHONY_LABEL, fmt, ##__VA_ARGS__)
37 #else
38 // Gets the raw file name of the file.
39 // This function is a function executed by the compiler, that is,
40 // it has been executed at compile time. When the program runs,
41 // it directly refers to the value calculated by this function
42 // and does not consume CPU for calculation.
GetRawFileName(const char * path)43 inline constexpr const char *GetRawFileName(const char *path)
44 {
45 char ch = '/';
46 const char *start = path;
47 // get the end of the string
48 while (*start++) {
49 ;
50 }
51 while (--start != path && *start != ch) {
52 ;
53 }
54
55 return (*start == ch) ? ++start : path;
56 }
57
58 #define PRINT_TELEPHONY_LOG(op, fmt, ...) \
59 (void)OHOS::HiviewDFX::HiLog::op(TELEPHONY_LABEL, "[%{public}s-(%{public}s:%{public}d)] " fmt, __FUNCTION__, \
60 GetRawFileName(__FILE__), __LINE__, ##__VA_ARGS__)
61 #endif
62
63 #define TELEPHONY_LOGE(fmt, ...) PRINT_TELEPHONY_LOG(Error, fmt, ##__VA_ARGS__)
64 #define TELEPHONY_LOGW(fmt, ...) PRINT_TELEPHONY_LOG(Warn, fmt, ##__VA_ARGS__)
65 #define TELEPHONY_LOGI(fmt, ...) PRINT_TELEPHONY_LOG(Info, fmt, ##__VA_ARGS__)
66 #define TELEPHONY_LOGF(fmt, ...) PRINT_TELEPHONY_LOG(Fatal, fmt, ##__VA_ARGS__)
67 #define TELEPHONY_LOGD(fmt, ...) PRINT_TELEPHONY_LOG(Debug, fmt, ##__VA_ARGS__)
68 } // namespace Telephony
69 } // namespace OHOS
70 #endif // OHOS_TELEPHONY_LOG_WRAPPER_H