1 /*
2 * Copyright (c) 2021-2022 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 "base/log/dump_log.h"
17
18 #include <fstream>
19
20 #if defined(OHOS_PLATFORM)
21 #include "zlib.h"
22 #endif
23
24 #include "core/common/ace_application_info.h"
25
26 namespace OHOS::Ace {
27
28 #define CHUNK 16384
29
30 #define COMPRESS_VERSION 9
31
32 DumpLog::DumpLog() = default;
33 DumpLog::~DumpLog() = default;
34
Print(int32_t depth,const std::string & className,int32_t childSize)35 void DumpLog::Print(int32_t depth, const std::string& className, int32_t childSize)
36 {
37 if (!ostream_ || !ostream_->good()) {
38 return;
39 }
40 std::string space = " ";
41 for (int32_t i = 0; i < depth; ++i) {
42 ostream_->write(space.c_str(), space.length());
43 }
44 ostream_->write(space.c_str(), space.length());
45 std::string data = "|-> ";
46 data.append(className);
47 data.append(" childSize:" + std::to_string(childSize));
48 data.append("\n");
49 ostream_->write(data.c_str(), data.length());
50 for (auto& desc : description_) {
51 for (int32_t i = 0; i < depth; ++i) {
52 ostream_->write(space.c_str(), space.length());
53 }
54 std::string data = "";
55 if (childSize == 0) {
56 data = " ";
57 } else {
58 data = " | ";
59 }
60 data.append(desc);
61 ostream_->write(data.c_str(), data.length());
62 }
63 ostream_->flush();
64 description_.clear();
65 description_.shrink_to_fit();
66 }
67
Print(const std::string & content)68 void DumpLog::Print(const std::string& content)
69 {
70 Print(0, content);
71 }
72
Print(int32_t depth,const std::string & content)73 void DumpLog::Print(int32_t depth, const std::string& content)
74 {
75 std::string space = " ";
76 for (int32_t i = 0; i < depth; ++i) {
77 ostream_->write(space.c_str(), space.length());
78 }
79 std::string data = content + separator_;
80 ostream_->write(data.c_str(), data.length());
81 }
82
Reset()83 void DumpLog::Reset()
84 {
85 ostream_.reset();
86 }
87
ShowDumpHelp(std::vector<std::string> & info)88 void DumpLog::ShowDumpHelp(std::vector<std::string>& info)
89 {
90 info.emplace_back(" -element |show element tree");
91 info.emplace_back(" -render |show render tree");
92 info.emplace_back(" -inspector |show inspector tree");
93 info.emplace_back(" -frontend |show path and components count of current page");
94 info.emplace_back(" -navigation |show navigation path stack");
95 }
96
Append(int32_t depth,const std::string & className,int32_t childSize)97 void DumpLog::Append(int32_t depth, const std::string& className, int32_t childSize)
98 {
99 for (int32_t i = 0; i < depth; ++i) {
100 result_.append(" ");
101 }
102 result_.append("|-> ");
103 result_.append(className);
104 result_.append(" childSize:" + std::to_string(childSize));
105 result_.append("\n");
106 for (auto& desc : description_) {
107 for (int32_t i = 0; i < depth; ++i) {
108 result_.append(" ");
109 }
110 if (childSize == 0) {
111 result_.append(" ");
112 } else {
113 result_.append(" | ");
114 }
115 result_.append(desc);
116 }
117 description_.clear();
118 description_.shrink_to_fit();
119 }
120
OutPutBySize()121 bool DumpLog::OutPutBySize()
122 {
123 if (!ostream_->good()) {
124 result_.clear();
125 return false;
126 }
127 // if current result size > max size, dump will output as file
128 if (result_.size() + 1 > DumpLog::MAX_DUMP_LENGTH) {
129 auto dumpFilePath = AceApplicationInfo::GetInstance().GetDataFileDirPath() + "/arkui.dump";
130 std::unique_ptr<std::ostream> ostream = std::make_unique<std::ofstream>(dumpFilePath);
131 if (!ostream) {
132 result_.clear();
133 result_.append("Dump output failed,please try again");
134 ostream_->write(result_.c_str(), result_.length());
135 result_.clear();
136 }
137 CHECK_NULL_RETURN(ostream, false);
138 DumpLog::GetInstance().SetDumpFile(std::move(ostream));
139 }
140 ostream_->write(result_.c_str(), result_.length());
141 result_.clear();
142 ostream_->flush();
143 return true;
144 }
145
OutPutDefault()146 void DumpLog::OutPutDefault()
147 {
148 if (!ostream_ || !ostream_->good()) {
149 result_.clear();
150 return;
151 }
152 ostream_->write(result_.c_str(), result_.length());
153 result_.clear();
154 ostream_->flush();
155 }
156
PrintJson(const std::string & content)157 void DumpLog::PrintJson(const std::string& content)
158 {
159 if (!ostream_->good()) {
160 return;
161 }
162 ostream_->write(content.c_str(), content.length());
163 ostream_->flush();
164 }
165
PrintEndDumpInfoNG(bool isElement)166 void DumpLog::PrintEndDumpInfoNG(bool isElement)
167 {
168 int32_t depth = GetDepth() + DumpLog::END_POS_TWO;
169 std::string result;
170 for (int32_t i = 0; i < depth; ++i) {
171 result.append("}");
172 }
173 if (isElement) {
174 Append(result);
175 } else {
176 PrintJson(result);
177 }
178 }
179
GetPrefix(int32_t depth)180 std::string DumpLog::GetPrefix(int32_t depth)
181 {
182 std::string prefix = "";
183 if (depth > 0) {
184 int32_t lastDepth = GetDepth();
185 if (depth == lastDepth) {
186 prefix.append("},");
187 } else if (depth > lastDepth) {
188 prefix = ",";
189 } else {
190 int32_t diff = lastDepth - depth + 1;
191 prefix.assign(diff, '}').append(",");
192 }
193 }
194 SetDepth(depth);
195 return prefix;
196 }
197
FormatDumpInfo(const std::string & str,int32_t depth)198 std::string DumpLog::FormatDumpInfo(const std::string& str, int32_t depth)
199 {
200 if (str.length() > DumpLog::MIN_JSON_LENGTH) {
201 if (depth == 0) {
202 return str.substr(0, str.length() - DumpLog::END_POS_TWO);
203 }
204 return str.substr(1, str.length() - DumpLog::END_POS_THREE);
205 }
206 return str;
207 }
208
209 #if defined(OHOS_PLATFORM)
CompressString(const char * in_str,size_t in_len,std::string & out_str,int level)210 int DumpLog::CompressString(const char* in_str, size_t in_len, std::string& out_str, int level)
211 {
212 if (!in_str)
213 return Z_DATA_ERROR;
214
215 int ret, flush;
216 unsigned have;
217 z_stream strm;
218
219 unsigned char out[CHUNK];
220
221 /* allocate deflate state */
222 strm.zalloc = Z_NULL;
223 strm.zfree = Z_NULL;
224 strm.opaque = Z_NULL;
225 ret = deflateInit(&strm, level);
226 if (ret != Z_OK)
227 return ret;
228
229 std::shared_ptr<z_stream> sp_strm(&strm, [](z_stream* strm) { (void)deflateEnd(strm); });
230 const char* end = in_str + in_len;
231
232 size_t distance = 0;
233 /* compress until end of file */
234 do {
235 distance = end - in_str;
236 strm.avail_in = (distance >= CHUNK) ? CHUNK : distance;
237 strm.next_in = (Bytef*)in_str;
238 in_str += strm.avail_in;
239 flush = (in_str == end) ? Z_FINISH : Z_NO_FLUSH;
240 do {
241 strm.avail_out = CHUNK;
242 strm.next_out = out;
243 ret = deflate(&strm, flush);
244 if (ret == Z_STREAM_ERROR)
245 break;
246 have = CHUNK - strm.avail_out;
247 out_str.append((const char*)out, have);
248 } while (strm.avail_out == 0);
249 if (strm.avail_in != 0) {
250 break;
251 }
252 } while (flush != Z_FINISH);
253 if (ret != Z_STREAM_END)
254 return Z_STREAM_ERROR;
255 return Z_OK;
256 }
257 #endif
258
OutPutByCompress()259 void DumpLog::OutPutByCompress()
260 {
261 if (!ostream_ || !ostream_->good()) {
262 result_.clear();
263 return;
264 }
265 std::string compressString;
266 #if defined(OHOS_PLATFORM)
267 if (CompressString(result_.c_str(), result_.size(), compressString, COMPRESS_VERSION) == Z_OK) {
268 ostream_->write(compressString.c_str(), compressString.length());
269 } else {
270 ostream_->write(result_.c_str(), result_.length());
271 }
272 #else
273 ostream_->write(result_.c_str(), result_.length());
274 #endif
275 result_.clear();
276 ostream_->flush();
277 }
278 } // namespace OHOS::Ace
279