• 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 <string>
20 
21 #include "hilog/log.h"
22 
23 namespace OHOS {
24 namespace Notification {
25 #ifndef LOG_DOMAIN
26 #define LOG_DOMAIN 0xD001800
27 #endif
28 
29 #ifndef ANS_LOG_TAG
30 #define ANS_LOG_TAG NULL
31 #endif
32 
33 enum class AnsLogLevel : uint8_t { DEBUG = 0, INFO, WARN, ERROR, FATAL };
34 
35 static constexpr OHOS::HiviewDFX::HiLogLabel ANS_LABEL = {LOG_CORE, LOG_DOMAIN, "ANS_STANDARD"};
36 static constexpr OHOS::HiviewDFX::HiLogLabel ANS_REMINDER_LABEL = {LOG_CORE, LOG_DOMAIN, "ANS_REMINDER"};
37 
38 class AnsLogWrapper {
39 public:
40     AnsLogWrapper() = delete;
41     ~AnsLogWrapper() = delete;
42 
43     static bool JudgeLevel(const AnsLogLevel &level);
44 
SetLogLevel(const AnsLogLevel & level)45     static void SetLogLevel(const AnsLogLevel &level)
46     {
47         level_ = level;
48     }
49 
GetLogLevel()50     static const AnsLogLevel &GetLogLevel()
51     {
52         return level_;
53     }
54 
55     static std::string GetBriefFileName(const char *str);
56 
57 private:
58     static AnsLogLevel level_;
59 };
60 
61 #define PRINT_LOG(LEVEL, Level, fmt, ...)                  \
62     if (AnsLogWrapper::JudgeLevel(AnsLogLevel::LEVEL))     \
63     OHOS::HiviewDFX::HiLog::Level(ANS_LABEL,               \
64         "[%{public}s(%{public}s):%{public}d] " fmt,        \
65         AnsLogWrapper::GetBriefFileName(__FILE__).c_str(), \
66         __FUNCTION__,                                      \
67         __LINE__,                                          \
68         ##__VA_ARGS__)
69 
70 #define ANS_LOGD(fmt, ...) PRINT_LOG(DEBUG, Debug, fmt, ##__VA_ARGS__)
71 #define ANS_LOGI(fmt, ...) PRINT_LOG(INFO, Info, fmt, ##__VA_ARGS__)
72 #define ANS_LOGW(fmt, ...) PRINT_LOG(WARN, Warn, fmt, ##__VA_ARGS__)
73 #define ANS_LOGE(fmt, ...) PRINT_LOG(ERROR, Error, fmt, ##__VA_ARGS__)
74 #define ANS_LOGF(fmt, ...) PRINT_LOG(FATAL, Fatal, fmt, ##__VA_ARGS__)
75 
76 #define PRINT_REMINDER_LOG(LEVEL, Level, fmt, ...)                  \
77     if (AnsLogWrapper::JudgeLevel(AnsLogLevel::LEVEL))     \
78     OHOS::HiviewDFX::HiLog::Level(ANS_REMINDER_LABEL,      \
79         "[%{public}s(%{public}s):%{public}d] " fmt,        \
80         AnsLogWrapper::GetBriefFileName(__FILE__).c_str(), \
81         __FUNCTION__,                                      \
82         __LINE__,                                          \
83         ##__VA_ARGS__)
84 
85 #define ANSR_LOGD(fmt, ...) PRINT_REMINDER_LOG(DEBUG, Debug, fmt, ##__VA_ARGS__)
86 #define ANSR_LOGI(fmt, ...) PRINT_REMINDER_LOG(INFO, Info, fmt, ##__VA_ARGS__)
87 #define ANSR_LOGW(fmt, ...) PRINT_REMINDER_LOG(WARN, Warn, fmt, ##__VA_ARGS__)
88 #define ANSR_LOGE(fmt, ...) PRINT_REMINDER_LOG(ERROR, Error, fmt, ##__VA_ARGS__)
89 #define ANSR_LOGF(fmt, ...) PRINT_REMINDER_LOG(FATAL, Fatal, fmt, ##__VA_ARGS__)
90 }  // namespace Notification
91 }  // namespace OHOS
92 
93 #endif  // BASE_NOTIFICATION_ANS_STANDARD_INNERKITS_BASE_INCLUDE_ANS_LOG_HELPER_H