1 /*
2 * Copyright (C) 2021 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 #include "executor/list_dumper.h"
16
17 #include "dump_utils.h"
18 #include "manager/dump_implement.h"
19 #include "util/config_utils.h"
20 namespace OHOS {
21 namespace HiviewDFX {
22 const std::string ListDumper::ABILITY_HEADER = "System ability list:";
23 const std::string ListDumper::SYSTEM_HEADER = "System cluster list:";
24 const int ListDumper::ITEM_SUM_LINE = 3;
25 const std::string ListDumper::NAME_SPACE = " ";
ListDumper()26 ListDumper::ListDumper()
27 {
28 }
29
~ListDumper()30 ListDumper::~ListDumper()
31 {
32 }
33
PreExecute(const std::shared_ptr<DumperParameter> & parameter,StringMatrix dumpDatas)34 DumpStatus ListDumper::PreExecute(const std::shared_ptr<DumperParameter>& parameter,
35 StringMatrix dumpDatas)
36 {
37 target_ = ptrDumpCfg_->target_;
38 if (dumpDatas.get()) {
39 result_ = dumpDatas;
40 return DumpStatus::DUMP_OK;
41 }
42 return DumpStatus::DUMP_FAIL;
43 }
44
Execute()45 DumpStatus ListDumper::Execute()
46 {
47 std::string header;
48 std::vector<std::string> list;
49 if (target_ == ConfigUtils::STR_ABILITY) {
50 header = ABILITY_HEADER;
51 auto sma = DumpImplement::GetInstance().GetSystemAbilityManager();
52 if (sma == nullptr) {
53 return DumpStatus::DUMP_OK;
54 }
55 std::vector<std::u16string> abilities = sma->ListSystemAbilities();
56 std::transform(abilities.begin(), abilities.end(), std::back_inserter(list), Str16ToStr8);
57 std::transform(list.begin(), list.end(), list.begin(), DumpUtils::ConvertSaIdToSaName);
58 } else if (target_ == ConfigUtils::STR_SYSTEM) {
59 header = SYSTEM_HEADER;
60 ConfigUtils::GetSectionNames(ConfigUtils::CONFIG_GROUP_SYSTEM_, list);
61 }
62 if (!header.empty()) {
63 std::vector<std::string> line_vector;
64 line_vector.push_back(header);
65 result_->push_back(line_vector);
66 line_vector.clear();
67 int sum = 0;
68 for (size_t i = 0; i < list.size(); i++) {
69 std::string showname = list[i] + AppendBlank(list[i]);
70 line_vector.push_back(showname);
71 sum = sum + 1;
72 if (sum < ITEM_SUM_LINE) {
73 continue;
74 }
75 result_->push_back(line_vector);
76 line_vector.clear();
77 sum = 0;
78 }
79 if (!line_vector.empty()) {
80 result_->push_back(line_vector);
81 line_vector.clear();
82 }
83 line_vector.clear();
84 result_->push_back(line_vector);
85 }
86 return DumpStatus::DUMP_OK;
87 }
88
AfterExecute()89 DumpStatus ListDumper::AfterExecute()
90 {
91 DUMPER_HILOGD(MODULE_COMMON, "enter|");
92 for (size_t lineIndex = 0; lineIndex < result_->size(); lineIndex++) {
93 std::vector<std::string> line = result_->at(lineIndex);
94 for (size_t itemIndex = 0; itemIndex < line.size(); itemIndex++) {
95 std::string item = line[itemIndex];
96 DUMPER_HILOGD(MODULE_COMMON, "debug|item[%{public}zu, %{public}zu]=[%{public}s]",
97 lineIndex, itemIndex, item.c_str());
98 }
99 }
100 DUMPER_HILOGD(MODULE_COMMON, "leave|");
101 return DumpStatus::DUMP_OK;
102 }
103
AppendBlank(const std::string & str)104 const std::string ListDumper::AppendBlank(const std::string& str)
105 {
106 std::string spaces;
107 if (str.length() < NAME_SPACE.length()) {
108 spaces = NAME_SPACE.substr(str.length());
109 }
110 return spaces;
111 }
112 } // namespace HiviewDFX
113 } // namespace OHOS
114