LCOV - code coverage report
Current view: top level - common - bqs_log.h (source / functions) Coverage Total Hit
Test: coverage.info Lines: 88.9 % 18 16
Test Date: 2026-08-12 11:05:07 Functions: 100.0 % 3 3

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

Generated by: LCOV version 2.0-1