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 AICPU_KERNEL_UTILS_AICPU_KERNEL_LOG_H
12 : #define AICPU_KERNEL_UTILS_AICPU_KERNEL_LOG_H
13 : #include <sys/syscall.h>
14 : #include <cstdint>
15 : #include <unistd.h>
16 :
17 : #include "log_weak.h"
18 :
19 : namespace aicpu {
20 9 : inline int64_t GetTid()
21 : {
22 9 : thread_local static int64_t tid = syscall(__NR_gettid);
23 9 : return tid;
24 : }
25 : }
26 :
27 : #ifdef RUN_TEST
28 : const char KERNEL_MODULE[] = "AICPU";
29 : #define KERNEL_LOG_DEBUG(fmt, ...) ((void)0)
30 : #define KERNEL_LOG_INFO(fmt, ...) ((void)0)
31 : #define KERNEL_LOG_WARN(fmt, ...) \
32 : printf("[WARN] [%s][%s][%s:%d][tid:%ld]:" fmt "\n", KERNEL_MODULE, __FILE__, \
33 : __FUNCTION__, __LINE__, aicpu::GetTid(), ##__VA_ARGS__)
34 : #define KERNEL_LOG_ERROR(fmt, ...) \
35 : printf("[ERROR] [%s][%s][%s:%d][tid:%ld]:" fmt "\n", KERNEL_MODULE, \
36 : __FILE__, __FUNCTION__, __LINE__, aicpu::GetTid(), ##__VA_ARGS__)
37 : #define KERNEL_LOG_EVENT(fmt, ...) \
38 : printf("[EVENT] [%s][%s][%s:%d][tid:%ld]:" fmt "\n", KERNEL_MODULE, \
39 : __FILE__, __FUNCTION__, __LINE__, aicpu::GetTid(), ##__VA_ARGS__)
40 : #else
41 : #define KERNEL_LOG_DEBUG(fmt, ...) \
42 : do { \
43 : if ((&CheckLogLevel != nullptr) && (&DlogRecord != nullptr)) { \
44 : dlog_debug(AICPU, "[%s:%d][%s][tid:%ld]" fmt, __FILE__, __LINE__, __func__, \
45 : aicpu::GetTid(), ##__VA_ARGS__); \
46 : } \
47 : } while (0)
48 :
49 : #define KERNEL_LOG_INFO(fmt, ...) \
50 : do { \
51 : if ((&CheckLogLevel != nullptr) && (&DlogRecord != nullptr)) { \
52 : dlog_info(AICPU, "[%s:%d][%s][tid:%ld]" fmt, __FILE__, __LINE__, __func__, \
53 : aicpu::GetTid(), ##__VA_ARGS__); \
54 : } \
55 : } while (0)
56 :
57 : #define KERNEL_LOG_WARN(fmt, ...) \
58 : do { \
59 : if ((&CheckLogLevel != nullptr) && (&DlogRecord != nullptr)) { \
60 : dlog_warn(AICPU, "[%s:%d][%s][tid:%ld]" fmt, __FILE__, __LINE__, __func__, \
61 : aicpu::GetTid(), ##__VA_ARGS__); \
62 : } \
63 : } while (0)
64 :
65 : #define KERNEL_LOG_ERROR(fmt, ...) \
66 : do { \
67 : if (&DlogRecord != nullptr) { \
68 : dlog_error(AICPU, "[%s:%d][%s][tid:%ld]" fmt, __FILE__, __LINE__, __func__, \
69 : aicpu::GetTid(), ##__VA_ARGS__); \
70 : } \
71 : } while (0)
72 :
73 : #define KERNEL_LOG_EVENT(fmt, ...) \
74 : do { \
75 : if ((&CheckLogLevel != nullptr) && (&DlogRecord != nullptr)) { \
76 : dlog_info(static_cast<int32_t>(static_cast<uint32_t>(AICPU) | \
77 : static_cast<uint32_t>(RUN_LOG_MASK)), "[%s:%d][%s][tid:%ld]" fmt, \
78 : __FILE__, __LINE__, __func__, aicpu::GetTid(), ##__VA_ARGS__); \
79 : } \
80 : } while (0)
81 : #endif
82 :
83 : #define KERNEL_CHECK_NULLPTR_VOID(value, logText...) \
84 : if (value == nullptr) { \
85 : KERNEL_LOG_ERROR(logText); \
86 : return; \
87 : }
88 :
89 : #define KERNEL_CHECK_FALSE(condition, errorCode, logText...) \
90 : if (!(condition)) { \
91 : KERNEL_LOG_ERROR(logText); \
92 : return errorCode; \
93 : }
94 :
95 : #define KERNEL_CHECK_NULLPTR(value, errorCode, logText...) \
96 : if (value == nullptr) { \
97 : KERNEL_LOG_ERROR(logText); \
98 : return errorCode; \
99 : }
100 :
101 : #define KERNEL_CHECK_ASSIGN_64S_MULTI(A, B, result, errorCode) \
102 : if ((A) != 0 && (B) != 0 && ((INT64_MAX) / (A)) <= (B)) { \
103 : KERNEL_LOG_ERROR("Integer reversed multiA: %ld * multiB: %ld", (A), \
104 : (B)); \
105 : return errorCode; \
106 : } \
107 : (result) = ((A) * (B));
108 :
109 : #define KERNEL_CHECK_FALSE_RUN_INFO(condition, errorCode, logText...) \
110 : if (!(condition)) { \
111 : KERNEL_LOG_EVENT(logText); \
112 : return errorCode; \
113 : }
114 :
115 : #define KERNEL_CHECK_FALSE_VOID(condition, logText...) \
116 : if (!(condition)) { \
117 : KERNEL_LOG_ERROR(logText); \
118 : return; \
119 : }
120 :
121 : #define KERNEL_HANDLE_ERROR(expression, logText...) { \
122 : uint32_t ret = expression; \
123 : if (ret != 0U) { \
124 : KERNEL_LOG_ERROR(logText); \
125 : return ret; \
126 : } \
127 : }
128 :
129 : #define KERNEL_CHECK_ERROR(expression) \
130 : do { \
131 : uint32_t ret = expression; \
132 : if (ret != 0U) { \
133 : return ret; \
134 : } \
135 : } while (0)
136 :
137 : #define KERNEL_CHECK_FALSE_EXEC(condition, execExpr...) if (!(condition)) { \
138 : execExpr; \
139 : }
140 :
141 : #define KERNEL_CHECK_RET(condition, errorCode) \
142 : do { \
143 : if (condition) { \
144 : return errorCode; \
145 : } \
146 : } while (0)
147 :
148 : #endif // AICPU_KERNEL_UTILS_AICPU_KERNEL_LOG_H
|