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 QUEUE_SCHEDULE_BQS_LOG_H
12 : #define QUEUE_SCHEDULE_BQS_LOG_H
13 :
14 : #include <unistd.h>
15 : #include <cstdint>
16 : #include <cstdio>
17 : #include <string>
18 : #include <memory>
19 : #include <sys/syscall.h>
20 : #include "bqs_weak_log.h"
21 : #include "common/type_def.h"
22 :
23 : namespace bqs {
24 : enum class QsLogFuncId : uint32_t {
25 : FUNC_CHECKLOGLEVEL = 0,
26 : FUNC_DLOGRECORD = 1,
27 : FUNC_DLOGSETLEVEL = 2,
28 : FUNC_DLOGSETATTR = 3,
29 : FUNC_MAXID = 4
30 : };
31 :
32 18488 : inline int64_t GetTid()
33 : {
34 18488 : return syscall(__NR_gettid);
35 : }
36 :
37 : class HostQsLog {
38 : public:
39 : static HostQsLog &GetInstance();
40 : void *OpenLogSo() const;
41 : void *OpenLogSoOne(std::string ascendAicpuPath, const char *soName) const;
42 : void LogPrintNormal(const int32_t moduleId, const int32_t level, const char *fmt, ...) const;
43 : void LogPrintError(const int32_t moduleId, const char *fmt, ...) const;
44 : void DlogSetLevel(const int32_t logLevel, const int32_t eventLevel) const;
45 : void DlogSetAttr(const LogAttr logAttrInfo) const;
46 : bool CheckLogLevelHost(const int32_t moduleId, const int32_t logLevel) const;
47 : bool CheckLogLevel(const int32_t moduleId, const int32_t logLevel) const;
48 :
49 : private:
50 : HostQsLog() = default;
51 : ~HostQsLog() = default;
52 : };
53 :
54 : #ifdef RUN_ON_AICPU
55 : #ifdef QS_DEPLOY_ON_HOST
56 : #define BQS_LOG_DEBUG(fmt, ...) \
57 : do { \
58 : bqs::HostQsLog::GetInstance().LogPrintNormal(static_cast<int32_t>(AICPU), DLOG_DEBUG, \
59 : "[%s:%d][%s][tid:%llu] " fmt, __FILE__, __LINE__, &__FUNCTION__[0], \
60 : bqs::GetTid(), ##__VA_ARGS__); \
61 : } while (false)
62 :
63 : #define BQS_LOG_INFO(fmt, ...) \
64 : do { \
65 : bqs::HostQsLog::GetInstance().LogPrintNormal(static_cast<int32_t>(AICPU), DLOG_INFO, \
66 : "[%s:%d][%s][tid:%llu] " fmt, __FILE__, __LINE__, &__FUNCTION__[0], \
67 : bqs::GetTid(), ##__VA_ARGS__); \
68 : } while (false)
69 :
70 : #define BQS_LOG_WARN(fmt, ...) \
71 : do { \
72 : bqs::HostQsLog::GetInstance().LogPrintNormal(static_cast<int32_t>(AICPU), DLOG_WARN, \
73 : "[%s:%d][%s][tid:%llu] " fmt, __FILE__, __LINE__, &__FUNCTION__[0], \
74 : bqs::GetTid(), ##__VA_ARGS__); \
75 : } while (false)
76 :
77 : #define BQS_LOG_ERROR(fmt, ...) \
78 : do { \
79 : bqs::HostQsLog::GetInstance().LogPrintError(static_cast<int32_t>(AICPU), \
80 : "[%s:%d][%s][tid:%llu] " fmt, __FILE__, __LINE__, &__FUNCTION__[0], \
81 : bqs::GetTid(), ##__VA_ARGS__); \
82 : } while (false)
83 :
84 : #define BQS_LOG_RUN_INFO(fmt, ...) \
85 : do { \
86 : bqs::HostQsLog::GetInstance().LogPrintNormal(static_cast<int32_t>(static_cast<uint32_t>(AICPU) | \
87 : static_cast<uint32_t>(RUN_LOG_MASK)), DLOG_INFO, \
88 : "[%s:%d][%s][tid:%llu] " fmt, __FILE__, __LINE__, &__FUNCTION__[0], \
89 : bqs::GetTid(), ##__VA_ARGS__); \
90 : } while (false)
91 :
92 : #define BQS_LOG_RUN_WARN(fmt, ...) \
93 : do { \
94 : bqs::HostQsLog::GetInstance().LogPrintNormal(static_cast<int32_t>(static_cast<uint32_t>(AICPU) | \
95 : static_cast<uint32_t>(RUN_LOG_MASK)), DLOG_WARN, \
96 : "[%s:%d][%s][tid:%llu] " fmt, __FILE__, __LINE__, &__FUNCTION__[0], \
97 : bqs::GetTid(), ##__VA_ARGS__); \
98 : } while (false)
99 : #else
100 : #define BQS_LOG_DEBUG(fmt, ...) \
101 : if ((&CheckLogLevel != nullptr) && (&DlogRecord != nullptr)) { \
102 : dlog_debug(static_cast<int32_t>(AICPU), "[%s][tid:%llu] " fmt, &__FUNCTION__[0], bqs::GetTid(), ##__VA_ARGS__); \
103 : }
104 :
105 : #define BQS_LOG_INFO(fmt, ...) \
106 : if ((&CheckLogLevel != nullptr) && (&DlogRecord != nullptr)) { \
107 : dlog_info(static_cast<int32_t>(AICPU), "[%s][tid:%llu] " fmt, &__FUNCTION__[0], bqs::GetTid(), ##__VA_ARGS__); \
108 : }
109 :
110 : #define BQS_LOG_WARN(fmt, ...) \
111 : if ((&CheckLogLevel != nullptr) && (&DlogRecord != nullptr)) { \
112 : dlog_warn(static_cast<int32_t>(AICPU), "[%s][tid:%llu] " fmt, &__FUNCTION__[0], bqs::GetTid(), ##__VA_ARGS__); \
113 : }
114 :
115 : #define BQS_LOG_ERROR(fmt, ...) \
116 : if ((&CheckLogLevel != nullptr) && (&DlogRecord != nullptr)) { \
117 : dlog_error(static_cast<int32_t>(AICPU), "[%s][tid:%llu] " fmt, &__FUNCTION__[0], bqs::GetTid(), ##__VA_ARGS__); \
118 : }
119 :
120 : #define BQS_LOG_RUN_INFO(fmt, ...) \
121 : if ((&CheckLogLevel != nullptr) && (&DlogRecord != nullptr)) { \
122 : dlog_info(static_cast<int32_t>(static_cast<uint32_t>(AICPU) | static_cast<uint32_t>(RUN_LOG_MASK)), \
123 : "[%s][tid:%llu] " fmt, &__FUNCTION__[0], bqs::GetTid(), ##__VA_ARGS__); \
124 : }
125 :
126 : #define BQS_LOG_RUN_WARN(fmt, ...) \
127 : if ((&CheckLogLevel != nullptr) && (&DlogRecord != nullptr)) { \
128 : dlog_warn(static_cast<int32_t>(static_cast<uint32_t>(AICPU) | static_cast<uint32_t>(RUN_LOG_MASK)), \
129 : "[%s][tid:%llu] " fmt, &__FUNCTION__[0], bqs::GetTid(), ##__VA_ARGS__); \
130 : }
131 : #endif
132 : #else
133 : #define BQS_LOG_DEBUG(fmt, ...) \
134 : printf("[DEBUG] [%s][%s:%d][tid:%lu]:" fmt "\n", \
135 : __FILE__, \
136 : &__FUNCTION__[0], \
137 : __LINE__, \
138 : bqs::GetTid(), \
139 : ##__VA_ARGS__)
140 : #define BQS_LOG_INFO(fmt, ...) \
141 : printf("[INFO] [%s][%s:%d][tid:%lu]:" fmt "\n", \
142 : __FILE__, \
143 : &__FUNCTION__[0], \
144 : __LINE__, \
145 : bqs::GetTid(), \
146 : ##__VA_ARGS__)
147 : #define BQS_LOG_WARN(fmt, ...) \
148 : printf("[WARN] [%s][%s:%d][tid:%lu]:" fmt "\n", \
149 : __FILE__, \
150 : &__FUNCTION__[0], \
151 : __LINE__, \
152 : bqs::GetTid(), \
153 : ##__VA_ARGS__)
154 : #define BQS_LOG_ERROR(fmt, ...) \
155 : printf("[ERROR] [%s][%s:%d][tid:%lu]:" fmt "\n", \
156 : __FILE__, \
157 : &__FUNCTION__[0], \
158 : __LINE__, \
159 : bqs::GetTid(), \
160 : ##__VA_ARGS__)
161 : #define BQS_LOG_RUN_INFO(fmt, ...) \
162 : printf("[INFO] [%s][%s:%d][tid:%lu]:" fmt "\n", \
163 : __FILE__, \
164 : &__FUNCTION__[0], \
165 : __LINE__, \
166 : bqs::GetTid(), \
167 : ##__VA_ARGS__)
168 : #define BQS_LOG_RUN_WARN(fmt, ...) \
169 : printf("[INFO] [%s][%s:%d][tid:%lu]:" fmt "\n", \
170 : __FILE__, \
171 : &__FUNCTION__[0], \
172 : __LINE__, \
173 : bqs::GetTid(), \
174 : ##__VA_ARGS__)
175 : #endif
176 : #define DGW_LOG_DEBUG BQS_LOG_DEBUG
177 : #define DGW_LOG_INFO BQS_LOG_INFO
178 : #define DGW_LOG_WARN BQS_LOG_WARN
179 : #define DGW_LOG_ERROR BQS_LOG_ERROR
180 : #define DGW_LOG_RUN_INFO BQS_LOG_RUN_INFO
181 : #define DGW_LOG_RUN_WARN BQS_LOG_RUN_WARN
182 : // cond为真时,进行后续的log打印
183 : #define BQS_LOG_ERROR_WHEN(cond, log, ...) \
184 : if (cond) { \
185 : BQS_LOG_ERROR(log, ##__VA_ARGS__); \
186 : }
187 :
188 : // 注意: 该宏第一项Condition是期望为真,否则将进行后续的返回和日志记录
189 : #define DGW_CHECK(condition, retValue, log, ...) \
190 : do { \
191 : const bool cond = (condition); \
192 : if (!cond) { \
193 : DGW_LOG_ERROR(log, ##__VA_ARGS__); \
194 : return (retValue); \
195 : } \
196 : } while (false)
197 :
198 : // 注意: 该宏第一项Condition是期望为真,否则将进行后续的返回和日志记录
199 : #define DGW_CHECK_RET_VOID(condition, log, ...) \
200 : do { \
201 : const bool cond = (condition); \
202 : if (!cond) { \
203 : DGW_LOG_ERROR(log, ##__VA_ARGS__); \
204 : return; \
205 : } \
206 : } while (false)
207 :
208 : /**
209 : * check two uint32 integer adding weather overflow. execute adding when not overflow.
210 : * @param onceOverFlow weather overflow in history calculation:
211 : * true: overflow in history or current calculation
212 : * false: never overflow in history or current calculation
213 : */
214 57 : inline void BqsCheckAssign32UAdd(const uint32_t para1,
215 : const uint32_t para2, uint32_t &result, bool &onceOverFlow)
216 : {
217 57 : if (onceOverFlow) {
218 0 : return;
219 : }
220 57 : if ((UINT32_MAX - para1) < para2) {
221 1 : BQS_LOG_ERROR("Integer reversed para1: %u + para2: %u", para1, para2);
222 1 : onceOverFlow = true;
223 1 : return;
224 : }
225 56 : result = para1 + para2;
226 56 : return;
227 : }
228 :
229 : /**
230 : * check two uint32 integer multiplication weather overflow. execute multiplication when not overflow.
231 : * @param onceOverFlow weather overflow in history calculation:
232 : * true: overflow in history or current calculation
233 : * false: never overflow in history or current calculation
234 : */
235 : inline void BqsCheckAssign32UMuti(const uint32_t para1,
236 : const uint32_t para2, uint32_t &result, bool &onceOverFlow)
237 : {
238 : if (onceOverFlow) {
239 : return;
240 : }
241 : if ((para1 != 0U) && (para2 != 0U) && ((UINT32_MAX / para1) < para2)) {
242 : BQS_LOG_ERROR("Integer reversed para1: %u * para2: %u", para1, para2);
243 : onceOverFlow = true;
244 : return;
245 : }
246 : result = para1 * para2;
247 : return;
248 : }
249 :
250 7 : inline uint64_t BqsCheckAssign64UAdd(const uint64_t para1, const uint64_t para2, bool &onceOverFlow)
251 : {
252 7 : if (onceOverFlow) {
253 0 : return 0U;
254 : }
255 7 : if ((UINT64_MAX - para1) < para2) {
256 1 : BQS_LOG_ERROR("Integer reversed para1: %lu + para2: %lu", para1, para2);
257 1 : onceOverFlow = true;
258 1 : return 0U;
259 : }
260 6 : return para1 + para2;
261 : }
262 :
263 : #define BQS_LOG_WARN_HOST(fmt, ...) \
264 : do { \
265 : bqs::HostQsLog::GetInstance().LogPrintNormal(static_cast<int32_t>(AICPU), DLOG_WARN, \
266 : "[%s:%d][%s][tid:%llu] " fmt, __FILE__, __LINE__, &__FUNCTION__[0], \
267 : bqs::GetTid(), ##__VA_ARGS__); \
268 : } while (false)
269 : }
270 :
271 : #endif // QUEUE_SCHEDULE_BQS_LOG_H
|