1 /* 2 * Copyright (c) 2021-2025 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 OHOS_CAMERA_LOG_H 17 #define OHOS_CAMERA_LOG_H 18 19 #include <cstdio> 20 #include <cinttypes> 21 #include "hilog/log.h" 22 #include "hisysevent.h" 23 #include "hitrace_meter.h" 24 25 #undef LOG_DOMAIN 26 #undef LOG_TAG 27 #define LOG_DOMAIN 0xD002B01 28 #define LOG_TAG "CAMERA" 29 #define MAX_STRING_SIZE 256 30 31 #ifndef IS_RELEASE_VERSION 32 #define DECORATOR_HILOG(op, fmt, args...) \ 33 do { \ 34 op(LOG_CORE, "{%{public}s()-%{public}s:%{public}d} " fmt, __FUNCTION__, __FILE_NAME__, __LINE__, ##args); \ 35 } while (0) 36 #else 37 #define DECORATOR_HILOG(op, fmt, args...) \ 38 do { \ 39 op(LOG_CORE, "{%{public}s():%{public}d} " fmt, __FUNCTION__, __LINE__, ##args); \ 40 } while (0) 41 #endif 42 43 #define MEDIA_DEBUG_LOG(fmt, ...) DECORATOR_HILOG(HILOG_DEBUG, fmt, ##__VA_ARGS__) 44 #define MEDIA_ERR_LOG(fmt, ...) DECORATOR_HILOG(HILOG_ERROR, fmt, ##__VA_ARGS__) 45 #define MEDIA_WARNING_LOG(fmt, ...) DECORATOR_HILOG(HILOG_WARN, fmt, ##__VA_ARGS__) 46 #define MEDIA_INFO_LOG(fmt, ...) DECORATOR_HILOG(HILOG_INFO, fmt, ##__VA_ARGS__) 47 #define MEDIA_FATAL_LOG(fmt, ...) DECORATOR_HILOG(HILOG_FATAL, fmt, ##__VA_ARGS__) 48 49 #define MEDIA_OK 0 50 #define MEDIA_INVALID_PARAM (-1) 51 #define MEDIA_INIT_FAIL (-2) 52 #define MEDIA_ERR (-3) 53 #define MEDIA_PERMISSION_DENIED (-4) 54 55 #define CAMERA_LIKELY(x) __builtin_expect(!!(x), 1) 56 #define CAMERA_UNLIKELY(x) __builtin_expect(!!(x), 0) 57 58 #define CHECK_BREAK(cond) \ 59 if (1) { \ 60 if (cond) { \ 61 break; \ 62 } \ 63 } else \ 64 void(0) 65 66 #define CHECK_CONTINUE(cond) \ 67 if (1) { \ 68 if (cond) { \ 69 continue; \ 70 } \ 71 } else \ 72 void(0) 73 74 #define CHECK_RETURN(cond) \ 75 do { \ 76 if (cond) { \ 77 return; \ 78 } \ 79 } while (0) 80 81 #define CHECK_EXECUTE(cond, cmd) \ 82 do { \ 83 if (cond) { \ 84 cmd; \ 85 } \ 86 } while (0) 87 88 #define CHECK_RETURN_RET(cond, ret) \ 89 do { \ 90 if (cond) { \ 91 return ret; \ 92 } \ 93 } while (0) 94 95 #define CHECK_PRINT_ELOG(cond, fmt, ...) \ 96 do { \ 97 if (CAMERA_UNLIKELY(cond)) { \ 98 MEDIA_ERR_LOG(fmt, ##__VA_ARGS__); \ 99 } \ 100 } while (0) 101 102 #define CHECK_PRINT_WLOG(cond, fmt, ...) \ 103 do { \ 104 if (CAMERA_UNLIKELY(cond)) { \ 105 MEDIA_WARNING_LOG(fmt, ##__VA_ARGS__); \ 106 } \ 107 } while (0) 108 109 #define CHECK_PRINT_ILOG(cond, fmt, ...) \ 110 do { \ 111 if (CAMERA_UNLIKELY(cond)) { \ 112 MEDIA_INFO_LOG(fmt, ##__VA_ARGS__); \ 113 } \ 114 } while (0) 115 116 #define CHECK_PRINT_DLOG(cond, fmt, ...) \ 117 do { \ 118 if (CAMERA_UNLIKELY(cond)) { \ 119 MEDIA_DEBUG_LOG(fmt, ##__VA_ARGS__); \ 120 } \ 121 } while (0) 122 123 #define CHECK_RETURN_ELOG(cond, fmt, ...) \ 124 do { \ 125 if (CAMERA_UNLIKELY(cond)) { \ 126 MEDIA_ERR_LOG(fmt, ##__VA_ARGS__); \ 127 return; \ 128 } \ 129 } while (0) 130 131 #define CHECK_RETURN_WLOG(cond, fmt, ...) \ 132 do { \ 133 if (CAMERA_UNLIKELY(cond)) { \ 134 MEDIA_WARNING_LOG(fmt, ##__VA_ARGS__); \ 135 return; \ 136 } \ 137 } while (0) 138 139 #define CHECK_RETURN_ILOG(cond, fmt, ...) \ 140 do { \ 141 if (CAMERA_UNLIKELY(cond)) { \ 142 MEDIA_INFO_LOG(fmt, ##__VA_ARGS__); \ 143 return; \ 144 } \ 145 } while (0) 146 147 #define CHECK_RETURN_DLOG(cond, fmt, ...) \ 148 do { \ 149 if (CAMERA_UNLIKELY(cond)) { \ 150 MEDIA_DEBUG_LOG(fmt, ##__VA_ARGS__); \ 151 return; \ 152 } \ 153 } while (0) 154 155 #define CHECK_RETURN_RET_ELOG(cond, ret, fmt, ...) \ 156 do { \ 157 if (CAMERA_UNLIKELY(cond)) { \ 158 MEDIA_ERR_LOG(fmt, ##__VA_ARGS__); \ 159 return ret; \ 160 } \ 161 } while (0) 162 163 #define CHECK_RETURN_RET_WLOG(cond, ret, fmt, ...) \ 164 do { \ 165 if (CAMERA_UNLIKELY(cond)) { \ 166 MEDIA_WARNING_LOG(fmt, ##__VA_ARGS__); \ 167 return ret; \ 168 } \ 169 } while (0) 170 171 #define CHECK_RETURN_RET_ILOG(cond, ret, fmt, ...) \ 172 do { \ 173 if (CAMERA_UNLIKELY(cond)) { \ 174 MEDIA_INFO_LOG(fmt, ##__VA_ARGS__); \ 175 return ret; \ 176 } \ 177 } while (0) 178 179 #define CHECK_RETURN_RET_DLOG(cond, ret, fmt, ...) \ 180 do { \ 181 if (CAMERA_UNLIKELY(cond)) { \ 182 MEDIA_DEBUG_LOG(fmt, ##__VA_ARGS__); \ 183 return ret; \ 184 } \ 185 } while (0) 186 187 #define CHECK_BREAK_ELOG(cond, fmt, ...) \ 188 if (1) { \ 189 if (CAMERA_UNLIKELY(cond)) { \ 190 MEDIA_ERR_LOG(fmt, ##__VA_ARGS__); \ 191 break; \ 192 } \ 193 } else \ 194 void(0) 195 196 #define CHECK_BREAK_WLOG(cond, fmt, ...) \ 197 if (1) { \ 198 if (CAMERA_UNLIKELY(cond)) { \ 199 MEDIA_WARNING_LOG(fmt, ##__VA_ARGS__); \ 200 break; \ 201 } \ 202 } else \ 203 void(0) 204 205 #define CHECK_BREAK_ILOG(cond, fmt, ...) \ 206 if (1) { \ 207 if (CAMERA_UNLIKELY(cond)) { \ 208 MEDIA_INFO_LOG(fmt, ##__VA_ARGS__); \ 209 break; \ 210 } \ 211 } else \ 212 void(0) 213 214 #define CHECK_BREAK_DLOG(cond, fmt, ...) \ 215 if (1) { \ 216 if (CAMERA_UNLIKELY(cond)) { \ 217 MEDIA_DEBUG_LOG(fmt, ##__VA_ARGS__); \ 218 break; \ 219 } \ 220 } else \ 221 void(0) 222 223 #define CHECK_CONTINUE_ELOG(cond, fmt, ...) \ 224 if (1) { \ 225 if (CAMERA_UNLIKELY(cond)) { \ 226 MEDIA_ERR_LOG(fmt, ##__VA_ARGS__); \ 227 continue; \ 228 } \ 229 } else \ 230 void(0) 231 232 #define CHECK_CONTINUE_WLOG(cond, fmt, ...) \ 233 if (1) { \ 234 if (CAMERA_UNLIKELY(cond)) { \ 235 MEDIA_WARNING_LOG(fmt, ##__VA_ARGS__); \ 236 continue; \ 237 } \ 238 } else \ 239 void(0) 240 241 #define CHECK_CONTINUE_ILOG(cond, fmt, ...) \ 242 if (1) { \ 243 if (CAMERA_UNLIKELY(cond)) { \ 244 MEDIA_INFO_LOG(fmt, ##__VA_ARGS__); \ 245 continue; \ 246 } \ 247 } else \ 248 void(0) 249 250 #define CHECK_CONTINUE_DLOG(cond, fmt, ...) \ 251 if (1) { \ 252 if (CAMERA_UNLIKELY(cond)) { \ 253 MEDIA_DEBUG_LOG(fmt, ##__VA_ARGS__); \ 254 continue; \ 255 } \ 256 } else \ 257 void(0) 258 259 #define POINTER_MASK 0x00FFFFFF 260 261 #define CAMERA_SYNC_TRACE HITRACE_METER_NAME(HITRACE_TAG_ZCAMERA, __PRETTY_FUNCTION__) 262 263 #define CAMERA_SYSEVENT_STATISTIC(str) \ 264 do { \ 265 HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::CAMERA, "CAMERA_STATISTIC", \ 266 HiviewDFX::HiSysEvent::EventType::STATISTIC, \ 267 "MSG", str); \ 268 } while (0) 269 270 #define CAMERA_SYSEVENT_SECURITY(str) \ 271 do { \ 272 HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::CAMERA, "CAMERA_SECURITY", \ 273 HiviewDFX::HiSysEvent::EventType::SECURITY, \ 274 "MSG", str); \ 275 } while (0) 276 277 #define CAMERA_SYSEVENT_BEHAVIOR(str) \ 278 do { \ 279 HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::CAMERA, "CAMERA_STATE", \ 280 HiviewDFX::HiSysEvent::EventType::BEHAVIOR, \ 281 "MSG", str); \ 282 } while (0) 283 284 #define CAMERA_SYSEVENT_FAULT(str) \ 285 do { \ 286 HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::CAMERA, "CAMERA_ERR", \ 287 HiviewDFX::HiSysEvent::EventType::FAULT, \ 288 "MSG", str); \ 289 } while (0) 290 291 #define POWERMGR_SYSEVENT_CAMERA_CONNECT(pid, uid, camid, name) \ 292 do { \ 293 HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::CAMERA, "CAMERA_CONNECT", \ 294 HiviewDFX::HiSysEvent::EventType::STATISTIC, \ 295 "PID", pid, "UID", uid, "ID", camid, \ 296 "NAME", name); \ 297 } while (0) 298 299 #define POWERMGR_SYSEVENT_CAMERA_DISCONNECT(camid) \ 300 do { \ 301 HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::CAMERA, \ 302 "CAMERA_DISCONNECT", \ 303 HiviewDFX::HiSysEvent::EventType::STATISTIC, \ 304 "ID", camid); \ 305 } while (0) 306 307 #define POWERMGR_SYSEVENT_TORCH_STATE(pid, uid, status) \ 308 do { \ 309 HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::CAMERA, "TORCH_STATE", \ 310 HiviewDFX::HiSysEvent::EventType::STATISTIC, \ 311 "PID", pid, "UID", uid, "STATE", status); \ 312 } while (0) 313 314 #define POWERMGR_SYSEVENT_CAMERA_CONFIG(type, width, height) \ 315 do { \ 316 HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::CAMERA, "CAMERA_CONFIG", \ 317 HiviewDFX::HiSysEvent::EventType::STATISTIC, \ 318 "TYPE", #type, "WIDTH", width, "HEIGHT", height); \ 319 } while (0) 320 321 #define POWERMGR_SYSEVENT_FLASH_ON() \ 322 do { \ 323 HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::CAMERA, "FLASHLIGHT_ON", \ 324 HiviewDFX::HiSysEvent::EventType::STATISTIC); \ 325 } while (0) 326 327 #define POWERMGR_SYSEVENT_FLASH_OFF() \ 328 do { \ 329 HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::CAMERA, "FLASHLIGHT_OFF", \ 330 HiviewDFX::HiSysEvent::EventType::STATISTIC); \ 331 } while (0) 332 333 #define CAMERA_START_ASYNC_TRACE(str, taskId) \ 334 do { \ 335 StartAsyncTrace(HITRACE_TAG_ZCAMERA, str, taskId, -1); \ 336 } while (0) 337 338 #define CAMERA_FINISH_ASYNC_TRACE(str, taskId) \ 339 do { \ 340 FinishAsyncTrace(HITRACE_TAG_ZCAMERA, str, taskId); \ 341 } while (0) 342 343 #endif // OHOS_CAMERA_LOG_H 344