• 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 BASE_NOTIFICATION_ANS_STANDARD_INNERKITS_BASE_INCLUDE_ANS_LOG_HELPER_H
17 #define BASE_NOTIFICATION_ANS_STANDARD_INNERKITS_BASE_INCLUDE_ANS_LOG_HELPER_H
18 
19 #include <stdint.h>         // for uint8_t
20 #include <string>           // for basic_string
21 
22 #include "hilog/log.h"
23 
24 namespace OHOS {
25 namespace Notification {
26 #ifndef ANS_LOG_DOMAIN
27 #define ANS_LOG_DOMAIN 0xD001200
28 #endif
29 
30 #ifndef ANS_LOG_TAG
31 #define ANS_LOG_TAG "Ans"
32 #endif
33 
34 enum class AnsLogLevel : uint8_t { DEBUG = 0, INFO, WARN, ERROR, FATAL };
35 
36 static constexpr OHOS::HiviewDFX::HiLogLabel ANS_LABEL = {LOG_CORE, ANS_LOG_DOMAIN, ANS_LOG_TAG};
37 static constexpr OHOS::HiviewDFX::HiLogLabel ANS_REMINDER_LABEL = {LOG_CORE, ANS_LOG_DOMAIN, "ANS_REMINDER"};
38 
39 class AnsLogWrapper {
40 public:
41     AnsLogWrapper() = delete;
42     ~AnsLogWrapper() = delete;
43 
44     /**
45      * @brief Judge the level of the log.
46      *
47      * @param level Indicates the level of the log.
48      * @return Returns ture on passed, otherwise false.
49      */
50     static bool JudgeLevel(const AnsLogLevel &level);
51 
52     /**
53      * @brief Set the level of the log.
54      *
55      * @param level Indicates the level of the log.
56      */
SetLogLevel(const AnsLogLevel & level)57     static void SetLogLevel(const AnsLogLevel &level)
58     {
59         level_ = level;
60     }
61 
62     /**
63      * @brief Get the level of the log.
64      *
65      * @return Indicates the level of the log.
66      */
GetLogLevel()67     static const AnsLogLevel &GetLogLevel()
68     {
69         return level_;
70     }
71 
72     /**
73      * @brief Get the brief name of the file.
74      *
75      * @param str Indicates the full name of the file.
76      * @return Indicates the file name.
77      */
78     static std::string GetBriefFileName(const char *str);
79 
80 private:
81     static AnsLogLevel level_;
82 };
83 
84 #define PRINT_LOG(LEVEL, Level, fmt, ...)                  \
85     if (AnsLogWrapper::JudgeLevel(AnsLogLevel::LEVEL))     \
86     OHOS::HiviewDFX::HiLog::Level(ANS_LABEL,               \
87         "[%{public}s(%{public}s):%{public}d] " fmt,        \
88         AnsLogWrapper::GetBriefFileName(__FILE__).c_str(), \
89         __FUNCTION__,                                      \
90         __LINE__,                                          \
91         ##__VA_ARGS__)
92 
93 #define ANS_LOGD(fmt, ...) PRINT_LOG(DEBUG, Debug, fmt, ##__VA_ARGS__)
94 #define ANS_LOGI(fmt, ...) PRINT_LOG(INFO, Info, fmt, ##__VA_ARGS__)
95 #define ANS_LOGW(fmt, ...) PRINT_LOG(WARN, Warn, fmt, ##__VA_ARGS__)
96 #define ANS_LOGE(fmt, ...) PRINT_LOG(ERROR, Error, fmt, ##__VA_ARGS__)
97 #define ANS_LOGF(fmt, ...) PRINT_LOG(FATAL, Fatal, fmt, ##__VA_ARGS__)
98 
99 #define PRINT_REMINDER_LOG(LEVEL, Level, fmt, ...)                  \
100     if (AnsLogWrapper::JudgeLevel(AnsLogLevel::LEVEL))     \
101     OHOS::HiviewDFX::HiLog::Level(ANS_REMINDER_LABEL,      \
102         "[%{public}s(%{public}s):%{public}d] " fmt,        \
103         AnsLogWrapper::GetBriefFileName(__FILE__).c_str(), \
104         __FUNCTION__,                                      \
105         __LINE__,                                          \
106         ##__VA_ARGS__)
107 
108 #define ANSR_LOGD(fmt, ...) PRINT_REMINDER_LOG(DEBUG, Debug, fmt, ##__VA_ARGS__)
109 #define ANSR_LOGI(fmt, ...) PRINT_REMINDER_LOG(INFO, Info, fmt, ##__VA_ARGS__)
110 #define ANSR_LOGW(fmt, ...) PRINT_REMINDER_LOG(WARN, Warn, fmt, ##__VA_ARGS__)
111 #define ANSR_LOGE(fmt, ...) PRINT_REMINDER_LOG(ERROR, Error, fmt, ##__VA_ARGS__)
112 #define ANSR_LOGF(fmt, ...) PRINT_REMINDER_LOG(FATAL, Fatal, fmt, ##__VA_ARGS__)
113 }  // namespace Notification
114 }  // namespace OHOS
115 
116 #endif  // BASE_NOTIFICATION_ANS_STANDARD_INNERKITS_BASE_INCLUDE_ANS_LOG_HELPER_H