1/*
2 * Copyright (c) 2021-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 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 0xD002B00
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 CHECK_AND_RETURN_RET_LOG(cond, ret, fmt, ...)  \
56    do {                                               \
57        if (!(cond)) {                                 \
58            MEDIA_ERR_LOG(fmt, ##__VA_ARGS__);         \
59            return ret;                                \
60        }                                              \
61    } while (0)
62
63#define CHECK_ERROR_RETURN_RET_LOG(cond, ret, fmt, ...)  \
64    do {                                                 \
65        if (cond) {                                      \
66            MEDIA_ERR_LOG(fmt, ##__VA_ARGS__);           \
67            return ret;                                  \
68        }                                                \
69    } while (0)
70
71#define CHECK_ERROR_RETURN(cond)                      \
72    do {                                              \
73        if (cond) {                                   \
74            return;                                   \
75        }                                             \
76    } while (0)
77
78#define CHECK_AND_RETURN_LOG(cond, fmt, ...)           \
79    do {                                               \
80        if (!(cond)) {                                 \
81            MEDIA_ERR_LOG(fmt, ##__VA_ARGS__);         \
82            return;                                    \
83        }                                              \
84    } while (0)
85
86#define CHECK_ERROR_RETURN_LOG(cond, fmt, ...)         \
87    do {                                               \
88        if (cond) {                                    \
89            MEDIA_ERR_LOG(fmt, ##__VA_ARGS__);         \
90            return;                                    \
91        }                                              \
92    } while (0)
93
94#define CHECK_AND_PRINT_LOG(cond, fmt, ...)            \
95    do {                                               \
96        if (!(cond)) {                                 \
97            MEDIA_ERR_LOG(fmt, ##__VA_ARGS__);         \
98        }                                              \
99    } while (0)
100
101#define CHECK_ERROR_PRINT_LOG(cond, fmt, ...)          \
102    do {                                               \
103        if (cond) {                                    \
104            MEDIA_ERR_LOG(fmt, ##__VA_ARGS__);         \
105        }                                              \
106    } while (0)
107
108#define CHECK_AND_RETURN_RET(cond, ret)                \
109    do {                                               \
110        if (!(cond)) {                                 \
111            return ret;                                \
112        }                                              \
113    } while (0)
114
115#define CHECK_ERROR_RETURN_RET(cond, ret)              \
116    do {                                               \
117        if (cond) {                                    \
118            return ret;                                \
119        }                                              \
120    } while (0)
121
122#define CHECK_AND_BREAK_LOG(cond, fmt, ...)                                 \
123    if (1) {                                                                \
124        if (!(cond)) {                                                      \
125            MEDIA_WARNING_LOG(fmt, ##__VA_ARGS__);                          \
126            break;                                                          \
127        }                                                                   \
128    } else void (0)
129
130#define CHECK_AND_CONTINUE_LOG(cond, fmt, ...)                              \
131    if (1) {                                                                \
132        if (!(cond)) {                                                      \
133            MEDIA_WARNING_LOG(fmt, ##__VA_ARGS__);                          \
134            continue;                                                       \
135        }                                                                   \
136    } else void (0)
137
138#define CHECK_EXECUTE(cond, cmd)                                            \
139    do {                                                                    \
140        if (cond) {                                                         \
141            cmd;                                                            \
142        }                                                                   \
143    } while (0)
144
145#define POINTER_MASK 0x00FFFFFF
146
147#define CAMERA_SYNC_TRACE HITRACE_METER_NAME(HITRACE_TAG_ZCAMERA, __PRETTY_FUNCTION__)
148
149#define CAMERA_SYSEVENT_STATISTIC(str)                                             \
150    do {                                                                           \
151        HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::CAMERA, "CAMERA_STATISTIC", \
152                                     HiviewDFX::HiSysEvent::EventType::STATISTIC,  \
153                                     "MSG", str);                                  \
154    } while (0)
155
156#define CAMERA_SYSEVENT_SECURITY(str)                                              \
157    do {                                                                           \
158        HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::CAMERA, "CAMERA_SECURITY",  \
159                                     HiviewDFX::HiSysEvent::EventType::SECURITY,   \
160                                     "MSG", str);                                  \
161    } while (0)
162
163#define CAMERA_SYSEVENT_BEHAVIOR(str)                                              \
164    do {                                                                           \
165        HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::CAMERA, "CAMERA_STATE",     \
166                                     HiviewDFX::HiSysEvent::EventType::BEHAVIOR,   \
167                                     "MSG", str);                                  \
168    } while (0)
169
170#define CAMERA_SYSEVENT_FAULT(str)                                                 \
171    do {                                                                           \
172        HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::CAMERA, "CAMERA_ERR",       \
173                                     HiviewDFX::HiSysEvent::EventType::FAULT,      \
174                                     "MSG", str);                                  \
175    } while (0)
176
177#define POWERMGR_SYSEVENT_CAMERA_CONNECT(pid, uid, camid, name)                    \
178    do {                                                                           \
179        HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::CAMERA, "CAMERA_CONNECT",   \
180                                     HiviewDFX::HiSysEvent::EventType::STATISTIC,  \
181                                     "PID", pid, "UID", uid, "ID", camid,          \
182                                     "NAME", name);                                \
183    } while (0)
184
185#define POWERMGR_SYSEVENT_CAMERA_DISCONNECT(camid)                                 \
186    do {                                                                           \
187        HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::CAMERA,                     \
188                                     "CAMERA_DISCONNECT",                          \
189                                     HiviewDFX::HiSysEvent::EventType::STATISTIC,  \
190                                     "ID", camid);                                 \
191    } while (0)
192
193#define POWERMGR_SYSEVENT_TORCH_STATE(pid, uid, status)                            \
194    do {                                                                           \
195        HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::CAMERA, "TORCH_STATE",      \
196                                     HiviewDFX::HiSysEvent::EventType::STATISTIC,  \
197                                     "PID", pid, "UID", uid, "STATE", status);     \
198    } while (0)
199
200#define POWERMGR_SYSEVENT_CAMERA_CONFIG(type, width, height)                           \
201    do {                                                                               \
202        HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::CAMERA, "CAMERA_CONFIG",        \
203                                     HiviewDFX::HiSysEvent::EventType::STATISTIC,      \
204                                     "TYPE", #type, "WIDTH", width, "HEIGHT", height); \
205    } while (0)
206
207#define POWERMGR_SYSEVENT_FLASH_ON()                                               \
208    do {                                                                           \
209        HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::CAMERA, "FLASHLIGHT_ON",    \
210                                     HiviewDFX::HiSysEvent::EventType::STATISTIC); \
211    } while (0)
212
213#define POWERMGR_SYSEVENT_FLASH_OFF()                                              \
214    do {                                                                           \
215        HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::CAMERA, "FLASHLIGHT_OFF",   \
216                                     HiviewDFX::HiSysEvent::EventType::STATISTIC); \
217    } while (0)
218
219#define CAMERA_START_ASYNC_TRACE(str, taskId)                                      \
220    do {                                                                           \
221        StartAsyncTrace(HITRACE_TAG_ZCAMERA, str, taskId, -1);                     \
222    } while (0)
223
224#define CAMERA_FINISH_ASYNC_TRACE(str, taskId)                                     \
225    do {                                                                           \
226        FinishAsyncTrace(HITRACE_TAG_ZCAMERA, str, taskId);                        \
227    } while (0)
228
229#endif // OHOS_CAMERA_LOG_H
230