1 /** 2 * Copyright 2021-2023 Huawei Technologies Co., Ltd 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 #ifndef AICPU_OPS_AICPU_COMMON_KERNEL_LOG_H_ 17 #define AICPU_OPS_AICPU_COMMON_KERNEL_LOG_H_ 18 #include "inc/cust_cpu_utils.h" 19 20 namespace aicpu { 21 #define CUST_AICPU_LOGD(ctx, fmt, ...) \ 22 CustCpuKernelUtils::CustLogDebug(ctx, "[%s:%d][%s:%d]:" fmt, __FILE__, __LINE__, __FUNCTION__, __LINE__, \ 23 ##__VA_ARGS__) 24 #define CUST_AICPU_LOGI(ctx, fmt, ...) \ 25 CustCpuKernelUtils::CustLogInfo(ctx, "[%s:%d][%s:%d]:" fmt, __FILE__, __LINE__, __FUNCTION__, __LINE__, ##__VA_ARGS__) 26 #define CUST_AICPU_LOGW(ctx, fmt, ...) \ 27 CustCpuKernelUtils::CustLogWarning(ctx, "[%s:%d][%s:%d]:" fmt, __FILE__, __LINE__, __FUNCTION__, __LINE__, \ 28 ##__VA_ARGS__) 29 #define CUST_AICPU_LOGE(ctx, fmt, ...) \ 30 CustCpuKernelUtils::CustLogError(ctx, "[%s:%d][%s:%d]:" fmt, __FILE__, __LINE__, __FUNCTION__, __LINE__, \ 31 ##__VA_ARGS__) 32 33 #define CUST_KERNEL_LOG_WARN(ctx, fmt, ...) CUST_KERNEL_LOG_WARNING(ctx, fmt, ##__VA_ARGS__) 34 35 #define CUST_KERNEL_CHECK_NULLPTR_VOID(ctx, value, logText...) \ 36 if (value == nullptr) { \ 37 CUST_AICPU_LOGE(ctx, logText); \ 38 return; \ 39 } 40 41 #define CUST_KERNEL_CHECK_FALSE(ctx, condition, errorCode, logText...) \ 42 if (!(condition)) { \ 43 CUST_AICPU_LOGE(ctx, logText); \ 44 return errorCode; \ 45 } 46 47 #define CUST_KERNEL_CHECK_NULLPTR(ctx, value, errorCode, logText...) \ 48 if (value == nullptr) { \ 49 CUST_AICPU_LOGE(ctx, logText); \ 50 return errorCode; \ 51 } 52 53 #define CUST_KERNEL_CHECK_ASSIGN_64S_MULTI(ctx, A, B, result, errorCode) \ 54 do { \ 55 if ((A) != 0 && (B) != 0 && ((INT64_MAX) / (A)) <= (B)) { \ 56 CUST_AICPU_LOGE(ctx, "Integer reversed multiA: %llu * multiB: %llu", (A), (B)); \ 57 return errorCode; \ 58 } \ 59 (result) = ((A) * (B)); \ 60 } while (0) 61 62 #define CUST_KERNEL_CHECK_FALSE_VOID(ctx, condition, logText...) \ 63 if (!(condition)) { \ 64 CUST_AICPU_LOGE(ctx, logText); \ 65 return; \ 66 } 67 68 #define CUST_KERNEL_HANDLE_ERROR(ctx, expression, logText...) \ 69 ; \ 70 do { \ 71 uint32_t ret = expression; \ 72 if (ret != static_cast<uint32_t>(KERNEL_STATUS_OK)) { \ 73 CUST_AICPU_LOGE(ctx, logText); \ 74 return ret; \ 75 } \ 76 } while (0) 77 78 #define RETURN_IF_FAILURE(expr) \ 79 do { \ 80 auto ret = (expr); \ 81 if (ret != KERNEL_STATUS_OK) { \ 82 return ret; \ 83 } \ 84 } while (0) 85 /* 86 #include <unistd.h> 87 #include <sys/syscall.h> 88 #include <iostream> 89 #include <utility> 90 #include "common/kernel_errcode.h" 91 #include "toolchain/slog.h" 92 93 inline int64_t GetTid(void) { 94 thread_local static const int64_t tid = syscall(__NR_gettid); 95 return tid; 96 } 97 98 extern "C" { 99 __attribute__((visibility("default"))) void DlogInner(int moduleId, int level, const char *fmt, ...); 100 } 101 102 namespace aicpu { 103 #define AICPU_MODULE_NAME static_cast<int32_t>(AICPU) 104 #define KERNEL_MODULE "AICPU" 105 106 #define AICPU_LOG(level, fmt, ...) \ 107 do { \ 108 if (CheckLogLevel(AICPU, level) == 1) { \ 109 auto log_func = DlogRecord == nullptr ? DlogInner : DlogRecord; \ 110 log_func(AICPU, level, "[%s:%d][%s][tid:%lu]:" fmt, __FILE__, __LINE__, __func__, GetTid(), ##__VA_ARGS__); \ 111 } \ 112 } while (0) 113 114 #define AICPU_LOGD(fmt, ...) AICPU_LOG(DLOG_DEBUG, fmt, ##__VA_ARGS__); 115 #define AICPU_LOGI(fmt, ...) AICPU_LOG(DLOG_INFO, fmt, ##__VA_ARGS__); 116 #define AICPU_LOGW(fmt, ...) AICPU_LOG(DLOG_WARN, fmt, ##__VA_ARGS__); 117 #define AICPU_LOGE(fmt, ...) AICPU_LOG(DLOG_ERROR, fmt, ##__VA_ARGS__); 118 #define AICPU_EVENT(fmt, ...) AICPU_LOG(DLOG_EVENT, fmt, ##__VA_ARGS__); 119 120 #define AICPU_CHK_STATUS_RET(expr...) \ 121 do { \ 122 const uint32_t status = (expr); \ 123 if (status != kAicpuKernelStateSucess) { \ 124 return status; \ 125 } \ 126 } while (0); 127 128 #define AICPU_CHECK_NULLPTR_VOID(value, logText...) \ 129 if (value == nullptr) { \ 130 AICPU_LOGE(logText); \ 131 return; \ 132 } 133 134 #define AICPU_CHECK_FALSE(condition, errorCode, logText...) \ 135 if (!(condition)) { \ 136 AICPU_LOGE(logText); \ 137 return errorCode; \ 138 } 139 140 #define AICPU_CHECK_NULLPTR(value, errorCode, logText...) \ 141 if (value == nullptr) { \ 142 AICPU_LOGE(logText); \ 143 return errorCode; \ 144 } 145 146 #define KERNEL_LOG_DEBUG(fmt, ...) AICPU_LOGD(fmt, ##__VA_ARGS__) 147 #define KERNEL_LOG_INFO(fmt, ...) AICPU_LOGI(fmt, ##__VA_ARGS__) 148 #define KERNEL_LOG_WARN(fmt, ...) AICPU_LOGW(fmt, ##__VA_ARGS__) 149 #define KERNEL_LOG_ERROR(fmt, ...) AICPU_LOGE(fmt, ##__VA_ARGS__) 150 #define KERNEL_LOG_EVENT(fmt, ...) AICPU_EVENT(fmt, ##__VA_ARGS__) 151 152 #define KERNEL_CHECK_NULLPTR_VOID(value, logText...) \ 153 if (value == nullptr) { \ 154 AICPU_LOGE(logText); \ 155 return; \ 156 } 157 158 #define KERNEL_CHECK_FALSE(condition, errorCode, logText...) \ 159 if (!(condition)) { \ 160 AICPU_LOGE(logText); \ 161 return errorCode; \ 162 } 163 164 #define KERNEL_CHECK_NULLPTR(value, errorCode, logText...) \ 165 if (value == nullptr) { \ 166 AICPU_LOGE(logText); \ 167 return errorCode; \ 168 } 169 170 #define KERNEL_CHECK_ASSIGN_64S_MULTI(A, B, result, errorCode) \ 171 do { \ 172 if ((A) != 0 && (B) != 0 && ((INT64_MAX) / (A)) <= (B)) { \ 173 AICPU_LOGE("Integer reversed multiA: %llu * multiB: %llu", (A), (B)); \ 174 return errorCode; \ 175 } \ 176 (result) = ((A) * (B)); \ 177 } while (0) 178 179 #define KERNEL_CHECK_FALSE_VOID(condition, logText...) \ 180 if (!(condition)) { \ 181 AICPU_LOGE(logText); \ 182 return; \ 183 } 184 185 #define KERNEL_HANDLE_ERROR(expression, logText...) \ 186 ; \ 187 do { \ 188 uint32_t ret = expression; \ 189 if (ret != static_cast<uint32_t>(KERNEL_STATUS_OK)) { \ 190 AICPU_LOGE(logText); \ 191 return ret; \ 192 } \ 193 } while (0) 194 195 #define KERNEL_CHECK_FALSE_EXEC(condition, execExpr...) \ 196 if (!(condition)) { \ 197 execExpr; \ 198 } 199 200 #define RETURN_IF_FAILURE(expr) \ 201 do { \ 202 auto ret = (expr); \ 203 if (ret != KERNEL_STATUS_OK) { \ 204 return ret; \ 205 } \ 206 } while (0) 207 */ 208 } // namespace aicpu 209 #endif // AICPU_OPS_AICPU_COMMON_KERNEL_LOG_H_ 210