• 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  * miscservices under the License is miscservices 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 "dump_helper.h"
17 
18 #include <cstdio>
19 
20 namespace OHOS {
21 namespace ScreenLock {
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     dprintf(fd, "\n---------------------------------\n");
36     if (args.empty() || args.at(0) == "-h") {
37         dprintf(fd, "\n%-15s  %-20s", "Option", "Description");
38         for (auto &[key, handler] : cmdHandler) {
39             dprintf(fd, "\n%-15s: %-20s\n", handler->GetFormat().c_str(), handler->ShowHelp().c_str());
40         }
41         return false;
42     }
43 
44     auto handler = cmdHandler.find(args.at(0));
45     if (handler != cmdHandler.end()) {
46         std::string output;
47         handler->second->DoAction(args, output);
48         dprintf(fd, "%s\n", output.c_str());
49         return true;
50     }
51     return false;
52 }
53 } // namespace ScreenLock
54 } // namespace OHOS
55