• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 + (isUIExt_ ? ";" : "\n");
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         std::string tmp;
126         result_.swap(tmp);
127         return false;
128     }
129     // if current result size > max size, dump will output as file
130     if (result_.size() + 1 > DumpLog::MAX_DUMP_LENGTH || isUIExt_) {
131         auto dumpFilePath = AceApplicationInfo::GetInstance().GetDataFileDirPath() + "/arkui.dump";
132         std::unique_ptr<std::ostream> ostream = std::make_unique<std::ofstream>(dumpFilePath);
133         if (!ostream) {
134             result_.clear();
135             result_.append("Dump output failed,please try again");
136             ostream_->write(result_.c_str(), result_.length());
137             result_.clear();
138             std::string tmp;
139             result_.swap(tmp);
140         }
141         CHECK_NULL_RETURN(ostream, false);
142         DumpLog::GetInstance().SetDumpFile(std::move(ostream));
143     }
144     ostream_->write(result_.c_str(), result_.length());
145     result_.clear();
146     std::string tmp;
147     result_.swap(tmp);
148     ostream_->flush();
149     return true;
150 }
151 
OutPutDefault()152 void DumpLog::OutPutDefault()
153 {
154     if (!ostream_ || !ostream_->good()) {
155         result_.clear();
156         std::string tmp;
157         result_.swap(tmp);
158         return;
159     }
160     ostream_->write(result_.c_str(), result_.length());
161     result_.clear();
162     std::string tmp;
163     result_.swap(tmp);
164     ostream_->flush();
165 }
166 
PrintJson(const std::string & content)167 void DumpLog::PrintJson(const std::string& content)
168 {
169     if (!ostream_->good()) {
170         return;
171     }
172     ostream_->write(content.c_str(), content.length());
173     ostream_->flush();
174 }
175 
PrintEndDumpInfoNG(bool isElement)176 void DumpLog::PrintEndDumpInfoNG(bool isElement)
177 {
178     int32_t depth = GetDepth() + DumpLog::END_POS_TWO;
179     std::string result;
180     for (int32_t i = 0; i < depth; ++i) {
181         result.append("}");
182     }
183     if (isElement) {
184         Append(result);
185     } else {
186         PrintJson(result);
187     }
188 }
189 
GetPrefix(int32_t depth)190 std::string DumpLog::GetPrefix(int32_t depth)
191 {
192     std::string prefix = "";
193     if (depth > 0) {
194         int32_t lastDepth = GetDepth();
195         if (depth == lastDepth) {
196             prefix.append("},");
197         } else if (depth > lastDepth) {
198             prefix = ",";
199         } else {
200             int32_t diff = lastDepth - depth + 1;
201             prefix.assign(diff, '}').append(",");
202         }
203     }
204     SetDepth(depth);
205     return prefix;
206 }
207 
FormatDumpInfo(const std::string & str,int32_t depth)208 std::string DumpLog::FormatDumpInfo(const std::string& str, int32_t depth)
209 {
210     if (str.length() > DumpLog::MIN_JSON_LENGTH) {
211         if (depth == 0) {
212             return str.substr(0, str.length() - DumpLog::END_POS_TWO);
213         }
214         return str.substr(1, str.length() - DumpLog::END_POS_THREE);
215     }
216     return str;
217 }
218 
219 #if defined(OHOS_PLATFORM)
CompressString(const char * in_str,size_t in_len,std::string & out_str,int level)220 int DumpLog::CompressString(const char* in_str, size_t in_len, std::string& out_str, int level)
221 {
222     if (!in_str)
223         return Z_DATA_ERROR;
224 
225     int ret;
226     int flush;
227     unsigned have;
228     z_stream strm;
229 
230     unsigned char out[CHUNK];
231 
232     /* allocate deflate state */
233     strm.zalloc = Z_NULL;
234     strm.zfree = Z_NULL;
235     strm.opaque = Z_NULL;
236     ret = deflateInit(&strm, level);
237     if (ret != Z_OK)
238         return ret;
239 
240     std::shared_ptr<z_stream> sp_strm(&strm, [](z_stream* strm) { (void)deflateEnd(strm); });
241     const char* end = in_str + in_len;
242 
243     size_t distance = 0;
244     /* compress until end of file */
245     do {
246         distance = end - in_str;
247         strm.avail_in = (distance >= CHUNK) ? CHUNK : distance;
248         strm.next_in = (Bytef*)in_str;
249         in_str += strm.avail_in;
250         flush = (in_str == end) ? Z_FINISH : Z_NO_FLUSH;
251         do {
252             strm.avail_out = CHUNK;
253             strm.next_out = out;
254             ret = deflate(&strm, flush);
255             if (ret == Z_STREAM_ERROR)
256                 break;
257             have = CHUNK - strm.avail_out;
258             out_str.append((const char*)out, have);
259         } while (strm.avail_out == 0);
260         if (strm.avail_in != 0) {
261             break;
262         }
263     } while (flush != Z_FINISH);
264     if (ret != Z_STREAM_END)
265         return Z_STREAM_ERROR;
266     return Z_OK;
267 }
268 #endif
269 
OutPutByCompress()270 void DumpLog::OutPutByCompress()
271 {
272     if (!ostream_ || !ostream_->good()) {
273         result_.clear();
274         std::string tmp;
275         result_.swap(tmp);
276         return;
277     }
278     std::string compressString;
279 #if defined(OHOS_PLATFORM)
280     if (CompressString(result_.c_str(), result_.size(), compressString, COMPRESS_VERSION) == Z_OK) {
281         ostream_->write(compressString.c_str(), compressString.length());
282     } else {
283         ostream_->write(result_.c_str(), result_.length());
284     }
285 #else
286     ostream_->write(result_.c_str(), result_.length());
287 #endif
288     result_.clear();
289     std::string tmp;
290     result_.swap(tmp);
291     ostream_->flush();
292 }
293 } // namespace OHOS::Ace
294