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_ABILITY_RUNTIME_WANT_AGENT_LOG_WRAPPER_H
17 #define OHOS_ABILITY_RUNTIME_WANT_AGENT_LOG_WRAPPER_H
18 
19 #include <string>
20 #include "hilog/log.h"
21 
22 namespace OHOS::AbilityRuntime::WantAgent {
23 #ifndef WANT_AGENT_LOG_DOMAIN
24 #define WANT_AGENT_LOG_DOMAIN 0xD001000
25 #endif
26 #ifndef WANT_AGENT_LOG_TAG
27 #define WANT_AGENT_LOG_TAG "WantAgent"
28 #endif
29 
30 enum class WantAgentLogLevel { DEBUG = 0, INFO, WARN, ERROR, FATAL };
31 
32 static constexpr OHOS::HiviewDFX::HiLogLabel Want_Agent_LABEL = {LOG_CORE, WANT_AGENT_LOG_DOMAIN, WANT_AGENT_LOG_TAG};
33 
34 class WantAgentLogWrapper {
35 public:
36     static bool JudgeLevel(const WantAgentLogLevel &level);
37 
SetLogLevel(const WantAgentLogLevel & level)38     static void SetLogLevel(const WantAgentLogLevel &level)
39     {
40         level_ = level;
41     }
42 
GetLogLevel()43     static const WantAgentLogLevel &GetLogLevel()
44     {
45         return level_;
46     }
47 
48     static std::string GetBriefFileName(const char *str);
49 
50 private:
51     static WantAgentLogLevel level_;
52 };
53 
54 #define PRINT_LOG(LEVEL, Level, fmt, ...)                          \
55     if (WantAgentLogWrapper::JudgeLevel(WantAgentLogLevel::LEVEL)) \
56     OHOS::HiviewDFX::HiLog::Level(Want_Agent_LABEL,                \
57         "[%{public}s(%{public}s)] " fmt,                           \
58         WantAgentLogWrapper::GetBriefFileName(__FILE__).c_str(),   \
59         __FUNCTION__,                                              \
60         ##__VA_ARGS__)
61 
62 #define WANT_AGENT_LOGD(fmt, ...) PRINT_LOG(DEBUG, Debug, fmt, ##__VA_ARGS__)
63 #define WANT_AGENT_LOGI(fmt, ...) PRINT_LOG(INFO, Info, fmt, ##__VA_ARGS__)
64 #define WANT_AGENT_LOGW(fmt, ...) PRINT_LOG(WARN, Warn, fmt, ##__VA_ARGS__)
65 #define WANT_AGENT_LOGE(fmt, ...) PRINT_LOG(ERROR, Error, fmt, ##__VA_ARGS__)
66 #define WANT_AGENT_LOGF(fmt, ...) PRINT_LOG(FATAL, Fatal, fmt, ##__VA_ARGS__)
67 }  // namespace OHOS::AbilityRuntime::WantAgent
68 #endif  // OHOS_ABILITY_RUNTIME_WANT_AGENT_LOG_WRAPPER_H
69