• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 BUNDLE_ACTIVE_LOG_H
17 #define BUNDLE_ACTIVE_LOG_H
18 
19 #include <string>
20 #include "hilog/log.h"
21 
22 namespace OHOS {
23 namespace DeviceUsageStats {
24 #ifndef LOG_TAG_DOMAIN_ID_BUNDLE_ACTIVE
25 #define LOG_TAG_DOMAIN_ID_BUNDLE_ACTIVE 0xD001710
26 #endif
27 
28 #ifndef LOG_TAG_BUNDLE_ACTIVE
29 #define LOG_TAG_BUNDLE_ACTIVE "BUNDLE_ACTIVE"
30 #endif
31 
32 static constexpr OHOS::HiviewDFX::HiLogLabel BUNDLE_ACTIVE_LOG_LABEL = {
33     LOG_CORE,
34     LOG_TAG_DOMAIN_ID_BUNDLE_ACTIVE,
35     LOG_TAG_BUNDLE_ACTIVE
36 };
37 
38 enum class BundleActiveLogLevel : uint8_t { DEBUG = 0, INFO, WARN, ERROR, FATAL };
39 
40 class BundleActiveLog {
41 public:
42     BundleActiveLog() = delete;
43     ~BundleActiveLog() = delete;
44 
45     static bool JudgeValidLevel(const BundleActiveLogLevel &level);
46 
SetLogLevel(const BundleActiveLogLevel & level)47     static void SetLogLevel(const BundleActiveLogLevel &level)
48     {
49         logLevel_ = level;
50     }
51 
GetLogLevel()52     static const BundleActiveLogLevel &GetLogLevel()
53     {
54         return logLevel_;
55     }
56 
57     static std::string GetCurrFileName(const char *str);
58 
59 private:
60     static BundleActiveLogLevel logLevel_;
61 };
62 
63 #define BUNDLE_ACTIVE_PRINT_LOG(LEVEL, Level, fmt, ...)                                              \
64     if (BundleActiveLog::JudgeValidLevel(BundleActiveLogLevel::LEVEL))                 \
65     OHOS::HiviewDFX::HiLog::Level(BUNDLE_ACTIVE_LOG_LABEL,                             \
66         "[%{public}s(%{public}s):%{public}d] " fmt,                                    \
67         BundleActiveLog::GetCurrFileName(__FILE__).c_str(),                            \
68         __FUNCTION__,                                                                  \
69         __LINE__,                                                                      \
70         ##__VA_ARGS__)
71 
72 #define BUNDLE_ACTIVE_LOGD(fmt, ...) BUNDLE_ACTIVE_PRINT_LOG(DEBUG, Debug, fmt, ##__VA_ARGS__)
73 #define BUNDLE_ACTIVE_LOGI(fmt, ...) BUNDLE_ACTIVE_PRINT_LOG(INFO, Info, fmt, ##__VA_ARGS__)
74 #define BUNDLE_ACTIVE_LOGW(fmt, ...) BUNDLE_ACTIVE_PRINT_LOG(WARN, Warn, fmt, ##__VA_ARGS__)
75 #define BUNDLE_ACTIVE_LOGE(fmt, ...) BUNDLE_ACTIVE_PRINT_LOG(ERROR, Error, fmt, ##__VA_ARGS__)
76 #define BUNDLE_ACTIVE_LOGF(fmt, ...) BUNDLE_ACTIVE_PRINT_LOG(FATAL, Fatal, fmt, ##__VA_ARGS__)
77 }  // namespace DeviceUsageStats
78 }  // namespace OHOS
79 #endif  // BUNDLE_ACTIVE_LOG_H
80 
81