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 // ipc 77 FLAG_DEBUG_IPC = 0x00000800, 78 }; 79 80 class RSLogManager { 81 public: 82 RSLogManager(); 83 ~RSLogManager() = default; 84 85 static RSLogManager& GetInstance(); 86 bool SetRSLogFlag(std::string& flag); IsRSLogFlagEnabled(RSLogFlag flag)87 inline bool IsRSLogFlagEnabled(RSLogFlag flag) 88 { 89 return logFlag_ & flag; 90 } 91 92 private: 93 uint32_t logFlag_ = 0; 94 95 bool IsFlagValid(std::string& flag); 96 97 static constexpr uint32_t INPUT_FLAG_MIN_LENGTH = 2; 98 99 static constexpr uint32_t INPUT_FLAG_MAX_LENGTH = 10; 100 101 static constexpr uint32_t NUMERICAL_BASE = 16; 102 }; 103 104 } // namespace Rosen 105 } // namespace OHOS 106 107 #undef LOG_DOMAIN 108 #define LOG_DOMAIN 0xD001406 109 110 #undef LOG_TAG 111 #define LOG_TAG "OHOS::RS" 112 113 #define ROSEN_LOGI(format, ...) \ 114 HILOG_INFO(LOG_CORE, format, ##__VA_ARGS__) 115 #define ROSEN_LOGD(format, ...) \ 116 HILOG_DEBUG(LOG_CORE, format, ##__VA_ARGS__) 117 #define ROSEN_LOGE(format, ...) \ 118 HILOG_ERROR(LOG_CORE, format, ##__VA_ARGS__) 119 #define ROSEN_LOGW(format, ...) \ 120 HILOG_WARN(LOG_CORE, format, ##__VA_ARGS__) 121 #define ROSEN_LOGF(format, ...) \ 122 HILOG_FATAL(LOG_CORE, format, ##__VA_ARGS__) 123 124 #define RS_LOGI(format, ...) \ 125 HILOG_INFO(LOG_CORE, format, ##__VA_ARGS__) 126 #define RS_LOGD(format, ...) \ 127 HILOG_DEBUG(LOG_CORE, format, ##__VA_ARGS__) 128 #define RS_LOGE(format, ...) \ 129 HILOG_ERROR(LOG_CORE, format, ##__VA_ARGS__) 130 #define RS_LOGW(format, ...) \ 131 HILOG_WARN(LOG_CORE, format, ##__VA_ARGS__) 132 #define RS_LOGF(format, ...) \ 133 HILOG_FATAL(LOG_CORE, format, ##__VA_ARGS__) 134 135 #define CONDITION(cond) (__builtin_expect((cond) != 0, 0)) 136 137 #ifndef RS_LOGD_IF 138 #define RS_LOGD_IF(cond, format, ...) \ 139 ( (CONDITION(cond)) \ 140 ? ((void)HILOG_DEBUG(LOG_CORE, format, ##__VA_ARGS__)) \ 141 : (void)0) 142 #endif 143 144 #ifndef RS_LOGI_IF 145 #define RS_LOGI_IF(cond, format, ...) \ 146 ( (CONDITION(cond)) \ 147 ? ((void)HILOG_INFO(LOG_CORE, format, ##__VA_ARGS__)) \ 148 : (void)0) 149 #endif 150 151 #ifndef RS_LOGW_IF 152 #define RS_LOGW_IF(cond, format, ...) \ 153 ( (CONDITION(cond)) \ 154 ? ((void)HILOG_WARN(LOG_CORE, format, ##__VA_ARGS__)) \ 155 : (void)0) 156 #endif 157 158 #ifndef RS_LOGE_IF 159 #define RS_LOGE_IF(cond, format, ...) \ 160 ( (CONDITION(cond)) \ 161 ? ((void)HILOG_ERROR(LOG_CORE, format, ##__VA_ARGS__)) \ 162 : (void)0) 163 #endif 164 165 #ifndef RS_LOGF_IF 166 #define RS_LOGF_IF(cond, format, ...) \ 167 ( (CONDITION(cond)) \ 168 ? ((void)HILOG_FATAL(LOG_CORE, format, ##__VA_ARGS__)) \ 169 : (void)0) 170 #endif 171 172 #ifndef ROSEN_LOGD_IF 173 #define ROSEN_LOGD_IF RS_LOGD_IF 174 #endif 175 176 #ifndef ROSEN_LOGI_IF 177 #define ROSEN_LOGI_IF RS_LOGI_IF 178 #endif 179 180 #ifndef ROSEN_LOGW_IF 181 #define ROSEN_LOGW_IF RS_LOGW_IF 182 #endif 183 184 #ifndef ROSEN_LOGE_IF 185 #define ROSEN_LOGE_IF RS_LOGE_IF 186 #endif 187 188 #ifndef ROSEN_LOGF_IF 189 #define ROSEN_LOGF_IF RS_LOGF_IF 190 #endif 191 192 #define RS_LOG_ENABLE(flag) (RSLogManager::GetInstance().IsRSLogFlagEnabled(flag)) 193 194 #define DEBUG_SCREEN RS_LOG_ENABLE(FLAG_DEBUG_SCREEN) 195 196 #define DEBUG_NODE RS_LOG_ENABLE(FLAG_DEBUG_NODE) 197 198 #define DEBUG_MODIFIER RS_LOG_ENABLE(FLAG_DEBUG_MODIFIER) 199 200 #define DEBUG_BUFFER RS_LOG_ENABLE(FLAG_DEBUG_BUFFER) 201 202 #define DEBUG_LAYER RS_LOG_ENABLE(FLAG_DEBUG_LAYER) 203 204 #define DEBUG_COMPOSER RS_LOG_ENABLE(FLAG_DEBUG_COMPOSER) 205 206 #define DEBUG_PIPELINE RS_LOG_ENABLE(FLAG_DEBUG_PIPELINE) 207 208 #define DEBUG_VSYNC RS_LOG_ENABLE(FLAG_DEBUG_VSYNC) 209 210 #define DEBUG_DRAWING RS_LOG_ENABLE(FLAG_DEBUG_DRAWING) 211 212 #define DEBUG_PREVALIDATE RS_LOG_ENABLE(FLAG_DEBUG_PREVALIDATE) 213 214 #define DEBUG_IPC RS_LOG_ENABLE(FLAG_DEBUG_IPC) 215 216 #define RS_LOGE_LIMIT(func, line, format, ...) \ 217 { \ 218 static constexpr uint64_t LOG_PRINT_INTERVAL_IN_SECOND = 20; \ 219 static std::atomic<bool> isFirstTime##func##line = true; \ 220 static std::atomic<uint64_t> prePrintTime##func##line = static_cast<uint64_t>( \ 221 std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now().time_since_epoch()) \ 222 .count()); \ 223 uint64_t currTime = static_cast<uint64_t>( \ 224 std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now().time_since_epoch()) \ 225 .count()); \ 226 if ((currTime - prePrintTime##func##line >= LOG_PRINT_INTERVAL_IN_SECOND) || isFirstTime##func##line) { \ 227 prePrintTime##func##line = static_cast<uint64_t>( \ 228 std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now().time_since_epoch()) \ 229 .count()); \ 230 isFirstTime##func##line = false; \ 231 RS_LOGE(format, ##__VA_ARGS__); \ 232 } \ 233 } 234 235 #define RS_LOGI_LIMIT(format, ...) \ 236 { \ 237 static constexpr uint64_t LOG_PRINT_INTERVAL_IN_SECOND = 5; \ 238 static std::atomic<bool> isFirst##__func__##__line__ = true; \ 239 static std::atomic<uint64_t> preTime##__func__##__line__ = static_cast<uint64_t>( \ 240 std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now().time_since_epoch()) \ 241 .count()); \ 242 uint64_t currTime = static_cast<uint64_t>( \ 243 std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now().time_since_epoch()) \ 244 .count()); \ 245 if ((currTime - preTime##__func__##__line__ >= LOG_PRINT_INTERVAL_IN_SECOND) || isFirst##__func__##__line__) { \ 246 preTime##__func__##__line__ = static_cast<uint64_t>( \ 247 std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now().time_since_epoch()) \ 248 .count()); \ 249 isFirst##__func__##__line__ = false; \ 250 RS_LOGI(format, ##__VA_ARGS__); \ 251 } \ 252 } 253 254 #endif // RENDER_SERVICE_BASE_CORE_COMMON_RS_LOG_H 255