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 <string>
19
20 namespace OHOS::Ace {
21
22 DumpLog::DumpLog() = default;
23 DumpLog::~DumpLog() = default;
24
Print(int32_t depth,const std::string & className,int32_t childSize)25 void DumpLog::Print(int32_t depth, const std::string& className, int32_t childSize)
26 {
27 if (!ostream_->good()) {
28 return;
29 }
30 std::string space = " ";
31 for (int32_t i = 0; i < depth; ++i) {
32 ostream_->write(space.c_str(), space.length());
33 }
34 ostream_->write(space.c_str(), space.length());
35 std::string data = "|-> ";
36 data.append(className);
37 data.append(" childSize:" + std::to_string(childSize));
38 data.append("\n");
39 ostream_->write(data.c_str(), data.length());
40
41 for (auto& desc : description_) {
42 for (int32_t i = 0; i < depth; ++i) {
43 ostream_->write(space.c_str(), space.length());
44 }
45 std::string data = "";
46 if (childSize == 0) {
47 data = " ";
48 } else {
49 data = " | ";
50 }
51 data.append(desc);
52 ostream_->write(data.c_str(), data.length());
53 }
54 ostream_->flush();
55 description_.clear();
56 description_.shrink_to_fit();
57 }
58
Print(const std::string & content)59 void DumpLog::Print(const std::string& content)
60 {
61 Print(0, content);
62 }
63
Print(int32_t depth,const std::string & content)64 void DumpLog::Print(int32_t depth, const std::string& content)
65 {
66 std::string space = " ";
67 for (int32_t i = 0; i < depth; ++i) {
68 ostream_->write(space.c_str(), space.length());
69 }
70
71 std::string data = content + "\n";
72 ostream_->write(data.c_str(), data.length());
73 }
74
Reset()75 void DumpLog::Reset()
76 {
77 ostream_.reset();
78 }
79
ShowDumpHelp(std::vector<std::string> & info)80 void DumpLog::ShowDumpHelp(std::vector<std::string>& info)
81 {
82 info.emplace_back(" -element |show element tree");
83 info.emplace_back(" -render |show render tree");
84 info.emplace_back(" -inspector |show inspector tree");
85 info.emplace_back(" -frontend |show path and components count of current page");
86 }
87
88 } // namespace OHOS::Ace