• 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_TELEPHONY_LOG_WRAPPER_H
17 #define OHOS_TELEPHONY_LOG_WRAPPER_H
18 
19 #include <string>
20 
21 #include "hilog/log.h"
22 
23 namespace OHOS {
24 namespace Telephony {
25 enum class TelephonyLogLevel {
26     DEBUG = 0,
27     INFO,
28     WARN,
29     ERROR,
30     FATAL,
31 };
32 
33 class TelephonyLogWrapper {
34 public:
35     static bool JudgeLevel(const TelephonyLogLevel &level);
36 
SetLogLevel(const TelephonyLogLevel & level)37     static void SetLogLevel(const TelephonyLogLevel &level)
38     {
39         level_ = level;
40     }
41 
GetLogLevel()42     static const TelephonyLogLevel &GetLogLevel()
43     {
44         return level_;
45     }
46 
47     static std::string GetBriefFileName(const std::string &file);
48 
49 private:
50     static TelephonyLogLevel level_;
51 };
52 
53 #define CONFIG_HILOG
54 #ifdef CONFIG_HILOG
55 
56 #ifndef TELEPHONY_LOG_TAG
57 #define TELEPHONY_LOG_TAG "TelephonySubsystem"
58 #endif
59 
60 #ifndef TELEPHONY_LOG_FUNC_NAME
61 #define TELEPHONY_LOG_FUNC_NAME __func__
62 #endif
63 
64 static constexpr OHOS::HiviewDFX::HiLogLabel TELEPHONY_LABEL = {LOG_CORE, LOG_DOMAIN, TELEPHONY_LOG_TAG};
65 
66 #define OHOS_DEBUG
67 #ifndef OHOS_DEBUG
68 #define PRINT_TELEPHONY_LOG(op, fmt, ...) (void)OHOS::HiviewDFX::HiLog::op(TELEPHONY_LABEL, fmt, ##__VA_ARGS__)
69 #else
70 // Gets the raw file name of the file.
71 // This function is a function executed by the compiler, that is,
72 // it has been executed at compile time. When the program runs,
73 // it directly refers to the value calculated by this function
74 // and does not consume CPU for calculation.
GetRawFileName(const char * path)75 inline constexpr const char *GetRawFileName(const char *path)
76 {
77     char ch = '/';
78     const char *start = path;
79     // get the end of the string
80     while (*start++) {
81         ;
82     }
83     while (--start != path && *start != ch) {
84         ;
85     }
86 
87     return (*start == ch) ? ++start : path;
88 }
89 
90 #define PRINT_TELEPHONY_LOG(op, fmt, ...)                                                                        \
91     (void)OHOS::HiviewDFX::HiLog::op(TELEPHONY_LABEL, "[%{public}s-(%{public}s:%{public}d)] " fmt, __FUNCTION__, \
92         GetRawFileName(__FILE__), __LINE__, ##__VA_ARGS__)
93 #endif
94 
95 #define TELEPHONY_LOGE(fmt, ...) PRINT_TELEPHONY_LOG(Error, fmt, ##__VA_ARGS__)
96 #define TELEPHONY_LOGW(fmt, ...) PRINT_TELEPHONY_LOG(Warn, fmt, ##__VA_ARGS__)
97 #define TELEPHONY_LOGI(fmt, ...) PRINT_TELEPHONY_LOG(Info, fmt, ##__VA_ARGS__)
98 #define TELEPHONY_LOGF(fmt, ...) PRINT_TELEPHONY_LOG(Fatal, fmt, ##__VA_ARGS__)
99 #define TELEPHONY_LOGD(fmt, ...) PRINT_TELEPHONY_LOG(Debug, fmt, ##__VA_ARGS__)
100 #endif // CONFIG_HILOG
101 } // namespace Telephony
102 } // namespace OHOS
103 #endif // OHOS_TELEPHONY_LOG_WRAPPER_H