• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 void RSB_EXPORT RSLogEOutput(const char* format, ...);
43 void RSB_EXPORT RSLogWOutput(const char* format, ...);
44 void RSB_EXPORT RSLogDOutput(const char* format, ...);
45 
46 enum RSLogFlag {
47     // screen
48     FLAG_DEBUG_SCREEN = 0x00000001,
49 
50     // node
51     FLAG_DEBUG_NODE = 0x00000002,
52 
53     // effect
54     FLAG_DEBUG_EFFECT = 0x00000004,
55 
56     // pipeline
57     FLAG_DEBUG_PIPELINE = 0x00000008,
58 
59     // modifier
60     FLAG_DEBUG_MODIFIER = 0x00000010,
61 
62     // buffer
63     FLAG_DEBUG_BUFFER = 0x00000020,
64 
65     // layer
66     FLAG_DEBUG_LAYER = 0x00000040,
67 
68     // composer
69     FLAG_DEBUG_COMPOSER = 0x00000080,
70 
71     // vsync
72     FLAG_DEBUG_VSYNC = 0x00000100,
73 
74     // drawing
75     FLAG_DEBUG_DRAWING = 0x00000200,
76 
77     // prevalidate
78     FLAG_DEBUG_PREVALIDATE = 0x00000400,
79 
80     // ipc
81     FLAG_DEBUG_IPC = 0x00000800,
82 };
83 
84 class RSLogManager {
85 public:
86     RSLogManager();
87     ~RSLogManager() = default;
88 
89     static RSLogManager& GetInstance();
90     bool SetRSLogFlag(std::string& flag);
IsRSLogFlagEnabled(RSLogFlag flag)91     inline bool IsRSLogFlagEnabled(RSLogFlag flag)
92     {
93         return logFlag_ & flag;
94     }
95 
96 private:
97     uint32_t logFlag_ = 0;
98 
99     bool IsFlagValid(std::string& flag);
100 
101     static constexpr uint32_t INPUT_FLAG_MIN_LENGTH = 2;
102 
103     static constexpr uint32_t INPUT_FLAG_MAX_LENGTH = 10;
104 
105     static constexpr uint32_t NUMERICAL_BASE = 16;
106 };
107 
108 } // namespace Rosen
109 } // namespace OHOS
110 
111 #undef LOG_DOMAIN
112 #define LOG_DOMAIN 0xD001406
113 
114 #undef LOG_TAG
115 #define LOG_TAG "OHOS::RS"
116 
117 #if defined(MODULE_RSB) || defined(MODULE_RS)
118 
119 #define ROSEN_LOGI(format, ...) HILOG_INFO(LOG_CORE, format, ##__VA_ARGS__)
120 #define ROSEN_LOGD(format, ...)                   \
121     HILOG_DEBUG(LOG_CORE, format, ##__VA_ARGS__); \
122     OHOS::Rosen::RSLogDOutput(format, ##__VA_ARGS__)
123 #define ROSEN_LOGE(format, ...)                   \
124     HILOG_ERROR(LOG_CORE, format, ##__VA_ARGS__); \
125     OHOS::Rosen::RSLogEOutput(format, ##__VA_ARGS__)
126 #define ROSEN_LOGW(format, ...)                  \
127     HILOG_WARN(LOG_CORE, format, ##__VA_ARGS__); \
128     OHOS::Rosen::RSLogWOutput(format, ##__VA_ARGS__)
129 #define ROSEN_LOGF(format, ...) HILOG_FATAL(LOG_CORE, format, ##__VA_ARGS__)
130 
131 #define RS_LOGI(format, ...) HILOG_INFO(LOG_CORE, format, ##__VA_ARGS__)
132 #define RS_LOGD(format, ...)                      \
133     HILOG_DEBUG(LOG_CORE, format, ##__VA_ARGS__); \
134     OHOS::Rosen::RSLogDOutput(format, ##__VA_ARGS__)
135 #define RS_LOGE(format, ...)                      \
136     HILOG_ERROR(LOG_CORE, format, ##__VA_ARGS__); \
137     OHOS::Rosen::RSLogEOutput(format, ##__VA_ARGS__)
138 #define RS_LOGW(format, ...)                     \
139     HILOG_WARN(LOG_CORE, format, ##__VA_ARGS__); \
140     OHOS::Rosen::RSLogWOutput(format, ##__VA_ARGS__)
141 #define RS_LOGF(format, ...) HILOG_FATAL(LOG_CORE, format, ##__VA_ARGS__)
142 
143 #else
144 
145 #define ROSEN_LOGI(format, ...) HILOG_INFO(LOG_CORE, format, ##__VA_ARGS__)
146 #define ROSEN_LOGD(format, ...) HILOG_DEBUG(LOG_CORE, format, ##__VA_ARGS__)
147 #define ROSEN_LOGE(format, ...) HILOG_ERROR(LOG_CORE, format, ##__VA_ARGS__)
148 #define ROSEN_LOGW(format, ...) HILOG_WARN(LOG_CORE, format, ##__VA_ARGS__)
149 #define ROSEN_LOGF(format, ...) HILOG_FATAL(LOG_CORE, format, ##__VA_ARGS__)
150 
151 #define RS_LOGI(format, ...) HILOG_INFO(LOG_CORE, format, ##__VA_ARGS__)
152 #define RS_LOGD(format, ...) HILOG_DEBUG(LOG_CORE, format, ##__VA_ARGS__)
153 #define RS_LOGE(format, ...) HILOG_ERROR(LOG_CORE, format, ##__VA_ARGS__)
154 #define RS_LOGW(format, ...) HILOG_WARN(LOG_CORE, format, ##__VA_ARGS__)
155 #define RS_LOGF(format, ...) HILOG_FATAL(LOG_CORE, format, ##__VA_ARGS__)
156 
157 #endif
158 
159 #define CONDITION(cond)     (__builtin_expect((cond) != 0, 0))
160 
161 #ifndef RS_LOGD_IF
162 #define RS_LOGD_IF(cond, format, ...) \
163     ( (CONDITION(cond)) \
164     ? ((void)HILOG_DEBUG(LOG_CORE, format, ##__VA_ARGS__)) \
165     : (void)0)
166 #endif
167 
168 #ifndef RS_LOGI_IF
169 #define RS_LOGI_IF(cond, format, ...) \
170     ( (CONDITION(cond)) \
171     ? ((void)HILOG_INFO(LOG_CORE, format, ##__VA_ARGS__)) \
172     : (void)0)
173 #endif
174 
175 #ifndef RS_LOGW_IF
176 #define RS_LOGW_IF(cond, format, ...) \
177     ( (CONDITION(cond)) \
178     ? ((void)HILOG_WARN(LOG_CORE, format, ##__VA_ARGS__)) \
179     : (void)0)
180 #endif
181 
182 #ifndef RS_LOGE_IF
183 #define RS_LOGE_IF(cond, format, ...) \
184     ( (CONDITION(cond)) \
185     ? ((void)HILOG_ERROR(LOG_CORE, format, ##__VA_ARGS__)) \
186     : (void)0)
187 #endif
188 
189 #ifndef RS_LOGF_IF
190 #define RS_LOGF_IF(cond, format, ...) \
191     ( (CONDITION(cond)) \
192     ? ((void)HILOG_FATAL(LOG_CORE, format, ##__VA_ARGS__)) \
193     : (void)0)
194 #endif
195 
196 #ifndef ROSEN_LOGD_IF
197 #define ROSEN_LOGD_IF RS_LOGD_IF
198 #endif
199 
200 #ifndef ROSEN_LOGI_IF
201 #define ROSEN_LOGI_IF RS_LOGI_IF
202 #endif
203 
204 #ifndef ROSEN_LOGW_IF
205 #define ROSEN_LOGW_IF RS_LOGW_IF
206 #endif
207 
208 #ifndef ROSEN_LOGE_IF
209 #define ROSEN_LOGE_IF RS_LOGE_IF
210 #endif
211 
212 #ifndef ROSEN_LOGF_IF
213 #define ROSEN_LOGF_IF RS_LOGF_IF
214 #endif
215 
216 #define RS_LOG_ENABLE(flag) (RSLogManager::GetInstance().IsRSLogFlagEnabled(flag))
217 
218 #define DEBUG_SCREEN RS_LOG_ENABLE(FLAG_DEBUG_SCREEN)
219 
220 #define DEBUG_NODE RS_LOG_ENABLE(FLAG_DEBUG_NODE)
221 
222 #define DEBUG_MODIFIER RS_LOG_ENABLE(FLAG_DEBUG_MODIFIER)
223 
224 #define DEBUG_BUFFER RS_LOG_ENABLE(FLAG_DEBUG_BUFFER)
225 
226 #define DEBUG_LAYER RS_LOG_ENABLE(FLAG_DEBUG_LAYER)
227 
228 #define DEBUG_COMPOSER RS_LOG_ENABLE(FLAG_DEBUG_COMPOSER)
229 
230 #define DEBUG_PIPELINE RS_LOG_ENABLE(FLAG_DEBUG_PIPELINE)
231 
232 #define DEBUG_VSYNC RS_LOG_ENABLE(FLAG_DEBUG_VSYNC)
233 
234 #define DEBUG_DRAWING RS_LOG_ENABLE(FLAG_DEBUG_DRAWING)
235 
236 #define DEBUG_PREVALIDATE RS_LOG_ENABLE(FLAG_DEBUG_PREVALIDATE)
237 
238 #define DEBUG_IPC RS_LOG_ENABLE(FLAG_DEBUG_IPC)
239 
240 #define RS_LOGE_LIMIT(func, line, format, ...)                                                                    \
241 {                                                                                                                 \
242     static constexpr uint64_t LOG_PRINT_INTERVAL_IN_SECOND = 20;                                                  \
243     static std::atomic<bool> isFirstTime##func##line = true;                                                      \
244     static std::atomic<uint64_t> prePrintTime##func##line = static_cast<uint64_t>(                                \
245         std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now().time_since_epoch())     \
246             .count());                                                                                            \
247     uint64_t currTime = static_cast<uint64_t>(                                                                    \
248         std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now().time_since_epoch())     \
249             .count());                                                                                            \
250     if ((currTime - prePrintTime##func##line >= LOG_PRINT_INTERVAL_IN_SECOND) || isFirstTime##func##line) {       \
251         prePrintTime##func##line = static_cast<uint64_t>(                                                         \
252             std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now().time_since_epoch()) \
253                 .count());                                                                                        \
254         isFirstTime##func##line = false;                                                                          \
255         RS_LOGE(format, ##__VA_ARGS__);                                                                           \
256     }                                                                                                             \
257 }
258 
259 #define RS_LOGI_LIMIT(format, ...)                                                                                 \
260 {                                                                                                                  \
261     static constexpr uint64_t LOG_PRINT_INTERVAL_IN_SECOND = 5;                                                    \
262     static std::atomic<bool> isFirst##__func__##__line__ = true;                                                   \
263     static std::atomic<uint64_t> preTime##__func__##__line__ = static_cast<uint64_t>(                              \
264         std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now().time_since_epoch())      \
265             .count());                                                                                             \
266     uint64_t currTime = static_cast<uint64_t>(                                                                     \
267         std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now().time_since_epoch())      \
268             .count());                                                                                             \
269     if ((currTime - preTime##__func__##__line__ >= LOG_PRINT_INTERVAL_IN_SECOND) || isFirst##__func__##__line__) { \
270         preTime##__func__##__line__ = static_cast<uint64_t>(                                                       \
271             std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now().time_since_epoch())  \
272                 .count());                                                                                         \
273         isFirst##__func__##__line__ = false;                                                                       \
274         RS_LOGI(format, ##__VA_ARGS__);                                                                            \
275     }                                                                                                              \
276 }
277 
278 #endif // RENDER_SERVICE_BASE_CORE_COMMON_RS_LOG_H
279