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