1 /*
2 * Copyright (c) 2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "hilog_ani_base.h"
17
18 #include <ani.h>
19 #include <array>
20
21 #include "hilog/log.h"
22 #include "hilog/log_c.h"
23 #include "hilog/log_cpp.h"
24 #include "hilog_common.h"
25 #include "properties.h"
26 #include "securec.h"
27
28 namespace OHOS {
29 namespace HiviewDFX {
30 const HiLogLabel LABEL = { LOG_CORE, 0xD002D00, "HILOG_ETS" };
31 static constexpr int MIN_NUMBER = 0;
32 static constexpr int MAX_NUMBER = 97;
33 static constexpr int PUBLIC_LEN = 6;
34 static constexpr int PRIVATE_LEN = 7;
35 static constexpr int PROPERTY_POS = 2;
36 static const std::string PRIV_STR = "<private>";
37
HandleFormatFlags(const std::string & formatStr,uint32_t & pos,bool & showPriv)38 static void HandleFormatFlags(const std::string& formatStr, uint32_t& pos, bool& showPriv)
39 {
40 if ((pos + PUBLIC_LEN + PROPERTY_POS) < formatStr.size() &&
41 formatStr.substr(pos + PROPERTY_POS, PUBLIC_LEN) == "public") {
42 pos += (PUBLIC_LEN + PROPERTY_POS);
43 showPriv = false;
44 }
45 if ((pos + PRIVATE_LEN + PROPERTY_POS) < formatStr.size() &&
46 formatStr.substr(pos + PROPERTY_POS, PRIVATE_LEN) == "private") {
47 pos += (PRIVATE_LEN + PROPERTY_POS);
48 }
49 }
50
ProcessLogContent(const std::string & formatStr,const std::vector<AniParam> & params,bool isPriv,LogContentPosition & contentPos,std::string & ret)51 static void ProcessLogContent(const std::string &formatStr, const std::vector<AniParam> ¶ms, bool isPriv,
52 LogContentPosition &contentPos, std::string &ret)
53 {
54 if (contentPos.pos + 1 >= formatStr.size()) {
55 return;
56 }
57 switch (formatStr[contentPos.pos + 1]) {
58 case 'd':
59 case 'i':
60 if (params[contentPos.count].type == AniArgsType::ANI_INT ||
61 params[contentPos.count].type == AniArgsType::ANI_NUMBER ||
62 params[contentPos.count].type == AniArgsType::ANI_BIGINT) {
63 ret += isPriv ? PRIV_STR : params[contentPos.count].val;
64 }
65 ++contentPos.count;
66 ++contentPos.pos;
67 break;
68 case 's':
69 if (params[contentPos.count].type == AniArgsType::ANI_STRING ||
70 params[contentPos.count].type == AniArgsType::ANI_UNDEFINED ||
71 params[contentPos.count].type == AniArgsType::ANI_BOOLEAN ||
72 params[contentPos.count].type == AniArgsType::ANI_NULL) {
73 ret += isPriv ? PRIV_STR : params[contentPos.count].val;
74 }
75 ++contentPos.count;
76 ++contentPos.pos;
77 break;
78 case 'O':
79 case 'o':
80 if (params[contentPos.count].type == AniArgsType::ANI_OBJECT) {
81 ret += isPriv ? PRIV_STR : params[contentPos.count].val;
82 }
83 ++contentPos.count;
84 ++contentPos.pos;
85 break;
86 case '%':
87 ret += formatStr[contentPos.pos];
88 ++contentPos.pos;
89 break;
90 default:
91 ret += formatStr[contentPos.pos];
92 break;
93 }
94 }
95
ParseLogContent(std::string & formatStr,std::vector<AniParam> & params,std::string & logContent)96 void ParseLogContent(std::string& formatStr, std::vector<AniParam>& params, std::string& logContent)
97 {
98 if (params.empty()) {
99 logContent += formatStr;
100 return;
101 }
102 auto size = params.size();
103 auto len = formatStr.size();
104 LogContentPosition contentPos;
105 bool isPrivateEnable = true;
106 #if not (defined(__WINDOWS__) || defined(__MAC__) || defined(__LINUX__))
107 isPrivateEnable = IsPrivateModeEnable();
108 #endif
109 for (; contentPos.pos < len; ++contentPos.pos) {
110 bool showPriv = true;
111 if (contentPos.count >= size) {
112 break;
113 }
114 if (formatStr[contentPos.pos] != '%') {
115 logContent += formatStr[contentPos.pos];
116 continue;
117 }
118 HandleFormatFlags(formatStr, contentPos.pos, showPriv);
119 bool isPriv = isPrivateEnable && showPriv;
120 ProcessLogContent(formatStr, params, isPriv, contentPos, logContent);
121 }
122 if (contentPos.pos < len) {
123 logContent += formatStr.substr(contentPos.pos, len - contentPos.pos);
124 }
125 }
126
HilogImpl(ani_env * env,ani_double domain,ani_string tag,ani_string format,ani_array args,int level,bool isAppLog)127 void HilogAniBase::HilogImpl(ani_env *env, ani_double domain, ani_string tag,
128 ani_string format, ani_array args, int level, bool isAppLog)
129 {
130 ani_size length = 0;
131 if (ANI_OK != env->Array_GetLength(args, &length)) {
132 HiLog::Warn(LABEL, "Get array length failed");
133 return;
134 }
135 if (MIN_NUMBER > length || MAX_NUMBER < length) {
136 HiLog::Warn(LABEL, "Argc mismatch, length:%{public}zu", length);
137 return;
138 }
139 std::string logContent;
140 std::vector<AniParam> params;
141 for (ani_size i = 0; i < length; i++) {
142 ani_ref element;
143 if (ANI_OK != env->Array_Get_Ref(static_cast<ani_array_ref>(args), i, &element)) {
144 HiLog::Warn(LABEL, "Get element at index %{public}zu from array failed", i);
145 return;
146 }
147 ParseAniValue(env, element, params);
148 }
149 std::string fmtString = AniUtil::AniStringToStdString(env, format);
150 ParseLogContent(fmtString, params, logContent);
151 int32_t domainVal = static_cast<int32_t>(domain);
152 std::string tagString = AniUtil::AniStringToStdString(env, tag);
153 HiLogPrint((isAppLog ? LOG_APP : LOG_CORE),
154 static_cast<LogLevel>(level), domainVal, tagString.c_str(), "%{public}s", logContent.c_str());
155 }
156
ParseAniValue(ani_env * env,ani_ref element,std::vector<AniParam> & params)157 void HilogAniBase::ParseAniValue(ani_env *env, ani_ref element, std::vector<AniParam>& params)
158 {
159 AniParam res;
160 AniArgsType type = AniUtil::AniArgGetType(env, static_cast<ani_object>(element));
161 res.type = type;
162 if (type == AniArgsType::ANI_UNDEFINED) {
163 res.val = "undefined";
164 } else if (type == AniArgsType::ANI_NULL) {
165 res.val = "null";
166 } else if (type != AniArgsType::ANI_UNKNOWN) {
167 res.val = AniUtil::AniArgToString(env, static_cast<ani_object>(element));
168 } else {
169 HiLog::Info(LABEL, "%{public}s", "Type mismatch");
170 }
171 params.emplace_back(res);
172 }
173
Debug(ani_env * env,ani_double domain,ani_string tag,ani_string format,ani_array args)174 void HilogAniBase::Debug(ani_env *env, ani_double domain, ani_string tag,
175 ani_string format, ani_array args)
176 {
177 return HilogImpl(env, domain, tag, format, args, LOG_DEBUG, true);
178 }
179
Info(ani_env * env,ani_double domain,ani_string tag,ani_string format,ani_array args)180 void HilogAniBase::Info(ani_env *env, ani_double domain, ani_string tag,
181 ani_string format, ani_array args)
182 {
183 return HilogImpl(env, domain, tag, format, args, LOG_INFO, true);
184 }
185
Warn(ani_env * env,ani_double domain,ani_string tag,ani_string format,ani_array args)186 void HilogAniBase::Warn(ani_env *env, ani_double domain, ani_string tag,
187 ani_string format, ani_array args)
188 {
189 return HilogImpl(env, domain, tag, format, args, LOG_WARN, true);
190 }
191
Error(ani_env * env,ani_double domain,ani_string tag,ani_string format,ani_array args)192 void HilogAniBase::Error(ani_env *env, ani_double domain, ani_string tag,
193 ani_string format, ani_array args)
194 {
195 return HilogImpl(env, domain, tag, format, args, LOG_ERROR, true);
196 }
197
Fatal(ani_env * env,ani_double domain,ani_string tag,ani_string format,ani_array args)198 void HilogAniBase::Fatal(ani_env *env, ani_double domain, ani_string tag,
199 ani_string format, ani_array args)
200 {
201 return HilogImpl(env, domain, tag, format, args, LOG_FATAL, true);
202 }
203
IsLoggable(ani_env * env,ani_double domain,ani_string tag,ani_enum_item level)204 ani_boolean HilogAniBase::IsLoggable(ani_env *env, ani_double domain, ani_string tag,
205 ani_enum_item level)
206 {
207 int32_t domainVal = static_cast<int32_t>(domain);
208 if (domainVal < static_cast<int32_t>(DOMAIN_APP_MIN) || (domainVal > static_cast<int32_t>(DOMAIN_APP_MAX))) {
209 return static_cast<ani_boolean>(false);
210 }
211 int32_t levelVal;
212 std::string tagStr = AniUtil::AniStringToStdString(env, tag);
213 if (!AniUtil::AniEnumToInt32(env, level, levelVal)) {
214 return static_cast<ani_boolean>(false);
215 }
216 bool res = HiLogIsLoggable(domainVal, tagStr.c_str(), static_cast<LogLevel>(levelVal));
217 return static_cast<ani_boolean>(res);
218 }
219
SetMinLogLevel(ani_env * env,ani_enum_item level)220 void HilogAniBase::SetMinLogLevel(ani_env *env, ani_enum_item level)
221 {
222 int32_t levelVal = LOG_LEVEL_MIN;
223 if (!AniUtil::AniEnumToInt32(env, level, levelVal)) {
224 return;
225 }
226 HiLogSetAppMinLogLevel(static_cast<LogLevel>(levelVal));
227 }
228 } // namespace HiviewDFX
229 } // namespace OHOS
230