Line data Source code
1 : /**
2 : * Copyright (c) 2025 Huawei Technologies Co., Ltd.
3 : * This program is free software, you can redistribute it and/or modify it under the terms and conditions of
4 : * CANN Open Software License Agreement Version 2.0 (the "License").
5 : * Please refer to the License for details. You may not use this file except in compliance with the License.
6 : * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
7 : * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
8 : * See LICENSE in the root of the software repository for the full text of the License.
9 : */
10 :
11 : #ifndef ACL_COMMON_LOG_INNER_H_
12 : #define ACL_COMMON_LOG_INNER_H_
13 :
14 : #include <string>
15 : #include <vector>
16 : #include "dlog_pub.h"
17 : #include "mmpa/mmpa_api.h"
18 : #include "acl/acl_base.h"
19 : #include "common/error_codes_inner.h"
20 :
21 : #ifndef char_t
22 : using char_t = char;
23 : #endif
24 :
25 : #ifndef float32_t
26 : using float32_t = float;
27 : #endif
28 :
29 : namespace acl {
30 : constexpr int32_t ACL_MODE_ID = static_cast<int32_t>(ASCENDCL);
31 : constexpr int32_t APP_MODE_ID = static_cast<int32_t>(APP);
32 : constexpr uint16_t ACL_MODE_ID_U16 = static_cast<uint16_t>(ASCENDCL);
33 : constexpr uint16_t APP_MODE_ID_U16 = static_cast<uint16_t>(APP);
34 : constexpr const char_t *const ACL_MODULE_NAME = "ASCENDCL";
35 : constexpr const char_t *const INVALID_PARAM_MSG = "EH0001";
36 : constexpr const char_t *const INVALID_NULL_POINTER_MSG = "EH0002";
37 : constexpr const char_t *const INVALID_PATH_MSG = "EH0003";
38 : constexpr const char_t *const INVALID_FILE_MSG = "EH0004";
39 : constexpr const char_t *const INVALID_AIPP_MSG = "EH0005";
40 : constexpr const char_t *const UNSUPPORTED_FEATURE_MSG = "EH0006";
41 : constexpr const char_t *const INVALID_VALUE_MSG = "EH0007";
42 : constexpr const char_t *const NULL_POINTER_FUNC_MSG = "EH0008";
43 : constexpr const char_t *const INVALID_PARAM_REASON_MSG = "EH0009";
44 : constexpr const char_t *const ALLOC_MEMORY_FAILED_MSG = "EH0010";
45 : constexpr const char_t *const UNSUPPORTED_SYSTEM_MSG = "EH0011";
46 : constexpr const char_t *const INVALID_PARAM_NO_VALUE_MSG = "EH0012";
47 : constexpr const char_t *const STANDARD_FUNC_FAILED_MSG = "EH0013";
48 : constexpr const char_t *const INVALID_NULL_POINTER_AT_SAME_TIME_MSG = "EH0014";
49 :
50 : // first stage
51 : constexpr const char_t *const ACL_STAGE_SET = "SET";
52 : constexpr const char_t *const ACL_STAGE_GET = "GET";
53 : constexpr const char_t *const ACL_STAGE_CREATE = "CREATE";
54 : constexpr const char_t *const ACL_STAGE_DESTROY = "DESTROY";
55 : constexpr const char_t *const ACL_STAGE_BLAS = "BLAS";
56 : constexpr const char_t *const ACL_STAGE_INFER = "INFER";
57 : constexpr const char_t *const ACL_STAGE_COMP = "COMP";
58 : constexpr const char_t *const ACL_STAGE_LOAD = "LOAD";
59 : constexpr const char_t *const ACL_STAGE_UNLOAD = "UNLOAD";
60 : constexpr const char_t *const ACL_STAGE_EXEC = "EXEC";
61 : constexpr const char_t *const ACL_STAGE_COMP_AND_EXEC = "COMP_AND_EXEC";
62 : constexpr const char_t *const ACL_STAGE_DUMP = "DUMP";
63 : constexpr const char_t *const ACL_STAGE_DVPP = "DVPP";
64 : constexpr const char_t *const ACL_STAGE_TDT = "TDT";
65 : constexpr const char_t *const ACL_STAGE_INIT = "INIT";
66 : constexpr const char_t *const ACL_STAGE_FINAL = "FINAL";
67 : constexpr const char_t *const ACL_STAGE_QUEUE = "QUEUE";
68 : constexpr const char_t *const ACL_STAGE_MBUF = "MBUF";
69 : // second stage
70 : constexpr const char_t *const ACL_STAGE_DEFAULT = "DEFAULT";
71 :
72 : constexpr size_t MAX_LOG_STRING = 1024U;
73 :
74 : constexpr size_t MAX_ERROR_STRING = 128U;
75 :
76 6593 : inline const char_t* format_cast(const char_t *const src)
77 : {
78 6593 : return static_cast<const char_t *>(src);
79 : }
80 :
81 : ACL_FUNC_VISIBILITY std::string AclGetErrorFormatMessage(const mmErrorMsg errnum);
82 :
83 : class ACL_FUNC_VISIBILITY AclLog {
84 : public:
85 : static bool IsLogOutputEnable(const aclLogLevel logLevel);
86 : static mmPid_t GetTid();
87 : static void ACLSaveLog(const aclLogLevel logLevel, const char_t *const strLog);
88 : static bool IsEventLogOutputEnable();
89 : private:
90 : static aclLogLevel GetCurLogLevel();
91 : static bool isEnableEvent_;
92 : };
93 :
94 : class ACL_FUNC_VISIBILITY AclErrorLogManager {
95 : public:
96 : static std::string FormatStr(const char_t *const fmt, ...);
97 : #if !defined(ENABLE_DVPP_INTERFACE) || defined(RUN_TEST)
98 : static void ReportInputError(const char *errorCode, const std::vector<const char *> &key = {},
99 : const std::vector<const char *> &val = {});
100 : #else
101 : #endif
102 : static void ReportInputErrorWithChar(const char_t *const errorCode, const char_t *const argNames[],
103 : const char_t *const argVals[], const size_t size);
104 : static void ReportInnerError(const char_t *const fmt, ...);
105 : static void ReportCallError(const char_t *const fmt, ...);
106 : static std::string GetFuncNameWithoutImplSuffix(const char_t *funcName);
107 : };
108 : } // namespace acl
109 :
110 : #define UNLIKELY(expr) (expr)
111 :
112 : #ifdef RUN_TEST
113 : #define ACL_LOG_INFO(fmt, ...) \
114 : do { \
115 : if (acl::AclLog::IsLogOutputEnable(ACL_INFO)) { \
116 : constexpr const char_t *const funcName = __FUNCTION__; \
117 : printf("INFO %d %s:%s:%d: "#fmt "\n", acl::AclLog::GetTid(), acl::format_cast(funcName), \
118 : __FILE__, __LINE__, ##__VA_ARGS__); \
119 : } \
120 : } while (false)
121 : #define ACL_LOG_DEBUG(fmt, ...) \
122 : do { \
123 : if (acl::AclLog::IsLogOutputEnable(ACL_DEBUG)) { \
124 : constexpr const char_t *const funcName = __FUNCTION__; \
125 : printf("DEBUG %d %s:%s:%d: "#fmt "\n", acl::AclLog::GetTid(), acl::format_cast(funcName), \
126 : __FILE__, __LINE__, ##__VA_ARGS__); \
127 : } \
128 : } while (false)
129 : #define ACL_LOG_WARN(fmt, ...) \
130 : do { \
131 : if (acl::AclLog::IsLogOutputEnable(ACL_WARNING)) { \
132 : constexpr const char_t *const funcName = __FUNCTION__; \
133 : printf("WARN %d %s:%s:%d: "#fmt "\n", acl::AclLog::GetTid(), acl::format_cast(funcName), \
134 : __FILE__, __LINE__, ##__VA_ARGS__); \
135 : } \
136 : } while (false)
137 : #define ACL_LOG_ERROR(fmt, ...) \
138 : do { \
139 : constexpr const char_t *const funcName = __FUNCTION__; \
140 : printf("ERROR %d %s:%s:%d:" fmt "\n", acl::AclLog::GetTid(), acl::format_cast(funcName), \
141 : __FILE__, __LINE__, ##__VA_ARGS__); \
142 : } while (false)
143 : #define ACL_LOG_INNER_ERROR(fmt, ...) \
144 : do { \
145 : constexpr const char_t *const funcName = __FUNCTION__; \
146 : printf("ERROR %d %s:%s:%d:" fmt "\n", acl::AclLog::GetTid(), acl::format_cast(funcName), \
147 : __FILE__, __LINE__, ##__VA_ARGS__); \
148 : } while (false)
149 : #define ACL_LOG_CALL_ERROR(fmt, ...) \
150 : do { \
151 : constexpr const char_t *const funcName = __FUNCTION__; \
152 : printf("ERROR %d %s:%s:%d:" fmt "\n", acl::AclLog::GetTid(), acl::format_cast(funcName), \
153 : __FILE__, __LINE__, ##__VA_ARGS__); \
154 : } while (false)
155 : #define ACL_LOG_EVENT(fmt, ...) \
156 : do { \
157 : if (acl::AclLog::IsEventLogOutputEnable()) { \
158 : constexpr const char_t *const funcName = __FUNCTION__; \
159 : printf("EVENT %d %s:%s:%d: "#fmt "\n", acl::AclLog::GetTid(), acl::format_cast(funcName), \
160 : __FILE__, __LINE__, ##__VA_ARGS__); \
161 : } \
162 : } while (false)
163 : #else
164 : #define ACL_LOG_INFO(fmt, ...) \
165 : do { \
166 : constexpr const char_t *const funcName = __FUNCTION__; \
167 : dlog_info(acl::ACL_MODE_ID, "%d %s: " fmt, acl::AclLog::GetTid(), acl::format_cast(funcName), ##__VA_ARGS__); \
168 : } while (false)
169 : #define ACL_LOG_DEBUG(fmt, ...) \
170 : do { \
171 : constexpr const char_t *const funcName = __FUNCTION__; \
172 : dlog_debug(acl::ACL_MODE_ID, "%d %s: " fmt, acl::AclLog::GetTid(), acl::format_cast(funcName), ##__VA_ARGS__); \
173 : } while (false)
174 : #define ACL_LOG_WARN(fmt, ...) \
175 : do { \
176 : constexpr const char_t *const funcName = __FUNCTION__; \
177 : dlog_warn(acl::ACL_MODE_ID, "%d %s: " fmt, acl::AclLog::GetTid(), acl::format_cast(funcName), ##__VA_ARGS__); \
178 : } while (false)
179 : #define ACL_LOG_ERROR(fmt, ...) \
180 : do { \
181 : constexpr const char_t *const funcName = __FUNCTION__; \
182 : dlog_error(acl::ACL_MODE_ID, "%d %s:" fmt, acl::AclLog::GetTid(), acl::format_cast(funcName), \
183 : ##__VA_ARGS__); \
184 : } while (false)
185 : #define ACL_LOG_INNER_ERROR(fmt, ...) \
186 : do { \
187 : constexpr const char_t *const funcName = __FUNCTION__; \
188 : dlog_error(acl::ACL_MODE_ID, "%d %s:" fmt, acl::AclLog::GetTid(), acl::format_cast(funcName), \
189 : ##__VA_ARGS__); \
190 : acl::AclErrorLogManager::ReportInnerError((fmt), ##__VA_ARGS__); \
191 : } while (false)
192 : #define ACL_LOG_CALL_ERROR(fmt, ...) \
193 : do { \
194 : constexpr const char_t *const funcName = __FUNCTION__; \
195 : dlog_error(acl::ACL_MODE_ID, "%d %s:" fmt, acl::AclLog::GetTid(), acl::format_cast(funcName), \
196 : ##__VA_ARGS__); \
197 : acl::AclErrorLogManager::ReportCallError((fmt), ##__VA_ARGS__); \
198 : } while (false)
199 : #define ACL_LOG_EVENT(fmt, ...) \
200 : do { \
201 : if (acl::AclLog::IsEventLogOutputEnable()) { \
202 : constexpr const char_t *const funcName = __FUNCTION__; \
203 : dlog_info((acl::ACL_MODE_ID | (RUN_LOG_MASK)), "%d %s: " fmt, acl::AclLog::GetTid(), \
204 : acl::format_cast(funcName), ##__VA_ARGS__); \
205 : } \
206 : } while (false)
207 : #endif
208 :
209 : #define OFFSET_OF_MEMBER(type, member) reinterpret_cast<size_t>(&((reinterpret_cast<type *>(0))->member))
210 :
211 : #define ACL_REQUIRES_OK(expr) \
212 : do { \
213 : const aclError __ret = (expr); \
214 : if (__ret != ACL_SUCCESS) { \
215 : return __ret; \
216 : } \
217 : } \
218 : while (false)
219 :
220 : #define ACL_REQUIRES_OK_RETURN_NULL(expr) \
221 : do { \
222 : const aclError __ret = (expr); \
223 : if (__ret != ACL_SUCCESS) { \
224 : return nullptr; \
225 : } \
226 : } \
227 : while (false)
228 :
229 : #define ACL_REQUIRES_EOK(expr, interface) \
230 : do { \
231 : const auto __ret = (expr); \
232 : if (__ret != EOK) { \
233 : ACL_LOG_INNER_ERROR("call api [%s] failed, retCode is %d", #interface, __ret); \
234 : return ACL_ERROR_FAILURE; \
235 : } \
236 : } \
237 : while (false)
238 :
239 : #define ACL_REQUIRES_OK_WITH_INNER_MESSAGE(expr, ...) \
240 : do { \
241 : const aclError __ret = (expr); \
242 : if (__ret != ACL_SUCCESS) { \
243 : ACL_LOG_INNER_ERROR(__VA_ARGS__); \
244 : return __ret; \
245 : } \
246 : } \
247 : while (false)
248 :
249 : #define ACL_REQUIRES_CALL_GE_OK(expr, ...) \
250 : do { \
251 : const auto __ret = (expr); \
252 : if (__ret != ge::SUCCESS) { \
253 : ACL_LOG_CALL_ERROR(__VA_ARGS__); \
254 : return __ret; \
255 : } \
256 : } \
257 : while (false)
258 :
259 : #define ACL_REQUIRES_CALL_RTS_OK(expr, interface) \
260 : do { \
261 : const rtError_t __ret = (expr); \
262 : if (__ret != RT_ERROR_NONE) { \
263 : if (__ret == ACL_ERROR_RT_FEATURE_NOT_SUPPORT) { \
264 : ACL_LOG_WARN("rts api [%s] is not supported currently,", #interface); \
265 : return __ret; \
266 : } \
267 : ACL_LOG_CALL_ERROR("[Call][Rts]call rts api [%s] failed, retCode is %d", #interface, __ret); \
268 : return __ret; \
269 : } \
270 : } \
271 : while (false)
272 :
273 : #define ACL_REQUIRES_RTS_OK_WARN_NOT_SUPPORT(expr, interface) \
274 : do { \
275 : const rtError_t __ret = (expr); \
276 : if (__ret != RT_ERROR_NONE) { \
277 : if (__ret == ACL_ERROR_RT_FEATURE_NOT_SUPPORT) { \
278 : ACL_LOG_WARN("rts api [%s] is not supported currently,", #interface); \
279 : } \
280 : return __ret; \
281 : } \
282 : } \
283 : while (false)
284 :
285 : #define ACL_REQUIRES_RTS_OK(expr) \
286 : do { \
287 : const rtError_t __ret = (expr); \
288 : if (__ret != RT_ERROR_NONE) { \
289 : return ACL_GET_ERRCODE_RTS(__ret); \
290 : } \
291 : } \
292 : while (false)
293 :
294 : #define ACL_REQUIRES_CALL_HIMPI_OK(expr, interface) \
295 : do { \
296 : const int32_t __ret = (expr); \
297 : if (__ret != 0) { \
298 : ACL_LOG_CALL_ERROR("[Call][Himpi]call himpi api [%s] failed, retCode is %d", #interface, __ret); \
299 : return __ret; \
300 : } \
301 : } \
302 : while (false)
303 :
304 : // Validate whether the expr value is true
305 : #define ACL_REQUIRES_TRUE(expr, errCode, errDesc) \
306 : do { \
307 : const bool __ret = (expr); \
308 : if (!__ret) { \
309 : ACL_LOG_ERROR(errDesc); \
310 : return (errCode); \
311 : } \
312 : } \
313 : while (false)
314 :
315 : #define ACL_CHECK_RESERVED_PARAM_REPORT_RET(param, expect, ret) \
316 : do { \
317 : if ((param) != (expect)) { \
318 : const std::string paramVal = std::to_string(param); \
319 : const std::string expectVal = std::to_string(static_cast<long long>(expect)); \
320 : ACL_LOG_ERROR("[Check][PARAM]%s is a reserved parameter and must be %s, current value=%s", \
321 : #param, expectVal.c_str(), paramVal.c_str()); \
322 : std::string errMsg = acl::AclErrorLogManager::FormatStr("%s is a reserved parameter and must be %s", #param, expectVal.c_str()); \
323 : std::string funcName = acl::AclErrorLogManager::GetFuncNameWithoutImplSuffix(__func__); \
324 : acl::AclErrorLogManager::ReportInputError(acl::INVALID_PARAM_REASON_MSG, \
325 : std::vector<const char *>({"func", "value", "param", "reason"}), \
326 : std::vector<const char *>({funcName.c_str(), paramVal.c_str(), #param, errMsg.c_str()})); \
327 : return ret; \
328 : } \
329 : } while (false)
330 :
331 : #define ACL_CHECK_INVALID_PARAM_WITH_REASON(condition, param, reason) \
332 : do { \
333 : if (condition) { \
334 : const std::string paramVal = std::to_string(param); \
335 : ACL_LOG_ERROR("[Check][PARAM]%s is invalid, %s. value=%s", \
336 : #param, (reason), paramVal.c_str()); \
337 : std::string funcName = acl::AclErrorLogManager::GetFuncNameWithoutImplSuffix(__func__); \
338 : acl::AclErrorLogManager::ReportInputError(acl::INVALID_PARAM_REASON_MSG, \
339 : std::vector<const char *>({"func", "value", "param", "reason"}), \
340 : std::vector<const char *>({funcName.c_str(), paramVal.c_str(), #param, (reason)})); \
341 : return ACL_ERROR_INVALID_PARAM; \
342 : } \
343 : } while (false)
344 :
345 : #define ACL_CHECK_INVALID_PARAM_WITH_REASON_AND_FUNC_DESC(condition, param, reason, funcDesc) \
346 : do { \
347 : if (condition) { \
348 : const std::string paramVal = std::to_string(param); \
349 : ACL_LOG_ERROR("[Check][PARAM]%s is invalid, %s. value=%s", #param, (reason), paramVal.c_str()); \
350 : acl::AclErrorLogManager::ReportInputError( \
351 : acl::INVALID_PARAM_REASON_MSG, std::vector<const char*>({"func", "value", "param", "reason"}), \
352 : std::vector<const char*>({(funcDesc), paramVal.c_str(), #param, (reason)})); \
353 : return ACL_ERROR_INVALID_PARAM; \
354 : } \
355 : } while (false)
356 :
357 : #define ACL_CHECK_INVALID_PARAM_WITH_REASON_RET(condition, param, reason, ret) \
358 : do { \
359 : if (condition) { \
360 : const std::string paramVal = std::to_string(param); \
361 : ACL_LOG_ERROR("[Check][PARAM]%s is invalid, %s. value=%s", \
362 : #param, (reason), paramVal.c_str()); \
363 : std::string funcName = acl::AclErrorLogManager::GetFuncNameWithoutImplSuffix(__func__); \
364 : acl::AclErrorLogManager::ReportInputError(acl::INVALID_PARAM_REASON_MSG, \
365 : std::vector<const char *>({"func", "value", "param", "reason"}), \
366 : std::vector<const char *>({funcName.c_str(), paramVal.c_str(), #param, (reason)})); \
367 : return (ret); \
368 : } \
369 : } while (false)
370 :
371 : #define ACL_CHECK_INVALID_PARAM_WITH_REASON_RET_AND_FUNC_DESC(condition, param, reason, ret, funcDesc) \
372 : do { \
373 : if (condition) { \
374 : const std::string paramVal = std::to_string(param); \
375 : ACL_LOG_ERROR("[Check][PARAM]%s is invalid, %s. value=%s", #param, (reason), paramVal.c_str()); \
376 : acl::AclErrorLogManager::ReportInputError( \
377 : acl::INVALID_PARAM_REASON_MSG, std::vector<const char*>({"func", "value", "param", "reason"}), \
378 : std::vector<const char*>({(funcDesc), paramVal.c_str(), #param, (reason)})); \
379 : return (ret); \
380 : } \
381 : } while (false)
382 :
383 : #define ACL_CHECK_INVALID_PARAM_WITH_REASON_DESC_RET(condition, paramVal, paramName, reason, ret) \
384 : do { \
385 : if (condition) { \
386 : ACL_LOG_ERROR("[Check][PARAM]%s is invalid, %s. value=%s", \
387 : (paramName), (reason), (paramVal)); \
388 : std::string funcName = acl::AclErrorLogManager::GetFuncNameWithoutImplSuffix(__func__); \
389 : acl::AclErrorLogManager::ReportInputError(acl::INVALID_PARAM_REASON_MSG, \
390 : std::vector<const char *>({"func", "value", "param", "reason"}), \
391 : std::vector<const char *>({funcName.c_str(), (paramVal), (paramName), (reason)})); \
392 : return (ret); \
393 : } \
394 : } while (false)
395 :
396 : #define ACL_CHECK_INVALID_VALUE_WITH_EXPECT(condition, param, expect) \
397 : do { \
398 : if (!(condition)) { \
399 : const std::string paramVal = std::to_string(param); \
400 : ACL_LOG_ERROR("[Check][PARAM]%s is invalid, must be %s. value=%s", \
401 : #param, expect, paramVal.c_str()); \
402 : std::string funcName = acl::AclErrorLogManager::GetFuncNameWithoutImplSuffix(__func__); \
403 : acl::AclErrorLogManager::ReportInputError(acl::INVALID_VALUE_MSG, \
404 : std::vector<const char *>({"func", "value", "param", "expect"}), \
405 : std::vector<const char *>({funcName.c_str(), paramVal.c_str(), #param, expect})); \
406 : return ACL_ERROR_INVALID_PARAM; \
407 : } \
408 : } while (false)
409 :
410 : #define ACL_CHECK_INVALID_VALUE_WITH_EXPECT_RET(condition, param, expect, ret) \
411 : do { \
412 : if (!(condition)) { \
413 : const std::string paramVal = std::to_string(param); \
414 : ACL_LOG_ERROR("[Check][PARAM]%s is invalid, must be %s. value=%s", \
415 : #param, expect, paramVal.c_str()); \
416 : std::string funcName = acl::AclErrorLogManager::GetFuncNameWithoutImplSuffix(__func__); \
417 : acl::AclErrorLogManager::ReportInputError(acl::INVALID_VALUE_MSG, \
418 : std::vector<const char *>({"func", "value", "param", "expect"}), \
419 : std::vector<const char *>({funcName.c_str(), paramVal.c_str(), #param, expect})); \
420 : return ret; \
421 : } \
422 : } while (false)
423 :
424 : #define ACL_CHECK_INVALID_VALUE_WITH_DESC(condition, paramVal, paramName, expect, ret) \
425 : do { \
426 : if (!(condition)) { \
427 : ACL_LOG_ERROR("[Check][PARAM]%s is invalid, must be %s. value=%s", \
428 : (paramName), (expect), (paramVal)); \
429 : std::string funcName = acl::AclErrorLogManager::GetFuncNameWithoutImplSuffix(__func__); \
430 : acl::AclErrorLogManager::ReportInputError(acl::INVALID_VALUE_MSG, \
431 : std::vector<const char *>({"func", "value", "param", "expect"}), \
432 : std::vector<const char *>({funcName.c_str(), (paramVal), (paramName), (expect)})); \
433 : return (ret); \
434 : } \
435 : } while (false)
436 :
437 : #define ACL_CHECK_INVALID_VALUE_WITH_DESC_AND_FUNC_DESC(condition, paramVal, paramName, expect, ret, funcDesc) \
438 : do { \
439 : if (!(condition)) { \
440 : ACL_LOG_ERROR("[Check][PARAM]%s is invalid, must be %s. value=%s", (paramName), (expect), (paramVal)); \
441 : acl::AclErrorLogManager::ReportInputError( \
442 : acl::INVALID_VALUE_MSG, std::vector<const char*>({"func", "value", "param", "expect"}), \
443 : std::vector<const char*>({(funcDesc), (paramVal), (paramName), (expect)})); \
444 : return (ret); \
445 : } \
446 : } while (false)
447 :
448 : #define ACL_REQUIRES_PARAM_EQUAL_REPORT(param, expect) \
449 : do { \
450 : if ((param) != (expect)) { \
451 : const std::string paramVal = std::to_string(param); \
452 : ACL_LOG_ERROR("[Check][PARAM]%s is invalid, must be %s. value=%s", \
453 : #param, #expect, paramVal.c_str()); \
454 : std::string funcName = acl::AclErrorLogManager::GetFuncNameWithoutImplSuffix(__func__); \
455 : acl::AclErrorLogManager::ReportInputError(acl::INVALID_VALUE_MSG, \
456 : std::vector<const char *>({"func", "value", "param", "expect"}), \
457 : std::vector<const char *>({funcName.c_str(), paramVal.c_str(), #param, #expect})); \
458 : return ACL_ERROR_INVALID_PARAM; \
459 : } \
460 : } while (false)
461 :
462 : #ifndef ENABLE_DVPP_INTERFACE
463 : #define ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(val) \
464 : do { \
465 : if (UNLIKELY((val) == nullptr)) { \
466 : ACL_LOG_ERROR("[Check][%s]param must not be null.", #val); \
467 : std::string funcName = acl::AclErrorLogManager::GetFuncNameWithoutImplSuffix(__func__); \
468 : acl::AclErrorLogManager::ReportInputError(acl::NULL_POINTER_FUNC_MSG, {"func", "param"}, {funcName.c_str(), #val}); \
469 : return ACL_ERROR_INVALID_PARAM; } \
470 : } \
471 : while (false)
472 : #else
473 : #define ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(val) \
474 : do { \
475 : if (UNLIKELY((val) == nullptr)) { \
476 : ACL_LOG_ERROR("[Check][%s]param must not be null.", #val); \
477 : const char_t *argList[] = {"func", "param"}; \
478 : std::string funcName = acl::AclErrorLogManager::GetFuncNameWithoutImplSuffix(__func__); \
479 : const char_t *argVal[] = {funcName.c_str(), #val}; \
480 : acl::AclErrorLogManager::ReportInputErrorWithChar(acl::NULL_POINTER_FUNC_MSG, argList, argVal, 2U); \
481 : return ACL_ERROR_INVALID_PARAM; } \
482 : } \
483 : while (false)
484 : #endif
485 :
486 : #ifndef ENABLE_DVPP_INTERFACE
487 : #define ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT_AND_FUNC_DESC(val, funcDesc) \
488 : do { \
489 : if (UNLIKELY((val) == nullptr)) { \
490 : ACL_LOG_ERROR("[Check][%s]param must not be null.", #val); \
491 : acl::AclErrorLogManager::ReportInputError( \
492 : acl::NULL_POINTER_FUNC_MSG, {"func", "param"}, {(funcDesc), #val}); \
493 : return ACL_ERROR_INVALID_PARAM; \
494 : } \
495 : } while (false)
496 : #else
497 : #define ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT_AND_FUNC_DESC(val, funcDesc) \
498 : do { \
499 : if (UNLIKELY((val) == nullptr)) { \
500 : ACL_LOG_ERROR("[Check][%s]param must not be null.", #val); \
501 : const char_t* argList[] = {"func", "param"}; \
502 : const char_t* argVal[] = {(funcDesc), #val}; \
503 : acl::AclErrorLogManager::ReportInputErrorWithChar(acl::NULL_POINTER_FUNC_MSG, argList, argVal, 2U); \
504 : return ACL_ERROR_INVALID_PARAM; \
505 : } \
506 : } while (false)
507 : #endif
508 :
509 : #define ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT_WITH_PRAM_NAME(val, name) \
510 : do { \
511 : if (UNLIKELY((val) == nullptr)) { \
512 : ACL_LOG_ERROR("[Check][%s]param must not be null.", #val); \
513 : std::string funcName = acl::AclErrorLogManager::GetFuncNameWithoutImplSuffix(__func__); \
514 : acl::AclErrorLogManager::ReportInputError(acl::NULL_POINTER_FUNC_MSG, {"func", "param"}, {funcName.c_str(), (name)}); \
515 : return ACL_ERROR_INVALID_PARAM; } \
516 : } \
517 : while (false)
518 :
519 : #define ACL_REQUIRES_NOT_NULL(val) \
520 : do { \
521 : if (UNLIKELY((val) == nullptr)) { \
522 : ACL_LOG_ERROR("[Check][%s]param must not be null.", #val); \
523 : return ACL_ERROR_INVALID_PARAM; } \
524 : } \
525 : while (false)
526 :
527 : #define ACL_REQUIRES_NOT_NULL_WITH_INNER_REPORT(val) \
528 : do { \
529 : if (UNLIKELY((val) == nullptr)) { \
530 : ACL_LOG_INNER_ERROR("[Check][%s]param must not be null.", #val); \
531 : return ACL_ERROR_INVALID_PARAM; } \
532 : } \
533 : while (false)
534 :
535 : #define ACL_REQUIRES_NOT_NULL_RET_NULL(val) \
536 : do { \
537 : if (UNLIKELY((val) == nullptr)) { \
538 : ACL_LOG_ERROR("[Check][%s]param must not be null.", #val); \
539 : return nullptr; } \
540 : } \
541 : while (false)
542 :
543 : #define ACL_CHECK_INVALID_PARAM_NO_VALUE(condition, paramName, reason) \
544 : do { \
545 : if (!(condition)) { \
546 : const char_t *argList[] = {"func", "param", "reason"}; \
547 : std::string funcName = acl::AclErrorLogManager::GetFuncNameWithoutImplSuffix(__func__); \
548 : const char_t *argVal[] = {funcName.c_str(), (paramName), (reason)}; \
549 : acl::AclErrorLogManager::ReportInputErrorWithChar(acl::INVALID_PARAM_NO_VALUE_MSG, argList, argVal, 3U); \
550 : return ACL_ERROR_INVALID_PARAM; \
551 : } \
552 : } while (false)
553 :
554 : #define ACL_CHECK_INVALID_FILE_MSG_RET(condition, path, reason, ret) \
555 : do { \
556 : if (condition) { \
557 : ACL_LOG_ERROR("[Check][File]file %s is invalid, %s", path, reason); \
558 : acl::AclErrorLogManager::ReportInputError(acl::INVALID_FILE_MSG, \
559 : std::vector<const char *>({"path", "reason"}), \
560 : std::vector<const char *>({path, reason})); \
561 : return ret; \
562 : } \
563 : } while (false)
564 :
565 : #define ACL_REQUIRES_NOT_NULL_RET_NULL_INPUT_REPORT(val) \
566 : do { \
567 : if (UNLIKELY((val) == nullptr)) { \
568 : ACL_LOG_ERROR("[Check][%s]param must not be null.", #val); \
569 : std::string funcName = acl::AclErrorLogManager::GetFuncNameWithoutImplSuffix(__func__); \
570 : acl::AclErrorLogManager::ReportInputError(acl::NULL_POINTER_FUNC_MSG, {"func", "param"}, {funcName.c_str(), #val}); \
571 : return nullptr; } \
572 : } \
573 : while (false)
574 :
575 : #define ACL_REQUIRES_NOT_NULL_RET_INPUT_REPORT(val, ret) \
576 : do { \
577 : if (UNLIKELY((val) == nullptr)) { \
578 : ACL_LOG_ERROR("[Check][%s]param must not be null.", #val); \
579 : std::string funcName = acl::AclErrorLogManager::GetFuncNameWithoutImplSuffix(__func__); \
580 : acl::AclErrorLogManager::ReportInputError(acl::NULL_POINTER_FUNC_MSG, {"func", "param"}, {funcName.c_str(), #val}); \
581 : return (ret); } \
582 : } \
583 : while (false)
584 :
585 : #define ACL_REQUIRES_NOT_NULL_RET_INPUT_REPORT_WITH_FUNC_DESC(val, ret, funcDesc) \
586 : do { \
587 : if (UNLIKELY((val) == nullptr)) { \
588 : ACL_LOG_ERROR("[Check][%s]param must not be null.", #val); \
589 : acl::AclErrorLogManager::ReportInputError( \
590 : acl::NULL_POINTER_FUNC_MSG, {"func", "param"}, {(funcDesc), #val}); \
591 : return (ret); \
592 : } \
593 : } while (false)
594 :
595 : #define ACL_CHECK_MALLOC_RESULT_REPORT_RET(val, size, allocInterface, ret) \
596 : do { \
597 : if ((val) == nullptr) { \
598 : const std::string sizeVal = std::to_string(size); \
599 : ACL_LOG_ERROR("[Check][Malloc]Allocate memory for [%s] failed, bufferSize=%zu.", #val, size); \
600 : acl::AclErrorLogManager::ReportInputError(acl::ALLOC_MEMORY_FAILED_MSG, \
601 : std::vector<const char *>({"buf_size", "alloc_interface"}), \
602 : std::vector<const char *>({sizeVal.c_str(), allocInterface})); \
603 : return ret; } \
604 : } \
605 : while (false)
606 :
607 : #define ACL_REQUIRES_NOT_NULL_RET_VOID(val) \
608 : do { \
609 : if (UNLIKELY((val) == nullptr)) { \
610 : ACL_LOG_ERROR("[Check][%s]param must not be null.", #val); \
611 : return; } \
612 : } \
613 : while (false)
614 :
615 : #define ACL_CHECK_RANGE_INT(val, min, max) \
616 : do { \
617 : if (((val) < (min)) || ((val) > (max))) { \
618 : ACL_LOG_ERROR("[Check][%s]param:[%d] must be in range of [%d] and [%d]", \
619 : #val, (static_cast<int32_t>(val)), (static_cast<int32_t>(min)), (static_cast<int32_t>(max))); \
620 : return ACL_ERROR_INVALID_PARAM; } \
621 : } \
622 : while (false)
623 :
624 : #define ACL_CHECK_LESS_UINT(val, max) \
625 : do { \
626 : if ((val) > (max)) { \
627 : ACL_LOG_ERROR("[Check][%s]param:[%u] must be in range of [0] and [%u]", \
628 : #val, (static_cast<uint32_t>(val)), (static_cast<uint32_t>(max))); \
629 : return ACL_ERROR_INVALID_PARAM; } \
630 : } \
631 : while (false)
632 :
633 : #define ACL_CHECK_RANGE_FLOAT(val, min, max) \
634 : do { \
635 : if (((val) < (min)) || ((val) > (max))) { \
636 : ACL_LOG_ERROR("[Check][%s]param:[%.2f] must be in range of [%.2f] and [%.2f]", \
637 : #val, (static_cast<float64_t>(val)), (static_cast<float64_t>(min)), (static_cast<float64_t>(max))); \
638 : return ACL_ERROR_INVALID_PARAM; } \
639 : } \
640 : while (false)
641 :
642 : #define ACL_CHECK_MALLOC_RESULT(val) \
643 : do { \
644 : if ((val) == nullptr) { \
645 : ACL_LOG_INNER_ERROR("[Check][Malloc]Allocate memory for [%s] failed.", #val); \
646 : return ACL_ERROR_BAD_ALLOC; } \
647 : } \
648 : while (false)
649 :
650 : #define ACL_REQUIRES_NON_NEGATIVE(val) \
651 : do { \
652 : if ((val) < 0) { \
653 : ACL_LOG_ERROR("[Check][%s]param must be non-negative.", #val); \
654 : return ACL_ERROR_INVALID_PARAM; } \
655 : } \
656 : while (false)
657 :
658 : #define ACL_REQUIRES_POSITIVE_REPORT(val) \
659 : do { \
660 : if ((val) <= 0) { \
661 : ACL_LOG_ERROR("[Check][%s]param must be positive.", #val); \
662 : const std::string valStr = std::to_string(val); \
663 : std::string funcName = acl::AclErrorLogManager::GetFuncNameWithoutImplSuffix(__func__); \
664 : acl::AclErrorLogManager::ReportInputError(acl::INVALID_VALUE_MSG, \
665 : std::vector<const char *>({"func", "value", "param", "expect"}), \
666 : std::vector<const char *>({funcName.c_str(), valStr.c_str(), #val, "must be greater than zero"})); \
667 : return ACL_ERROR_INVALID_PARAM; } \
668 : } \
669 : while (false)
670 :
671 : #define ACL_REQUIRES_POSITIVE_REPORT_WITH_FUNC_DESC(val, funcDesc) \
672 : do { \
673 : if ((val) <= 0) { \
674 : ACL_LOG_ERROR("[Check][%s]param must be positive.", #val); \
675 : const std::string valStr = std::to_string(val); \
676 : acl::AclErrorLogManager::ReportInputError( \
677 : acl::INVALID_VALUE_MSG, std::vector<const char*>({"func", "value", "param", "expect"}), \
678 : std::vector<const char*>({(funcDesc), valStr.c_str(), #val, "must be greater than zero"})); \
679 : return ACL_ERROR_INVALID_PARAM; \
680 : } \
681 : } while (false)
682 :
683 : #define ACL_REQUIRES_POSITIVE(val) \
684 : do { \
685 : if ((val) <= 0) { \
686 : ACL_LOG_ERROR("[Check][%s]param must be positive.", #val); \
687 : return ACL_ERROR_INVALID_PARAM; } \
688 : } \
689 : while (false)
690 :
691 : #define ACL_REQUIRES_POSITIVE_RET_NULL(val) \
692 : do { \
693 : if ((val) <= 0) { \
694 : ACL_LOG_ERROR("[Check][%s]param must be positive.", #val); \
695 : return nullptr; } \
696 : } \
697 : while (false)
698 :
699 : #define ACL_DELETE(memory) \
700 : do { \
701 : delete (memory); \
702 : (memory) = nullptr; \
703 : } \
704 : while (false)
705 :
706 : #define ACL_DELETE_ARRAY(memory) \
707 : do { \
708 : if ((memory) != nullptr) { \
709 : delete[] static_cast<char_t *>(memory); \
710 : (memory) = nullptr; \
711 : } \
712 : } \
713 : while (false)
714 :
715 : #define ACL_CHECK_WITH_MESSAGE_AND_RETURN(exp, ret, ...) \
716 : do { \
717 : if (!(exp)) { \
718 : ACL_LOG_ERROR(__VA_ARGS__); \
719 : return (ret); \
720 : } \
721 : } \
722 : while (false)
723 :
724 : #define ACL_CHECK_WITH_INNER_MESSAGE_AND_RETURN(exp, ret, ...) \
725 : do { \
726 : if (!(exp)) { \
727 : ACL_LOG_INNER_ERROR(__VA_ARGS__); \
728 : return (ret); \
729 : } \
730 : } \
731 : while (false)
732 :
733 : #define ACL_DELETE_AND_SET_NULL(var) \
734 : do { \
735 : if ((var) != nullptr) { \
736 : delete (var); \
737 : (var) = nullptr; \
738 : } \
739 : } \
740 : while (false)
741 :
742 : #define ACL_DELETE_ARRAY_AND_SET_NULL(var) \
743 : do { \
744 : if ((var) != nullptr) { \
745 : delete[] (var); \
746 : (var) = nullptr; \
747 : } \
748 : } \
749 : while (false)
750 :
751 : #define ACL_FREE(var) \
752 : do { \
753 : if ((var) != nullptr) { \
754 : free(var); \
755 : (var) = nullptr; \
756 : } \
757 : } \
758 : while (false)
759 :
760 : #define ACL_ALIGN_FREE(var) \
761 : do { \
762 : if ((var) != nullptr) { \
763 : mmAlignFree(var); \
764 : (var) = nullptr; \
765 : } \
766 : } \
767 : while (false)
768 :
769 : // If make_shared is abnormal, print the log and execute the statement
770 : #define ACL_MAKE_SHARED(expr0, expr1) \
771 : try { \
772 : (expr0); \
773 : } catch (const std::bad_alloc &) { \
774 : ACL_LOG_INNER_ERROR("[Make][Shared]Make shared failed"); \
775 : expr1; \
776 : }
777 :
778 : #define RETURN_NULL_WITH_ALIGN_FREE(newAddr, mallocAddr) \
779 : do { \
780 : if ((newAddr) == nullptr) { \
781 : ACL_LOG_INNER_ERROR("[Malloc][Mem]new memory failed"); \
782 : mmAlignFree(mallocAddr); \
783 : return nullptr; \
784 : } \
785 : } \
786 : while (false)
787 :
788 : #define ACL_CHECK_INT32_EQUAL(leftValue, rightValue) \
789 : do { \
790 : if ((leftValue) != (rightValue)) { \
791 : ACL_LOG_INFO("[%d] is not equal to [%d].", (leftValue), (rightValue)); \
792 : return false; \
793 : } \
794 : } \
795 : while (false)
796 :
797 : #define ACL_REQUIRES_EQ(x, y) \
798 : do { \
799 : const auto &xv = (x); \
800 : const auto &yv = (y); \
801 : if (xv != yv) { \
802 : std::stringstream ss; \
803 : ss << "Assert (" << #x << " == " << #y << ") failed, expect " << yv << " actual " << xv; \
804 : ACL_LOG_INNER_ERROR("%s", ss.str().c_str()); \
805 : return ACL_ERROR_INVALID_PARAM; \
806 : } \
807 : } while (false)
808 :
809 : #define ACL_REQUIRES_LE(x, y) \
810 : do { \
811 : const auto &xv = (x); \
812 : const auto &yv = (y); \
813 : if (!(xv <= yv)) { \
814 : std::stringstream ss; \
815 : ss << "Assert (" << #x << " <= " << #y << ") failed, left value is " << xv << " right value is " << yv; \
816 : ACL_LOG_INNER_ERROR("%s", ss.str().c_str()); \
817 : return ACL_ERROR_INVALID_PARAM; \
818 : } \
819 : } while (false)
820 :
821 : #define ACL_CHECK_UINT32_GREATER(leftValue, rightValue) \
822 : do { \
823 : if ((leftValue) < (rightValue)) { \
824 : ACL_LOG_ERROR("left value [%u] can not be smaller than right value [%u].", \
825 : static_cast<uint32_t>(leftValue), static_cast<uint32_t>(rightValue)); \
826 : return ACL_ERROR_INVALID_PARAM; \
827 : } \
828 : } \
829 : while (false)
830 :
831 : #define ACL_CHECK_FILE_OPEN_FAILED(condition, path, reasonPrefix, ret) \
832 : do { \
833 : if (!(condition)) { \
834 : const std::string errReason = strerror(errno); \
835 : std::string errMsg = acl::AclErrorLogManager::FormatStr("%s, %s", (reasonPrefix), errReason.c_str()); \
836 : ACL_LOG_ERROR("[Check]Open file [%s] failed, reason is [%s].", path, errReason.c_str()); \
837 : acl::AclErrorLogManager::ReportInputError(acl::INVALID_FILE_MSG, \
838 : std::vector<const char *>({"path", "reason"}), \
839 : std::vector<const char *>({path, errMsg.c_str()})); \
840 : return ret; \
841 : } \
842 : } while (false)
843 :
844 : #endif // ACL_COMMON_LOG_H_
|