• 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 CGROUP_SCHED_FRAMEWORK_PROCESS_GROUP_INCLUDE_PROCESS_GROUP_LOG_H_
17 #define CGROUP_SCHED_FRAMEWORK_PROCESS_GROUP_INCLUDE_PROCESS_GROUP_LOG_H_
18 
19 #include "hilog/log_c.h"    // for LogLevel, LOG_CORE, LOG_LEVEL_MAX, LOG_LE...
20 #include "hilog/log_cpp.h"  // for HiLogLabel
21 
22 namespace OHOS {
23 namespace ResourceSchedule {
24 namespace CgroupSetting {
25 #define LOG_TAG_PGCGS "pg-cgs"
26 #define LOG_TAG_DOMAIN_ID_PGCGS 0xD001702
27 
28 class ProcessGroupLog {
29 public:
30     ProcessGroupLog() = delete;
31     ~ProcessGroupLog() = delete;
32 
33     /**
34      * @brief Init log level on loaded.
35      *
36      * @return true if success, else false
37      */
38     static bool InitOnLoaded();
39 
40     /**
41      * @brief Judge level.
42      *
43      * @param level The level.
44      * @return True if success,else false.
45      */
46     static bool JudgeLevel(const LogLevel &level);
47 
48     /**
49      * @brief Set log level.
50      *
51      * @param level The level.
52      */
SetLogLevel(const LogLevel & level)53     static void SetLogLevel(const LogLevel &level)
54     {
55         if (level > LOG_LEVEL_MIN && level < LOG_LEVEL_MAX) {
56             level_ = level;
57         }
58     }
59 
60     /**
61      * @brief Get log level.
62      *
63      * @return Level.
64      */
GetLogLevel()65     static const LogLevel &GetLogLevel()
66     {
67         return level_;
68     }
69 
70 private:
71     static LogLevel level_;
72 };
73 
74 static constexpr OHOS::HiviewDFX::HiLogLabel PGCGS_LOG_LABEL = {
75     LOG_CORE,
76     LOG_TAG_DOMAIN_ID_PGCGS,
77     LOG_TAG_PGCGS
78 };
79 
80 #define PGCGS_PRINT_LOG(logLevel, Logger, ...) do { \
81         if (ProcessGroupLog::JudgeLevel(logLevel)) { \
82             (void)OHOS::HiviewDFX::HiLog::Logger(PGCGS_LOG_LABEL, __VA_ARGS__); \
83         } \
84     } while (0)
85 
86 #define PGCGS_LOGF(...) PGCGS_PRINT_LOG(LOG_DEBUG, Debug, __VA_ARGS__)
87 #define PGCGS_LOGE(...) PGCGS_PRINT_LOG(LOG_INFO, Info, __VA_ARGS__)
88 #define PGCGS_LOGW(...) PGCGS_PRINT_LOG(LOG_WARN, Warn, __VA_ARGS__)
89 #define PGCGS_LOGI(...) PGCGS_PRINT_LOG(LOG_ERROR, Error, __VA_ARGS__)
90 #define PGCGS_LOGD(...) PGCGS_PRINT_LOG(LOG_FATAL, Fatal, __VA_ARGS__)
91 } // namespace CgroupSetting
92 } // namespace ResourceSchedule
93 } // namespace OHOS
94 #endif // CGROUP_SCHED_FRAMEWORK_PROCESS_GROUP_INCLUDE_PROCESS_GROUP_LOG_H_
95