LCOV - code coverage report
Current view: top level - acl/common - log_inner.h (source / functions) Hit Total Coverage
Test: coverage.info Lines: 2 2 100.0 %
Date: 2026-08-27 13:24:42 Functions: 1 1 100.0 %

          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             : 
      20             : #ifndef char_t
      21             : using char_t = char;
      22             : #endif
      23             : 
      24             : #ifndef float32_t
      25             : using float32_t = float;
      26             : #endif
      27             : 
      28             : namespace acl {
      29             : constexpr int32_t ACL_MODE_ID = static_cast<int32_t>(ASCENDCL);
      30             : constexpr int32_t APP_MODE_ID = static_cast<int32_t>(APP);
      31             : constexpr uint16_t ACL_MODE_ID_U16 = static_cast<uint16_t>(ASCENDCL);
      32             : constexpr uint16_t APP_MODE_ID_U16 = static_cast<uint16_t>(APP);
      33             : constexpr const char_t *const ACL_MODULE_NAME = "ASCENDCL";
      34             : constexpr const char_t *const INVALID_PARAM_MSG = "EH0001";
      35             : constexpr const char_t *const INVALID_NULL_POINTER_MSG = "EH0002";
      36             : constexpr const char_t *const INVALID_PATH_MSG = "EH0003";
      37             : constexpr const char_t *const INVALID_FILE_MSG = "EH0004";
      38             : constexpr const char_t *const INVALID_AIPP_MSG = "EH0005";
      39             : constexpr const char_t *const UNSUPPORTED_FEATURE_MSG = "EH0006";
      40             : 
      41             : // first stage
      42             : constexpr const char_t *const ACL_STAGE_SET = "SET";
      43             : constexpr const char_t *const ACL_STAGE_GET = "GET";
      44             : constexpr const char_t *const ACL_STAGE_CREATE = "CREATE";
      45             : constexpr const char_t *const ACL_STAGE_DESTROY = "DESTROY";
      46             : constexpr const char_t *const ACL_STAGE_BLAS = "BLAS";
      47             : constexpr const char_t *const ACL_STAGE_INFER = "INFER";
      48             : constexpr const char_t *const ACL_STAGE_COMP = "COMP";
      49             : constexpr const char_t *const ACL_STAGE_LOAD = "LOAD";
      50             : constexpr const char_t *const ACL_STAGE_UNLOAD = "UNLOAD";
      51             : constexpr const char_t *const ACL_STAGE_EXEC = "EXEC";
      52             : constexpr const char_t *const ACL_STAGE_COMP_AND_EXEC = "COMP_AND_EXEC";
      53             : constexpr const char_t *const ACL_STAGE_DUMP = "DUMP";
      54             : constexpr const char_t *const ACL_STAGE_DVPP = "DVPP";
      55             : constexpr const char_t *const ACL_STAGE_TDT = "TDT";
      56             : constexpr const char_t *const ACL_STAGE_INIT = "INIT";
      57             : constexpr const char_t *const ACL_STAGE_FINAL = "FINAL";
      58             : constexpr const char_t *const ACL_STAGE_QUEUE = "QUEUE";
      59             : constexpr const char_t *const ACL_STAGE_MBUF = "MBUF";
      60             : // second stage
      61             : constexpr const char_t *const ACL_STAGE_DEFAULT = "DEFAULT";
      62             : 
      63             : constexpr size_t MAX_LOG_STRING = 1024U;
      64             : 
      65             : constexpr size_t MAX_ERROR_STRING = 128U;
      66             : 
      67        6196 : inline const char_t* format_cast(const char_t *const src)
      68             : {
      69        6196 :     return static_cast<const char_t *>(src);
      70             : }
      71             : 
      72             : ACL_FUNC_VISIBILITY std::string AclGetErrorFormatMessage(const mmErrorMsg errnum);
      73             : 
      74             : class ACL_FUNC_VISIBILITY AclLog {
      75             : public:
      76             :     static bool IsLogOutputEnable(const aclLogLevel logLevel);
      77             :     static mmPid_t GetTid();
      78             :     static void ACLSaveLog(const aclLogLevel logLevel, const char_t *const strLog);
      79             :     static bool IsEventLogOutputEnable();
      80             : private:
      81             :     static aclLogLevel GetCurLogLevel();
      82             :     static bool isEnableEvent_;
      83             : };
      84             : 
      85             : class ACL_FUNC_VISIBILITY AclErrorLogManager {
      86             : public:
      87             :     static std::string FormatStr(const char_t *const fmt, ...);
      88             : #if !defined(ENABLE_DVPP_INTERFACE) || defined(RUN_TEST)
      89             :     static void ReportInputError(const char *errorCode, const std::vector<const char *> &key = {},
      90             :         const std::vector<const char *> &val = {});
      91             : #else
      92             : #endif
      93             :     static void ReportInputErrorWithChar(const char_t *const errorCode, const char_t *const argNames[],
      94             :         const char_t *const argVals[], const size_t size);
      95             :     static void ReportInnerError(const char_t *const fmt, ...);
      96             :     static void ReportCallError(const char_t *const fmt, ...);
      97             : };
      98             : } // namespace acl
      99             : 
     100             : #if defined(__GNUC__) || defined(__clang__)
     101             : #define LIKELY(expr) (static_cast<bool>(__builtin_expect(static_cast<bool>(expr), 1)))
     102             : #define UNLIKELY(expr) (static_cast<bool>(__builtin_expect(static_cast<bool>(expr), 0)))
     103             : #else
     104             : #define LIKELY(expr) (expr)
     105             : #define UNLIKELY(expr) (expr)
     106             : #endif
     107             : 
     108             : #ifdef RUN_TEST
     109             : #define ACL_LOG_INFO(fmt, ...)                                                                      \
     110             :     do {                                                                                            \
     111             :             if (acl::AclLog::IsLogOutputEnable(ACL_INFO)) {                                         \
     112             :                 constexpr const char_t *const funcName = __FUNCTION__;                                        \
     113             :                 printf("INFO %d %s:%s:%d: "#fmt "\n", acl::AclLog::GetTid(), acl::format_cast(funcName), \
     114             :                     __FILE__, __LINE__, ##__VA_ARGS__);                                             \
     115             :             }                                                                                       \
     116             :     } while (false)
     117             : #define ACL_LOG_DEBUG(fmt, ...)                                                                     \
     118             :     do {                                                                                            \
     119             :             if (acl::AclLog::IsLogOutputEnable(ACL_DEBUG)) {                                        \
     120             :                 constexpr const char_t *const funcName = __FUNCTION__;                                        \
     121             :                 printf("DEBUG %d %s:%s:%d: "#fmt "\n", acl::AclLog::GetTid(), acl::format_cast(funcName), \
     122             :                     __FILE__, __LINE__, ##__VA_ARGS__);                                             \
     123             :             }                                                                                       \
     124             :     } while (false)
     125             : #define ACL_LOG_WARN(fmt, ...)                                                                      \
     126             :     do {                                                                                            \
     127             :             if (acl::AclLog::IsLogOutputEnable(ACL_WARNING)) {                                      \
     128             :                 constexpr const char_t *const funcName = __FUNCTION__;                                        \
     129             :                 printf("WARN %d %s:%s:%d: "#fmt "\n", acl::AclLog::GetTid(), acl::format_cast(funcName), \
     130             :                     __FILE__, __LINE__, ##__VA_ARGS__);                                             \
     131             :             }                                                                                       \
     132             :     } while (false)
     133             : #define ACL_LOG_ERROR(fmt, ...)                                                                     \
     134             :     do {                                                                                            \
     135             :             constexpr const char_t *const funcName = __FUNCTION__;                                            \
     136             :             printf("ERROR %d %s:%s:%d:" fmt "\n", acl::AclLog::GetTid(), acl::format_cast(funcName), \
     137             :                 __FILE__, __LINE__, ##__VA_ARGS__);  \
     138             :     } while (false)
     139             : #define ACL_LOG_INNER_ERROR(fmt, ...)                                                               \
     140             :     do {                                                                                            \
     141             :             constexpr const char_t *const funcName = __FUNCTION__;                                            \
     142             :             printf("ERROR %d %s:%s:%d:" fmt "\n", acl::AclLog::GetTid(), acl::format_cast(funcName), \
     143             :                 __FILE__, __LINE__, ##__VA_ARGS__); \
     144             :     } while (false)
     145             : #define ACL_LOG_CALL_ERROR(fmt, ...)                                                                \
     146             :     do {                                                                                            \
     147             :             constexpr const char_t *const funcName = __FUNCTION__;                                            \
     148             :             printf("ERROR %d %s:%s:%d:" fmt "\n", acl::AclLog::GetTid(), acl::format_cast(funcName), \
     149             :                 __FILE__, __LINE__, ##__VA_ARGS__); \
     150             :     } while (false)
     151             : #define ACL_LOG_EVENT(fmt, ...)                                                                     \
     152             :     do {                                                                                            \
     153             :             if (acl::AclLog::IsEventLogOutputEnable()) {                                            \
     154             :                 constexpr const char_t *const funcName = __FUNCTION__;                                        \
     155             :                 printf("EVENT %d %s:%s:%d: "#fmt "\n", acl::AclLog::GetTid(), acl::format_cast(funcName), \
     156             :                     __FILE__, __LINE__, ##__VA_ARGS__);                                             \
     157             :             }                                                                                       \
     158             :     } while (false)
     159             : #else
     160             : #define ACL_LOG_INFO(fmt, ...)                                                                                        \
     161             :     do {                                                                                                              \
     162             :         constexpr const char_t *const funcName = __FUNCTION__;                                                        \
     163             :         dlog_info(acl::ACL_MODE_ID, "%d %s: " fmt, acl::AclLog::GetTid(), acl::format_cast(funcName), ##__VA_ARGS__); \
     164             :     } while (false)
     165             : #define ACL_LOG_DEBUG(fmt, ...)                                                                                        \
     166             :     do {                                                                                                               \
     167             :         constexpr const char_t *const funcName = __FUNCTION__;                                                         \
     168             :         dlog_debug(acl::ACL_MODE_ID, "%d %s: " fmt, acl::AclLog::GetTid(), acl::format_cast(funcName), ##__VA_ARGS__); \
     169             :     } while (false)
     170             : #define ACL_LOG_WARN(fmt, ...)                                                                                        \
     171             :     do {                                                                                                              \
     172             :         constexpr const char_t *const funcName = __FUNCTION__;                                                        \
     173             :         dlog_warn(acl::ACL_MODE_ID, "%d %s: " fmt, acl::AclLog::GetTid(), acl::format_cast(funcName), ##__VA_ARGS__); \
     174             :     } while (false)
     175             : #define ACL_LOG_ERROR(fmt, ...)                                                                          \
     176             :     do {                                                                                                 \
     177             :         constexpr const char_t *const funcName = __FUNCTION__;                                           \
     178             :         dlog_error(acl::ACL_MODE_ID, "%d %s:" fmt, acl::AclLog::GetTid(), acl::format_cast(funcName), \
     179             :             ##__VA_ARGS__);                          \
     180             :     } while (false)
     181             : #define ACL_LOG_INNER_ERROR(fmt, ...)                                                                    \
     182             :     do {                                                                                                 \
     183             :         constexpr const char_t *const funcName = __FUNCTION__;                                           \
     184             :         dlog_error(acl::ACL_MODE_ID, "%d %s:" fmt, acl::AclLog::GetTid(), acl::format_cast(funcName), \
     185             :             ##__VA_ARGS__);                          \
     186             :         acl::AclErrorLogManager::ReportInnerError((fmt), ##__VA_ARGS__);                                 \
     187             :     } while (false)
     188             : #define ACL_LOG_CALL_ERROR(fmt, ...)                                                                     \
     189             :     do {                                                                                                 \
     190             :         constexpr const char_t *const funcName = __FUNCTION__;                                           \
     191             :         dlog_error(acl::ACL_MODE_ID, "%d %s:" fmt, acl::AclLog::GetTid(), acl::format_cast(funcName), \
     192             :             ##__VA_ARGS__);                          \
     193             :         acl::AclErrorLogManager::ReportCallError((fmt), ##__VA_ARGS__);                                  \
     194             :     } while (false)
     195             : #define ACL_LOG_EVENT(fmt, ...)                                                                  \
     196             :     do {                                                                                         \
     197             :         if (acl::AclLog::IsEventLogOutputEnable()) {                                             \
     198             :             constexpr const char_t *const funcName = __FUNCTION__;                               \
     199             :             dlog_info((acl::ACL_MODE_ID | (RUN_LOG_MASK)), "%d %s: " fmt, acl::AclLog::GetTid(), \
     200             :                 acl::format_cast(funcName), ##__VA_ARGS__);                                      \
     201             :         }                                                                                        \
     202             :     } while (false)
     203             : #endif
     204             : 
     205             : #define OFFSET_OF_MEMBER(type, member) reinterpret_cast<size_t>(&((reinterpret_cast<type *>(0))->member))
     206             : 
     207             : #define ACL_REQUIRES_OK(expr) \
     208             :     do { \
     209             :         const aclError __ret = (expr); \
     210             :         if (__ret != ACL_SUCCESS) { \
     211             :             return __ret; \
     212             :         } \
     213             :     } \
     214             :     while (false)
     215             : 
     216             : #define ACL_REQUIRES_OK_RETURN_NULL(expr) \
     217             :     do { \
     218             :         const aclError __ret = (expr); \
     219             :         if (__ret != ACL_SUCCESS) { \
     220             :             return nullptr; \
     221             :         } \
     222             :     } \
     223             :     while (false)
     224             : 
     225             : #define ACL_REQUIRES_EOK(expr, interface) \
     226             :     do { \
     227             :         const auto __ret = (expr); \
     228             :         if (__ret != EOK) { \
     229             :             ACL_LOG_INNER_ERROR("call api [%s] failed, retCode is %d", #interface, __ret); \
     230             :             return ACL_ERROR_FAILURE; \
     231             :         } \
     232             :     } \
     233             :     while (false)
     234             : 
     235             : #define ACL_REQUIRES_OK_WITH_INNER_MESSAGE(expr, ...) \
     236             :     do { \
     237             :         const aclError __ret = (expr); \
     238             :         if (__ret != ACL_SUCCESS) { \
     239             :             ACL_LOG_INNER_ERROR(__VA_ARGS__); \
     240             :             return __ret; \
     241             :         } \
     242             :     } \
     243             :     while (false)
     244             : 
     245             : #define ACL_REQUIRES_CALL_GE_OK(expr, ...) \
     246             :     do { \
     247             :         const auto __ret = (expr); \
     248             :         if (__ret != ge::SUCCESS) { \
     249             :             ACL_LOG_CALL_ERROR(__VA_ARGS__); \
     250             :             return __ret; \
     251             :         } \
     252             :     } \
     253             :     while (false)
     254             : 
     255             : #define ACL_REQUIRES_CALL_RTS_OK(expr, interface) \
     256             :     do { \
     257             :         const rtError_t __ret = (expr); \
     258             :         if (__ret != RT_ERROR_NONE) { \
     259             :             if (__ret == ACL_ERROR_RT_FEATURE_NOT_SUPPORT) { \
     260             :                 ACL_LOG_WARN("rts api [%s] is not supported currently,", #interface); \
     261             :                 return __ret; \
     262             :             } \
     263             :             ACL_LOG_CALL_ERROR("[Call][Rts]call rts api [%s] failed, retCode is %d", #interface, __ret); \
     264             :             return __ret; \
     265             :         } \
     266             :     } \
     267             :     while (false)
     268             : 
     269             : #define ACL_REQUIRES_CALL_HIMPI_OK(expr, interface) \
     270             :     do { \
     271             :         const int32_t __ret = (expr); \
     272             :         if (__ret != 0) { \
     273             :             ACL_LOG_CALL_ERROR("[Call][Himpi]call himpi api [%s] failed, retCode is %d", #interface, __ret); \
     274             :             return __ret; \
     275             :         } \
     276             :     } \
     277             :     while (false)
     278             : 
     279             : // Validate whether the expr value is true
     280             : #define ACL_REQUIRES_TRUE(expr, errCode, errDesc) \
     281             :     do { \
     282             :         const bool __ret = (expr); \
     283             :         if (!__ret) { \
     284             :             ACL_LOG_ERROR(errDesc); \
     285             :             return (errCode); \
     286             :         } \
     287             :     } \
     288             :     while (false)
     289             : 
     290             : #ifndef ENABLE_DVPP_INTERFACE
     291             : #define ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(val) \
     292             :     do { \
     293             :     if (UNLIKELY((val) == nullptr)) { \
     294             :         ACL_LOG_ERROR("[Check][%s]param must not be null.", #val); \
     295             :         acl::AclErrorLogManager::ReportInputError("EH0002", {"param"}, {#val}); \
     296             :         return ACL_ERROR_INVALID_PARAM; } \
     297             :     } \
     298             :     while (false)
     299             : #else
     300             : #define ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(val) \
     301             :     do { \
     302             :     if (UNLIKELY((val) == nullptr)) { \
     303             :         ACL_LOG_ERROR("[Check][%s]param must not be null.", #val); \
     304             :         const char_t *argList[] = {"param"}; \
     305             :         const char_t *argVal[] = {#val}; \
     306             :         acl::AclErrorLogManager::ReportInputErrorWithChar("EH0002", argList, argVal, 1U); \
     307             :         return ACL_ERROR_INVALID_PARAM; } \
     308             :     } \
     309             :     while (false)
     310             : #endif
     311             : 
     312             : #define ACL_REQUIRES_NOT_NULL(val) \
     313             :     do { \
     314             :         if (UNLIKELY((val) == nullptr)) { \
     315             :             ACL_LOG_ERROR("[Check][%s]param must not be null.", #val); \
     316             :             return ACL_ERROR_INVALID_PARAM; } \
     317             :         } \
     318             :     while (false)
     319             : 
     320             : #define ACL_REQUIRES_NOT_NULL_WITH_INNER_REPORT(val) \
     321             :     do { \
     322             :         if (UNLIKELY((val) == nullptr)) { \
     323             :             ACL_LOG_INNER_ERROR("[Check][%s]param must not be null.", #val); \
     324             :             return ACL_ERROR_INVALID_PARAM; } \
     325             :         } \
     326             :     while (false)
     327             : 
     328             : #define ACL_REQUIRES_NOT_NULL_RET_NULL(val) \
     329             :     do { \
     330             :         if (UNLIKELY((val) == nullptr)) { \
     331             :             ACL_LOG_ERROR("[Check][%s]param must not be null.", #val); \
     332             :             return nullptr; } \
     333             :         } \
     334             :     while (false)
     335             : 
     336             : #define ACL_REQUIRES_NOT_NULL_RET_NULL_INPUT_REPORT(val) \
     337             :     do { \
     338             :         if (UNLIKELY((val) == nullptr)) { \
     339             :             ACL_LOG_ERROR("[Check][%s]param must not be null.", #val); \
     340             :             acl::AclErrorLogManager::ReportInputError("EH0002", {"param"}, {#val}); \
     341             :             return nullptr; } \
     342             :         } \
     343             :     while (false)
     344             : 
     345             : #define ACL_REQUIRES_NOT_NULL_RET_VOID(val) \
     346             :     do { \
     347             :         if (UNLIKELY((val) == nullptr)) { \
     348             :             ACL_LOG_ERROR("[Check][%s]param must not be null.", #val); \
     349             :             return; } \
     350             :         } \
     351             :     while (false)
     352             : 
     353             : #define ACL_CHECK_RANGE_INT(val, min, max) \
     354             :     do { \
     355             :         if (((val) < (min)) || ((val) > (max))) { \
     356             :             ACL_LOG_ERROR("[Check][%s]param:[%d] must be in range of [%d] and [%d]", \
     357             :                 #val, (static_cast<int32_t>(val)), (static_cast<int32_t>(min)), (static_cast<int32_t>(max))); \
     358             :             return ACL_ERROR_INVALID_PARAM; } \
     359             :         } \
     360             :     while (false)
     361             : 
     362             : #define ACL_CHECK_LESS_UINT(val, max) \
     363             :     do { \
     364             :         if ((val) > (max)) { \
     365             :             ACL_LOG_ERROR("[Check][%s]param:[%u] must be in range of [0] and [%u]", \
     366             :                 #val, (static_cast<uint32_t>(val)), (static_cast<uint32_t>(max))); \
     367             :             return ACL_ERROR_INVALID_PARAM; } \
     368             :         } \
     369             :     while (false)
     370             : 
     371             : #define ACL_CHECK_RANGE_FLOAT(val, min, max) \
     372             :     do { \
     373             :         if (((val) < (min)) || ((val) > (max))) { \
     374             :             ACL_LOG_ERROR("[Check][%s]param:[%.2f] must be in range of [%.2f] and [%.2f]", \
     375             :                 #val, (static_cast<float64_t>(val)), (static_cast<float64_t>(min)), (static_cast<float64_t>(max))); \
     376             :             return ACL_ERROR_INVALID_PARAM; } \
     377             :         } \
     378             :     while (false)
     379             : 
     380             : #define ACL_CHECK_MALLOC_RESULT(val) \
     381             :     do { \
     382             :         if ((val) == nullptr) { \
     383             :             ACL_LOG_INNER_ERROR("[Check][Malloc]Allocate memory for [%s] failed.", #val); \
     384             :             return ACL_ERROR_BAD_ALLOC; } \
     385             :         } \
     386             :     while (false)
     387             : 
     388             : #define ACL_REQUIRES_NON_NEGATIVE(val) \
     389             :     do { \
     390             :         if ((val) < 0) { \
     391             :             ACL_LOG_ERROR("[Check][%s]param must be non-negative.", #val); \
     392             :             return ACL_ERROR_INVALID_PARAM; } \
     393             :         } \
     394             :     while (false)
     395             : 
     396             : #define ACL_REQUIRES_NON_NEGATIVE_WITH_INPUT_REPORT(val) \
     397             :     do { \
     398             :         if ((val) < 0) { \
     399             :             ACL_LOG_ERROR("[Check][%s]param must be non-negative.", #val); \
     400             :             acl::AclErrorLogManager::ReportInputError("EH0001", \
     401             :                 std::vector<const char *>({"param", "value", "reason"}), \
     402             :                 std::vector<const char *>({#val, std::to_string(val).c_str(), "must be non-negative"})); \
     403             :             return ACL_ERROR_INVALID_PARAM; } \
     404             :         } \
     405             :     while (false)
     406             : 
     407             : #define ACL_REQUIRES_POSITIVE(val) \
     408             :     do { \
     409             :         if ((val) <= 0) { \
     410             :             ACL_LOG_ERROR("[Check][%s]param must be positive.", #val); \
     411             :             return ACL_ERROR_INVALID_PARAM; } \
     412             :         } \
     413             :     while (false)
     414             : 
     415             : #define ACL_REQUIRES_POSITIVE_WITH_INPUT_REPORT(val) \
     416             :     do { \
     417             :         if ((val) <= 0) { \
     418             :             ACL_LOG_ERROR("[Check][%s]param must be positive.", #val); \
     419             :             acl::AclErrorLogManager::ReportInputError("EH0001", \
     420             :                 std::vector<const char *>({"param", "value", "reason"}), \
     421             :                 std::vector<const char *>({#val, std::to_string(val).c_str(), "must be positive"})); \
     422             :             return ACL_ERROR_INVALID_PARAM; } \
     423             :         } \
     424             :     while (false)
     425             : 
     426             : #define ACL_REQUIRES_POSITIVE_RET_NULL(val) \
     427             :     do { \
     428             :         if ((val) <= 0) { \
     429             :             ACL_LOG_ERROR("[Check][%s]param must be positive.", #val); \
     430             :             return nullptr; } \
     431             :         } \
     432             :     while (false)
     433             : 
     434             : #define ACL_DELETE(memory) \
     435             :     do { \
     436             :         delete (memory); \
     437             :         (memory) = nullptr; \
     438             :     } \
     439             :     while (false)
     440             : 
     441             : #define ACL_DELETE_ARRAY(memory) \
     442             :     do { \
     443             :         if ((memory) != nullptr) { \
     444             :             delete[] static_cast<char_t *>(memory); \
     445             :             (memory) = nullptr; \
     446             :         } \
     447             :     } \
     448             :     while (false)
     449             : 
     450             : #define ACL_CHECK_WITH_MESSAGE_AND_RETURN(exp, ret, ...) \
     451             :     do { \
     452             :         if (!(exp)) { \
     453             :             ACL_LOG_ERROR(__VA_ARGS__); \
     454             :             return (ret); \
     455             :         } \
     456             :     } \
     457             :     while (false)
     458             : 
     459             : #define ACL_CHECK_WITH_INNER_MESSAGE_AND_RETURN(exp, ret, ...) \
     460             :     do { \
     461             :         if (!(exp)) { \
     462             :             ACL_LOG_INNER_ERROR(__VA_ARGS__); \
     463             :             return (ret); \
     464             :         } \
     465             :     } \
     466             :     while (false)
     467             : 
     468             : #define ACL_DELETE_AND_SET_NULL(var) \
     469             :     do { \
     470             :         if ((var) != nullptr) { \
     471             :             delete (var); \
     472             :             (var) = nullptr; \
     473             :         } \
     474             :     } \
     475             :     while (false)
     476             : 
     477             : #define ACL_DELETE_ARRAY_AND_SET_NULL(var) \
     478             :     do { \
     479             :         if ((var) != nullptr) { \
     480             :             delete[] (var); \
     481             :             (var) = nullptr; \
     482             :         } \
     483             :     } \
     484             :     while (false)
     485             : 
     486             : #define ACL_FREE(var) \
     487             :     do { \
     488             :         if ((var) != nullptr) { \
     489             :             free(var); \
     490             :             (var) = nullptr; \
     491             :         } \
     492             :     } \
     493             :     while (false)
     494             : 
     495             : #define ACL_ALIGN_FREE(var) \
     496             :     do { \
     497             :         if ((var) != nullptr) { \
     498             :             mmAlignFree(var); \
     499             :             (var) = nullptr; \
     500             :         } \
     501             :     } \
     502             :     while (false)
     503             : 
     504             : // If make_shared is abnormal, print the log and execute the statement
     505             : #define ACL_MAKE_SHARED(expr0, expr1) \
     506             :     try { \
     507             :         (expr0); \
     508             :     } catch (const std::bad_alloc &) { \
     509             :         ACL_LOG_INNER_ERROR("[Make][Shared]Make shared failed"); \
     510             :         expr1; \
     511             :     }
     512             : 
     513             : #define RETURN_NULL_WITH_ALIGN_FREE(newAddr, mallocAddr) \
     514             :     do { \
     515             :         if ((newAddr) == nullptr) { \
     516             :             ACL_LOG_INNER_ERROR("[Malloc][Mem]new memory failed"); \
     517             :             mmAlignFree(mallocAddr); \
     518             :             return nullptr; \
     519             :         } \
     520             :     } \
     521             :     while (false)
     522             : 
     523             : #define ACL_CHECK_INT32_EQUAL(leftValue, rightValue) \
     524             :     do { \
     525             :         if ((leftValue) != (rightValue)) { \
     526             :             ACL_LOG_INFO("[%d] is not equal to [%d].", (leftValue), (rightValue)); \
     527             :             return false; \
     528             :         } \
     529             :     } \
     530             :     while (false)
     531             : 
     532             : #define ACL_REQUIRES_EQ(x, y)                                                                                          \
     533             :     do {                                                                                                               \
     534             :         const auto &xv = (x);                                                                                          \
     535             :         const auto &yv = (y);                                                                                          \
     536             :         if (xv != yv) {                                                                                                \
     537             :             std::stringstream ss;                                                                                      \
     538             :             ss << "Assert (" << #x << " == " << #y << ") failed, expect " << yv << " actual " << xv;                   \
     539             :             ACL_LOG_INNER_ERROR("%s", ss.str().c_str());                                                               \
     540             :             return ACL_ERROR_INVALID_PARAM;                                                                            \
     541             :         }                                                                                                              \
     542             :     } while (false)
     543             : 
     544             : #define ACL_REQUIRES_LE(x, y)                                                                                          \
     545             :     do {                                                                                                               \
     546             :         const auto &xv = (x);                                                                                          \
     547             :         const auto &yv = (y);                                                                                          \
     548             :         if (!(xv <= yv)) {                                                                                             \
     549             :             std::stringstream ss;                                                                                      \
     550             :             ss << "Assert (" << #x << " <= " << #y << ") failed, left value is " << xv << " right value is " << yv;    \
     551             :             ACL_LOG_INNER_ERROR("%s", ss.str().c_str());                                                               \
     552             :             return ACL_ERROR_INVALID_PARAM;                                                                            \
     553             :         }                                                                                                              \
     554             :     } while (false)
     555             : 
     556             : #define ACL_CHECK_UINT32_GREATER(leftValue, rightValue) \
     557             :     do { \
     558             :         if ((leftValue) < (rightValue)) { \
     559             :             ACL_LOG_ERROR("left value [%u] can not be smaller than right value [%u].", \
     560             :                 static_cast<uint32_t>(leftValue), static_cast<uint32_t>(rightValue)); \
     561             :             return ACL_ERROR_INVALID_PARAM; \
     562             :         } \
     563             :     } \
     564             :     while (false)
     565             : 
     566             : #endif // ACL_COMMON_LOG_H_

Generated by: LCOV version 1.14