1 /*
2 * Copyright (c) 2022-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 #include "dump_helper.h"
16 #include <cstdio>
17 #include <utility>
18 #include "hilog_wrapper.h"
19 using namespace OHOS::WallpaperMgrService;
20 namespace OHOS {
21 namespace MiscServices {
GetInstance()22 DumpHelper &DumpHelper::GetInstance()
23 {
24 static DumpHelper instance;
25 return instance;
26 }
27
RegisterCommand(std::shared_ptr<Command> & cmd)28 void DumpHelper::RegisterCommand(std::shared_ptr<Command> &cmd)
29 {
30 cmdHandler_.insert(std::make_pair(cmd->GetOption(), cmd));
31 }
32
Dispatch(int fd,const std::vector<std::string> & args)33 bool DumpHelper::Dispatch(int fd, const std::vector<std::string> &args)
34 {
35 if (args.empty() || args.at(0) == "-h") {
36 dprintf(fd, "\n%-15s: %-20s", "Option", "Description");
37 for (auto &[key, handler] : cmdHandler_) {
38 dprintf(fd, "\n%-15s: %-20s", handler->GetFormat().c_str(), handler->ShowHelp().c_str());
39 }
40 return false;
41 }
42 auto handler = cmdHandler_.find(args.at(0));
43 if (handler != cmdHandler_.end()) {
44 std::string output;
45 bool ret = handler->second->DoAction(args, output);
46 if (!ret) {
47 HILOG_INFO("DoAction faild");
48 }
49 dprintf(fd, "\n%s", output.c_str());
50 return ret;
51 }
52 return false;
53 }
54 } // namespace MiscServices
55 } // namespace OHOS
56