1 /*
2  * Copyright (c) 2021-2023 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 RENDER_SERVICE_BASE_CORE_COMMON_RS_LOG_H
17 #define RENDER_SERVICE_BASE_CORE_COMMON_RS_LOG_H
18 
19 // NOT redundant, we need PRIu64/PRId64 for logging
20 #include <cinttypes>
21 #include <string>
22 #include <hilog/log.h>
23 
24 #include "common/rs_macros.h"
25 
26 namespace OHOS {
27 namespace Rosen {
28 // The "0xD001400" is the domain ID for graphic module that alloted by the OS.
29 constexpr OHOS::HiviewDFX::HiLogLabel LABEL_RS = { LOG_CORE, 0xD001400, "OHOS::RS" };
30 constexpr OHOS::HiviewDFX::HiLogLabel LABEL_ROSEN = { LOG_CORE, 0xD001400, "OHOS::ROSEN" };
31 constexpr const char* DEBUG_GRAPHIC_LOG_FLAG = "debug.graphic.logflag";
32 
33 class RSB_EXPORT RSLog {
34 public:
35     enum Tag { RS = 0, RS_CLIENT };
36     enum Level { LEVEL_INFO = 0, LEVEL_DEBUG, LEVEL_WARN, LEVEL_ERROR, LEVEL_FATAL };
37     virtual ~RSLog() = default;
38 };
39 
40 void RSB_EXPORT RSLogOutput(RSLog::Tag tag, RSLog::Level level, const char* format, ...);
41 
42 enum RSLogFlag {
43     // screen
44     FLAG_DEBUG_SCREEN = 0x00000001,
45 
46     // node
47     FLAG_DEBUG_NODE = 0x00000002,
48 
49     // effect
50     FLAG_DEBUG_EFFECT = 0x00000004,
51 
52     // pipeline
53     FLAG_DEBUG_PIPELINE = 0x00000008,
54 
55     // modifier
56     FLAG_DEBUG_MODIFIER = 0x00000010,
57 
58     // buffer
59     FLAG_DEBUG_BUFFER = 0x00000020,
60 
61     // layer
62     FLAG_DEBUG_LAYER = 0x00000040,
63 
64     // composer
65     FLAG_DEBUG_COMPOSER = 0x00000080,
66 
67     // vsync
68     FLAG_DEBUG_VSYNC = 0x00000100,
69 
70     // drawing
71     FLAG_DEBUG_DRAWING = 0x00000200,
72 
73     // prevalidate
74     FLAG_DEBUG_PREVALIDATE = 0x00000400,
75 };
76 
77 class RSLogManager {
78 public:
79     RSLogManager();
80     ~RSLogManager() = default;
81 
82     static RSLogManager& GetInstance();
83     bool SetRSLogFlag(std::string& flag);
IsRSLogFlagEnabled(RSLogFlag flag)84     inline bool IsRSLogFlagEnabled(RSLogFlag flag)
85     {
86         return logFlag_ & flag;
87     }
88 
89 private:
90     uint32_t logFlag_ = 0;
91 
92     bool IsFlagValid(std::string& flag);
93 
94     static constexpr uint32_t INPUT_FLAG_MIN_LENGTH = 2;
95 
96     static constexpr uint32_t INPUT_FLAG_MAX_LENGTH = 10;
97 
98     static constexpr uint32_t NUMERICAL_BASE = 16;
99 };
100 
101 } // namespace Rosen
102 } // namespace OHOS
103 
104 
105 #undef LOG_DOMAIN
106 #define LOG_DOMAIN 0xD001406
107 
108 #undef LOG_TAG
109 #define LOG_TAG "OHOS::RS"
110 
111 #define ROSEN_LOGI(format, ...) \
112     HILOG_INFO(LOG_CORE, format, ##__VA_ARGS__)
113 #define ROSEN_LOGD(format, ...) \
114     HILOG_DEBUG(LOG_CORE, format, ##__VA_ARGS__)
115 #define ROSEN_LOGE(format, ...) \
116     HILOG_ERROR(LOG_CORE, format, ##__VA_ARGS__)
117 #define ROSEN_LOGW(format, ...) \
118     HILOG_WARN(LOG_CORE, format, ##__VA_ARGS__)
119 #define ROSEN_LOGF(format, ...) \
120     HILOG_FATAL(LOG_CORE, format, ##__VA_ARGS__)
121 
122 #define RS_LOGI(format, ...) \
123     HILOG_INFO(LOG_CORE, format, ##__VA_ARGS__)
124 #define RS_LOGD(format, ...) \
125     HILOG_DEBUG(LOG_CORE, format, ##__VA_ARGS__)
126 #define RS_LOGE(format, ...) \
127     HILOG_ERROR(LOG_CORE, format, ##__VA_ARGS__)
128 #define RS_LOGW(format, ...) \
129     HILOG_WARN(LOG_CORE, format, ##__VA_ARGS__)
130 #define RS_LOGF(format, ...) \
131     HILOG_FATAL(LOG_CORE, format, ##__VA_ARGS__)
132 
133 #define CONDITION(cond)     (__builtin_expect((cond) != 0, 0))
134 
135 #ifndef RS_LOGD_IF
136 #define RS_LOGD_IF(cond, format, ...) \
137     ( (CONDITION(cond)) \
138     ? ((void)HILOG_DEBUG(LOG_CORE, format, ##__VA_ARGS__)) \
139     : (void)0)
140 #endif
141 
142 #ifndef RS_LOGI_IF
143 #define RS_LOGI_IF(cond, format, ...) \
144     ( (CONDITION(cond)) \
145     ? ((void)HILOG_INFO(LOG_CORE, format, ##__VA_ARGS__)) \
146     : (void)0)
147 #endif
148 
149 #ifndef RS_LOGW_IF
150 #define RS_LOGW_IF(cond, format, ...) \
151     ( (CONDITION(cond)) \
152     ? ((void)HILOG_WARN(LOG_CORE, format, ##__VA_ARGS__)) \
153     : (void)0)
154 #endif
155 
156 #ifndef RS_LOGE_IF
157 #define RS_LOGE_IF(cond, format, ...) \
158     ( (CONDITION(cond)) \
159     ? ((void)HILOG_ERROR(LOG_CORE, format, ##__VA_ARGS__)) \
160     : (void)0)
161 #endif
162 
163 #ifndef RS_LOGF_IF
164 #define RS_LOGF_IF(cond, format, ...) \
165     ( (CONDITION(cond)) \
166     ? ((void)HILOG_FATAL(LOG_CORE, format, ##__VA_ARGS__)) \
167     : (void)0)
168 #endif
169 
170 #ifndef ROSEN_LOGD_IF
171 #define ROSEN_LOGD_IF RS_LOGD_IF
172 #endif
173 
174 #ifndef ROSEN_LOGI_IF
175 #define ROSEN_LOGI_IF RS_LOGI_IF
176 #endif
177 
178 #ifndef ROSEN_LOGW_IF
179 #define ROSEN_LOGW_IF RS_LOGW_IF
180 #endif
181 
182 #ifndef ROSEN_LOGE_IF
183 #define ROSEN_LOGE_IF RS_LOGE_IF
184 #endif
185 
186 #ifndef ROSEN_LOGF_IF
187 #define ROSEN_LOGF_IF RS_LOGF_IF
188 #endif
189 
190 #define RS_LOG_ENABLE(flag) (RSLogManager::GetInstance().IsRSLogFlagEnabled(flag))
191 
192 #define DEBUG_SCREEN RS_LOG_ENABLE(FLAG_DEBUG_SCREEN)
193 
194 #define DEBUG_NODE RS_LOG_ENABLE(FLAG_DEBUG_NODE)
195 
196 #define DEBUG_MODIFIER RS_LOG_ENABLE(FLAG_DEBUG_MODIFIER)
197 
198 #define DEBUG_BUFFER RS_LOG_ENABLE(FLAG_DEBUG_BUFFER)
199 
200 #define DEBUG_LAYER RS_LOG_ENABLE(FLAG_DEBUG_LAYER)
201 
202 #define DEBUG_COMPOSER RS_LOG_ENABLE(FLAG_DEBUG_COMPOSER)
203 
204 #define DEBUG_PIPELINE RS_LOG_ENABLE(FLAG_DEBUG_PIPELINE)
205 
206 #define DEBUG_VSYNC RS_LOG_ENABLE(FLAG_DEBUG_VSYNC)
207 
208 #define DEBUG_DRAWING RS_LOG_ENABLE(FLAG_DEBUG_DRAWING)
209 
210 #define DEBUG_PREVALIDATE RS_LOG_ENABLE(FLAG_DEBUG_PREVALIDATE)
211 
212 #define RS_LOGE_LIMIT(func, line, format, ...)                                                                   \
213 {                                                                                                                \
214     static constexpr uint64_t LOG_PRINT_INTERVAL_IN_SECOND = 20;                                                 \
215     static std::atomic<bool> isFirstTime##func##line = true;                                                     \
216     static std::atomic<uint64_t> prePrintTime##func##line = std::chrono::duration_cast<std::chrono::seconds>(    \
217         std::chrono::system_clock::now().time_since_epoch()).count();                                            \
218     uint64_t currTime = std::chrono::duration_cast<std::chrono::seconds>(                                        \
219         std::chrono::system_clock::now().time_since_epoch()).count();                                            \
220     if ((currTime - prePrintTime##func##line >= LOG_PRINT_INTERVAL_IN_SECOND) || isFirstTime##func##line) {      \
221         prePrintTime##func##line = std::chrono::duration_cast<std::chrono::seconds>(                             \
222             std::chrono::system_clock::now().time_since_epoch()).count();                                        \
223         isFirstTime##func##line = false;                                                                         \
224         RS_LOGE(format, ##__VA_ARGS__);                                                                          \
225     }                                                                                                            \
226 }                                                                                                                \
227 
228 #endif // RENDER_SERVICE_BASE_CORE_COMMON_RS_LOG_H
229