• 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 #include <new>
20 #include <ostream>
21 #include <string>
22 
23 #include "base/log/log_wrapper.h"
24 #include "base/utils/utils.h"
25 #include "core/common/ace_application_info.h"
26 
27 namespace OHOS::Ace {
28 
29 DumpLog::DumpLog() = default;
30 DumpLog::~DumpLog() = default;
31 
Print(int32_t depth,const std::string & className,int32_t childSize)32 void DumpLog::Print(int32_t depth, const std::string& className, int32_t childSize)
33 {
34     if (!ostream_->good()) {
35         return;
36     }
37     std::string space = "  ";
38     for (int32_t i = 0; i < depth; ++i) {
39         ostream_->write(space.c_str(), space.length());
40     }
41     ostream_->write(space.c_str(), space.length());
42     std::string data = "|-> ";
43     data.append(className);
44     data.append(" childSize:" + std::to_string(childSize));
45     data.append("\n");
46     ostream_->write(data.c_str(), data.length());
47     for (auto& desc : description_) {
48         for (int32_t i = 0; i < depth; ++i) {
49             ostream_->write(space.c_str(), space.length());
50         }
51         std::string data = "";
52         if (childSize == 0) {
53             data = "      ";
54         } else {
55             data = "    | ";
56         }
57         data.append(desc);
58         ostream_->write(data.c_str(), data.length());
59     }
60     ostream_->flush();
61     description_.clear();
62     description_.shrink_to_fit();
63 }
64 
Print(const std::string & content)65 void DumpLog::Print(const std::string& content)
66 {
67     Print(0, content);
68 }
69 
Print(int32_t depth,const std::string & content)70 void DumpLog::Print(int32_t depth, const std::string& content)
71 {
72     std::string space = " ";
73     for (int32_t i = 0; i < depth; ++i) {
74         ostream_->write(space.c_str(), space.length());
75     }
76     std::string data = content + "\n";
77     ostream_->write(data.c_str(), data.length());
78 }
79 
Reset()80 void DumpLog::Reset()
81 {
82     ostream_.reset();
83 }
84 
ShowDumpHelp(std::vector<std::string> & info)85 void DumpLog::ShowDumpHelp(std::vector<std::string>& info)
86 {
87     info.emplace_back(" -element                       |show element tree");
88     info.emplace_back(" -render                        |show render tree");
89     info.emplace_back(" -inspector                     |show inspector tree");
90     info.emplace_back(" -frontend                      |show path and components count of current page");
91 }
92 
Append(int32_t depth,const std::string & className,int32_t childSize)93 void DumpLog::Append(int32_t depth, const std::string& className, int32_t childSize)
94 {
95     for (int32_t i = 0; i < depth; ++i) {
96         result_.append("  ");
97     }
98     result_.append("|-> ");
99     result_.append(className);
100     result_.append(" childSize:" + std::to_string(childSize));
101     result_.append("\n");
102     for (auto& desc : description_) {
103         for (int32_t i = 0; i < depth; ++i) {
104             result_.append("  ");
105         }
106         if (childSize == 0) {
107             result_.append("      ");
108         } else {
109             result_.append("    | ");
110         }
111         result_.append(desc);
112     }
113     description_.clear();
114     description_.shrink_to_fit();
115 }
116 
OutPutBySize()117 bool DumpLog::OutPutBySize()
118 {
119     if (!ostream_->good()) {
120         result_.clear();
121         return false;
122     }
123     // if current result size > max size, dump will output as file
124     if (result_.size() + 1 > DumpLog::MAX_DUMP_LENGTH) {
125         auto dumpFilePath = AceApplicationInfo::GetInstance().GetDataFileDirPath() + "/arkui.dump";
126         std::unique_ptr<std::ostream> ostream = std::make_unique<std::ofstream>(dumpFilePath);
127         if (!ostream) {
128             result_.clear();
129             result_.append("Dump output failed,please try again");
130             ostream_->write(result_.c_str(), result_.length());
131             result_.clear();
132         }
133         CHECK_NULL_RETURN(ostream, false);
134         DumpLog::GetInstance().SetDumpFile(std::move(ostream));
135     }
136     ostream_->write(result_.c_str(), result_.length());
137     result_.clear();
138     ostream_->flush();
139     return true;
140 }
141 } // namespace OHOS::Ace
142