• 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 #ifndef ANS_REMINDER_LOG_TAG
35 #define ANS_REMINDER_LOG_TAG "ANS_REMINDER"
36 #endif
37 
38 enum class AnsLogLevel : uint8_t { DEBUG = 0, INFO, WARN, ERROR, FATAL };
39 
40 static constexpr OHOS::HiviewDFX::HiLogLabel ANS_LABEL = {LOG_CORE, ANS_LOG_DOMAIN, ANS_LOG_TAG};
41 static constexpr OHOS::HiviewDFX::HiLogLabel ANS_REMINDER_LABEL = {LOG_CORE, ANS_LOG_DOMAIN, ANS_REMINDER_LOG_TAG};
42 
43 class AnsLogWrapper {
44 public:
45     AnsLogWrapper() = delete;
46     ~AnsLogWrapper() = delete;
47 
48     /**
49      * @brief Judge the level of the log.
50      *
51      * @param level Indicates the level of the log.
52      * @return Returns ture on passed, otherwise false.
53      */
54     static bool JudgeLevel(const AnsLogLevel &level);
55 
56     /**
57      * @brief Set the level of the log.
58      *
59      * @param level Indicates the level of the log.
60      */
SetLogLevel(const AnsLogLevel & level)61     static void SetLogLevel(const AnsLogLevel &level)
62     {
63         level_ = level;
64     }
65 
66     /**
67      * @brief Get the level of the log.
68      *
69      * @return Indicates the level of the log.
70      */
GetLogLevel()71     static const AnsLogLevel &GetLogLevel()
72     {
73         return level_;
74     }
75 
76     /**
77      * @brief Get the brief name of the file.
78      *
79      * @param str Indicates the full name of the file.
80      * @return Indicates the file name.
81      */
82     static std::string GetBriefFileName(const char *str);
83 
84 private:
85     static AnsLogLevel level_;
86 };
87 
88 #define CUR_FILENAME (__builtin_strrchr(__FILE__, '/') ? __builtin_strrchr(__FILE__, '/') + 1 : __FILE__)
89 
90 #define ANS_LOGF(fmt, ...)            \
91     ((void)HILOG_IMPL(LOG_CORE, LOG_FATAL, ANS_LOG_DOMAIN, ANS_LOG_TAG, \
92     "[%{public}s(%{public}s:%{public}d)]" fmt, CUR_FILENAME, __FUNCTION__, __LINE__, ##__VA_ARGS__))
93 #define ANS_LOGE(fmt, ...)            \
94     ((void)HILOG_IMPL(LOG_CORE, LOG_ERROR, ANS_LOG_DOMAIN, ANS_LOG_TAG, \
95     "[%{public}s(%{public}s:%{public}d)]" fmt, CUR_FILENAME, __FUNCTION__, __LINE__, ##__VA_ARGS__))
96 #define ANS_LOGW(fmt, ...)            \
97     ((void)HILOG_IMPL(LOG_CORE, LOG_WARN, ANS_LOG_DOMAIN, ANS_LOG_TAG, \
98     "[%{public}s(%{public}s:%{public}d)]" fmt, CUR_FILENAME, __FUNCTION__, __LINE__, ##__VA_ARGS__))
99 #define ANS_LOGI(fmt, ...)            \
100     ((void)HILOG_IMPL(LOG_CORE, LOG_INFO, ANS_LOG_DOMAIN, ANS_LOG_TAG, \
101     "[%{public}s(%{public}s:%{public}d)]" fmt, CUR_FILENAME, __FUNCTION__, __LINE__, ##__VA_ARGS__))
102 #define ANS_LOGD(fmt, ...)            \
103     ((void)HILOG_IMPL(LOG_CORE, LOG_DEBUG, ANS_LOG_DOMAIN, ANS_LOG_TAG, \
104     "[%{public}s(%{public}s:%{public}d)]" fmt, CUR_FILENAME, __FUNCTION__, __LINE__, ##__VA_ARGS__))
105 
106 #define ANSR_LOGF(fmt, ...)            \
107     ((void)HILOG_IMPL(LOG_CORE, LOG_FATAL, ANS_LOG_DOMAIN, ANS_REMINDER_LOG_TAG, \
108     "[%{public}s(%{public}s:%{public}d)]" fmt, CUR_FILENAME, __FUNCTION__, __LINE__, ##__VA_ARGS__))
109 #define ANSR_LOGE(fmt, ...)            \
110     ((void)HILOG_IMPL(LOG_CORE, LOG_ERROR, ANS_LOG_DOMAIN, ANS_REMINDER_LOG_TAG, \
111     "[%{public}s(%{public}s:%{public}d)]" fmt, CUR_FILENAME, __FUNCTION__, __LINE__, ##__VA_ARGS__))
112 #define ANSR_LOGW(fmt, ...)            \
113     ((void)HILOG_IMPL(LOG_CORE, LOG_WARN, ANS_LOG_DOMAIN, ANS_REMINDER_LOG_TAG, \
114     "[%{public}s(%{public}s:%{public}d)]" fmt, CUR_FILENAME, __FUNCTION__, __LINE__, ##__VA_ARGS__))
115 #define ANSR_LOGI(fmt, ...)            \
116     ((void)HILOG_IMPL(LOG_CORE, LOG_INFO, ANS_LOG_DOMAIN, ANS_REMINDER_LOG_TAG, \
117     "[%{public}s(%{public}s:%{public}d)]" fmt, CUR_FILENAME, __FUNCTION__, __LINE__, ##__VA_ARGS__))
118 #define ANSR_LOGD(fmt, ...)            \
119     ((void)HILOG_IMPL(LOG_CORE, LOG_DEBUG, ANS_LOG_DOMAIN, ANS_REMINDER_LOG_TAG, \
120     "[%{public}s(%{public}s:%{public}d)]" fmt, CUR_FILENAME, __FUNCTION__, __LINE__, ##__VA_ARGS__))
121 }  // namespace Notification
122 }  // namespace OHOS
123 
124 #endif  // BASE_NOTIFICATION_ANS_STANDARD_INNERKITS_BASE_INCLUDE_ANS_LOG_HELPER_H