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     DUMPER_HILOGI(MODULE_COMMON, "info|ListDumper Execute");
48     std::string header;
49     std::vector<std::string> list;
50     if (target_ == ConfigUtils::STR_ABILITY) {
51         header = ABILITY_HEADER;
52         auto sma = DumpImplement::GetInstance().GetSystemAbilityManager();
53         if (sma == nullptr) {
54             return DumpStatus::DUMP_OK;
55         }
56         std::vector<std::u16string> abilities = sma->ListSystemAbilities();
57         std::transform(abilities.begin(), abilities.end(), std::back_inserter(list), Str16ToStr8);
58         std::transform(list.begin(), list.end(), list.begin(), DumpUtils::ConvertSaIdToSaName);
59     } else if (target_ == ConfigUtils::STR_SYSTEM) {
60         header = SYSTEM_HEADER;
61         ConfigUtils::GetSectionNames(ConfigUtils::CONFIG_GROUP_SYSTEM_, list);
62     }
63     if (!header.empty()) {
64         std::vector<std::string> line_vector;
65         line_vector.push_back(header);
66         result_->push_back(line_vector);
67         line_vector.clear();
68         int sum = 0;
69         for (size_t i = 0; i < list.size(); i++) {
70             std::string showname = list[i] + AppendBlank(list[i]);
71             line_vector.push_back(showname);
72             sum = sum + 1;
73             if (sum < ITEM_SUM_LINE) {
74                 continue;
75             }
76             result_->push_back(line_vector);
77             line_vector.clear();
78             sum = 0;
79         }
80         if (!line_vector.empty()) {
81             result_->push_back(line_vector);
82             line_vector.clear();
83         }
84         line_vector.clear();
85         result_->push_back(line_vector);
86     }
87     DUMPER_HILOGI(MODULE_COMMON, "info|ListDumper Execute end");
88     return DumpStatus::DUMP_OK;
89 }
90 
AfterExecute()91 DumpStatus ListDumper::AfterExecute()
92 {
93     DUMPER_HILOGD(MODULE_COMMON, "enter|");
94     for (size_t lineIndex = 0; lineIndex < result_->size(); lineIndex++) {
95         std::vector<std::string> line = result_->at(lineIndex);
96         for (size_t itemIndex = 0; itemIndex < line.size(); itemIndex++) {
97             std::string item = line[itemIndex];
98             DUMPER_HILOGD(MODULE_COMMON, "debug|item[%{public}zu, %{public}zu]=[%{public}s]",
99                 lineIndex, itemIndex, item.c_str());
100         }
101     }
102     DUMPER_HILOGD(MODULE_COMMON, "leave|");
103     return DumpStatus::DUMP_OK;
104 }
105 
AppendBlank(const std::string & str)106 const std::string ListDumper::AppendBlank(const std::string& str)
107 {
108     std::string spaces;
109     if (str.length() < NAME_SPACE.length()) {
110         spaces = NAME_SPACE.substr(str.length());
111     }
112     return spaces;
113 }
114 } // namespace HiviewDFX
115 } // namespace OHOS
116