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 : #include "log_inner.h"
12 :
13 : #include "base/err_msg.h"
14 :
15 : namespace acl {
16 : bool AclLog::isEnableEvent_ = false;
17 : constexpr size_t const ACL_LIMIT_PER_MESSAGE = 1024U;
18 :
19 3 : std::string AclGetErrorFormatMessage(const mmErrorMsg errnum)
20 : {
21 3 : char_t errBuff[acl::MAX_ERROR_STRING + 1U] = {};
22 3 : const auto msgPtr = mmGetErrorFormatMessage(errnum, &errBuff[0], acl::MAX_ERROR_STRING);
23 3 : const auto errMessage = (msgPtr == nullptr) ? "" : msgPtr;
24 6 : return std::string(errMessage);
25 : }
26 :
27 6758 : aclLogLevel AclLog::GetCurLogLevel()
28 : {
29 6758 : int32_t eventEnable = 0;
30 6758 : const int32_t dlogLevel = dlog_getlevel(ACL_MODE_ID, &eventEnable);
31 6758 : aclLogLevel curLevel = ACL_INFO;
32 6758 : switch (dlogLevel) {
33 1 : case DLOG_ERROR:
34 1 : curLevel = ACL_ERROR;
35 1 : break;
36 1 : case DLOG_WARN:
37 1 : curLevel = ACL_WARNING;
38 1 : break;
39 1 : case DLOG_INFO:
40 1 : curLevel = ACL_INFO;
41 1 : break;
42 6754 : case DLOG_DEBUG:
43 6754 : curLevel = ACL_DEBUG;
44 6754 : break;
45 1 : default:
46 1 : break;
47 : }
48 :
49 6758 : isEnableEvent_ = (eventEnable == 1); // Out put event enable
50 6758 : return curLevel;
51 : }
52 :
53 1570 : bool AclLog::IsEventLogOutputEnable()
54 : {
55 1570 : (void)AclLog::GetCurLogLevel();
56 1570 : return isEnableEvent_;
57 : }
58 :
59 5183 : bool AclLog::IsLogOutputEnable(const aclLogLevel logLevel)
60 : {
61 5183 : const aclLogLevel curLevel = AclLog::GetCurLogLevel();
62 5183 : return (curLevel <= logLevel);
63 : }
64 :
65 6201 : mmPid_t AclLog::GetTid()
66 : {
67 6201 : static const thread_local mmPid_t tid = static_cast<mmPid_t>(mmGetTid());
68 6201 : return tid;
69 : }
70 :
71 11 : void AclLog::ACLSaveLog(const aclLogLevel logLevel, const char_t *const strLog)
72 : {
73 11 : if (strLog == nullptr) {
74 0 : return;
75 : }
76 11 : switch (logLevel) {
77 7 : case ACL_ERROR:
78 7 : DlogRecord(APP_MODE_ID, DLOG_ERROR, "%s", strLog);
79 7 : break;
80 1 : case ACL_WARNING:
81 1 : DlogRecord(APP_MODE_ID, DLOG_WARN, "%s", strLog);
82 1 : break;
83 2 : case ACL_INFO:
84 2 : DlogRecord(APP_MODE_ID, DLOG_INFO, "%s", strLog);
85 2 : break;
86 1 : case ACL_DEBUG:
87 1 : DlogRecord(APP_MODE_ID, DLOG_DEBUG, "%s", strLog);
88 1 : break;
89 0 : default:
90 0 : break;
91 : }
92 : }
93 :
94 8 : std::string AclErrorLogManager::FormatStr(const char_t *const fmt, ...)
95 : {
96 8 : if (fmt == nullptr) {
97 1 : return "";
98 : }
99 :
100 7 : va_list ap;
101 7 : va_start(ap, fmt);
102 7 : char_t str[acl::MAX_LOG_STRING] = {};
103 7 : const int32_t printRet = vsnprintf_s(str, static_cast<size_t>(acl::MAX_LOG_STRING),
104 : static_cast<size_t>(acl::MAX_LOG_STRING - 1U), fmt, ap);
105 7 : if (printRet == -1) {
106 1 : va_end(ap);
107 1 : return "";
108 : }
109 6 : va_end(ap);
110 6 : return str;
111 : }
112 :
113 63 : void AclErrorLogManager::ReportInputError(const char *errorCode, const std::vector<const char *> &key,
114 : const std::vector<const char *> &val)
115 : {
116 63 : REPORT_PREDEFINED_ERR_MSG(errorCode, key, val);
117 63 : }
118 :
119 1 : void AclErrorLogManager::ReportInnerError(const char_t *const fmt, ...)
120 : {
121 1 : va_list ap;
122 1 : va_start(ap, fmt);
123 1 : char_t errorMsgStr[ACL_LIMIT_PER_MESSAGE] = {};
124 1 : const int32_t ret = vsnprintf_s(errorMsgStr, static_cast<size_t>(ACL_LIMIT_PER_MESSAGE),
125 : static_cast<size_t>(ACL_LIMIT_PER_MESSAGE - 1U), fmt, ap);
126 1 : if (ret == -1) {
127 0 : va_end(ap);
128 0 : ACL_LOG_ERROR("[Call][Vsnprintf]call vsnprintf failed, ret = %d", ret);
129 0 : return;
130 : }
131 1 : va_end(ap);
132 1 : REPORT_INNER_ERR_MSG("EH9999", "%s", errorMsgStr);
133 : }
134 :
135 1 : void AclErrorLogManager::ReportCallError(const char_t *const fmt, ...)
136 : {
137 1 : va_list ap;
138 1 : va_start(ap, fmt);
139 1 : char_t errorMsgStr[ACL_LIMIT_PER_MESSAGE] = {};
140 1 : const int32_t ret = vsnprintf_s(errorMsgStr, static_cast<size_t>(ACL_LIMIT_PER_MESSAGE),
141 : static_cast<size_t>(ACL_LIMIT_PER_MESSAGE - 1U), fmt, ap);
142 1 : if (ret == -1) {
143 0 : va_end(ap);
144 0 : ACL_LOG_ERROR("[Call][Vsnprintf]call vsnprintf failed, ret = %d", ret);
145 0 : return;
146 : }
147 1 : va_end(ap);
148 1 : REPORT_INNER_ERR_MSG("EH9999", "%s", errorMsgStr);
149 : }
150 :
151 243 : void AclErrorLogManager::ReportInputErrorWithChar(const char_t *const errorCode, const char_t *const argNames[],
152 : const char_t *const argVals[], const size_t size)
153 : {
154 486 : std::vector<const char *> argNameArr;
155 486 : std::vector<const char *> argValArr;
156 507 : for (size_t i = 0U; i < size; ++i) {
157 264 : argNameArr.push_back(argNames[i]);
158 264 : argValArr.push_back(argVals[i]);
159 : }
160 243 : REPORT_PREDEFINED_ERR_MSG(errorCode, argNameArr, argValArr);
161 243 : }
162 : } // namespace acl
|