• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
19 #include <unistd.h>
20 #include <sys/syscall.h>
21 #include <iostream>
22 #include <utility>
23 #include "common/kernel_errcode.h"
24 #include "toolchain/slog.h"
25 
GetTid(void)26 inline int64_t GetTid(void) {
27   thread_local static const int64_t tid = syscall(__NR_gettid);
28   return tid;
29 }
30 
31 namespace aicpu {
32 #define AICPU_MODULE_NAME static_cast<int32_t>(AICPU)
33 #define KERNEL_MODULE "AICPU"
34 
35 #define AICPU_LOGD(fmt, ...)                                                                                  \
36   dlog_debug(AICPU_MODULE_NAME, "[%s][%s:%d][tid:%lu]:" fmt, KERNEL_MODULE, __FUNCTION__, __LINE__, GetTid(), \
37              ##__VA_ARGS__);
38 #define AICPU_LOGI(fmt, ...)                                                                                 \
39   dlog_info(AICPU_MODULE_NAME, "[%s][%s:%d][tid:%lu]:" fmt, KERNEL_MODULE, __FUNCTION__, __LINE__, GetTid(), \
40             ##__VA_ARGS__);
41 #define AICPU_LOGW(fmt, ...)                                                                                 \
42   dlog_warn(AICPU_MODULE_NAME, "[%s][%s:%d][tid:%lu]:" fmt, KERNEL_MODULE, __FUNCTION__, __LINE__, GetTid(), \
43             ##__VA_ARGS__);
44 #define AICPU_LOGE(fmt, ...)                                                                                  \
45   dlog_error(AICPU_MODULE_NAME, "[%s][%s:%d][tid:%lu]:" fmt, KERNEL_MODULE, __FUNCTION__, __LINE__, GetTid(), \
46              ##__VA_ARGS__);
47 #define AICPU_LOGEVENT(fmt, ...)                                                                              \
48   dlog_event(AICPU_MODULE_NAME, "[%s][%s:%d][tid:%lu]:" fmt, KERNEL_MODULE, __FUNCTION__, __LINE__, GetTid(), \
49              ##__VA_ARGS__);
50 
51 #define AICPU_CHK_STATUS_RET(expr...)        \
52   do {                                       \
53     const uint32_t status = (expr);          \
54     if (status != kAicpuKernelStateSucess) { \
55       return status;                         \
56     }                                        \
57   } while (0);
58 
59 #define AICPU_CHECK_NULLPTR_VOID(value, logText...) \
60   if (value == nullptr) {                           \
61     AICPU_LOGE(logText);                            \
62     return;                                         \
63   }
64 
65 #define AICPU_CHECK_FALSE(condition, errorCode, logText...) \
66   if (!(condition)) {                                       \
67     AICPU_LOGE(logText);                                    \
68     return errorCode;                                       \
69   }
70 
71 #define AICPU_CHECK_NULLPTR(value, errorCode, logText...) \
72   if (value == nullptr) {                                 \
73     AICPU_LOGE(logText);                                  \
74     return errorCode;                                     \
75   }
76 
77 #define KERNEL_LOG_DEBUG(fmt, ...)                                                                            \
78   dlog_debug(AICPU_MODULE_NAME, "[%s][%s:%d][tid:%lu]:" fmt, KERNEL_MODULE, __FUNCTION__, __LINE__, GetTid(), \
79              ##__VA_ARGS__);
80 #define KERNEL_LOG_INFO(fmt, ...)                                                                            \
81   dlog_info(AICPU_MODULE_NAME, "[%s][%s:%d][tid:%lu]:" fmt, KERNEL_MODULE, __FUNCTION__, __LINE__, GetTid(), \
82             ##__VA_ARGS__);
83 #define KERNEL_LOG_WARN(fmt, ...)                                                                            \
84   dlog_warn(AICPU_MODULE_NAME, "[%s][%s:%d][tid:%lu]:" fmt, KERNEL_MODULE, __FUNCTION__, __LINE__, GetTid(), \
85             ##__VA_ARGS__);
86 #define KERNEL_LOG_ERROR(fmt, ...)                                                                            \
87   dlog_error(AICPU_MODULE_NAME, "[%s][%s:%d][tid:%lu]:" fmt, KERNEL_MODULE, __FUNCTION__, __LINE__, GetTid(), \
88              ##__VA_ARGS__);
89 #define KERNEL_LOG_EVENT(fmt, ...)                                                                            \
90   dlog_event(AICPU_MODULE_NAME, "[%s][%s:%d][tid:%lu]:" fmt, KERNEL_MODULE, __FUNCTION__, __LINE__, GetTid(), \
91              ##__VA_ARGS__);
92 
93 #define KERNEL_CHECK_NULLPTR_VOID(value, logText...) \
94   if (value == nullptr) {                            \
95     AICPU_LOGE(logText);                             \
96     return;                                          \
97   }
98 
99 #define KERNEL_CHECK_FALSE(condition, errorCode, logText...) \
100   if (!(condition)) {                                        \
101     AICPU_LOGE(logText);                                     \
102     return errorCode;                                        \
103   }
104 
105 #define KERNEL_CHECK_NULLPTR(value, errorCode, logText...) \
106   if (value == nullptr) {                                  \
107     AICPU_LOGE(logText);                                   \
108     return errorCode;                                      \
109   }
110 
111 #define KERNEL_CHECK_ASSIGN_64S_MULTI(A, B, result, errorCode)              \
112   do {                                                                      \
113     if ((A) != 0 && (B) != 0 && ((INT64_MAX) / (A)) <= (B)) {               \
114       AICPU_LOGE("Integer reversed multiA: %llu * multiB: %llu", (A), (B)); \
115       return errorCode;                                                     \
116     }                                                                       \
117     (result) = ((A) * (B));                                                 \
118   } while (0)
119 
120 #define KERNEL_CHECK_FALSE_VOID(condition, logText...) \
121   if (!(condition)) {                                  \
122     AICPU_LOGE(logText);                               \
123     return;                                            \
124   }
125 
126 #define KERNEL_HANDLE_ERROR(expression, logText...)       \
127   ;                                                       \
128   do {                                                    \
129     uint32_t ret = expression;                            \
130     if (ret != static_cast<uint32_t>(KERNEL_STATUS_OK)) { \
131       AICPU_LOGE(logText);                                \
132       return ret;                                         \
133     }                                                     \
134   } while (0)
135 
136 #define KERNEL_CHECK_FALSE_EXEC(condition, execExpr...) \
137   if (!(condition)) {                                   \
138     execExpr;                                           \
139   }
140 }  // namespace aicpu
141 #endif  // AICPU_OPS_AICPU_COMMON_KERNEL_LOG_H_
142