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
16#include "inputmethod_dump.h"
17
18#include <cstdint>
19#include <functional>
20#include <list>
21#include <string>
22#include <vector>
23
24#include "global.h"
25
26namespace OHOS {
27namespace MiscServices {
28constexpr int32_t SUB_CMD_NAME = 0;
29constexpr int32_t CMD_ONE_PARAM = 1;
30constexpr const char *CMD_HELP = "-h";
31constexpr const char *CMD_ALL_DUMP = "-a";
32static const std::string ILLEGAL_INFO = "input dump parameter error,enter '-h' for usage.\n";
33
34void InputmethodDump::AddDumpAllMethod(const DumpNoParamFunc dumpAllMethod)
35{
36    if (dumpAllMethod == nullptr) {
37        return;
38    }
39    dumpAllMethod_ = dumpAllMethod;
40}
41
42bool InputmethodDump::Dump(int fd, const std::vector<std::string> &args)
43{
44    IMSA_HILOGI("InputmethodDump::Dump start.");
45    std::string command = "";
46    if (args.size() == CMD_ONE_PARAM) {
47        command = args.at(SUB_CMD_NAME);
48    } else {
49        ShowIllegalInformation(fd);
50    }
51    if (command == CMD_HELP) {
52        ShowHelp(fd);
53    } else if (command == CMD_ALL_DUMP) {
54        if (dumpAllMethod_ == nullptr) {
55            return false;
56        }
57        dumpAllMethod_(fd);
58    } else {
59        ShowIllegalInformation(fd);
60    }
61    IMSA_HILOGI("InputmethodDump::Dump command=%{public}s.", command.c_str());
62    return true;
63}
64
65void InputmethodDump::ShowHelp(int fd)
66{
67    std::string result;
68    result.append("Usage:dump  <command> [options]\n")
69        .append("Description:\n")
70        .append("-h show help\n")
71        .append("-a dump all input methods\n");
72    dprintf(fd, "%s\n", result.c_str());
73}
74
75void InputmethodDump::ShowIllegalInformation(int fd)
76{
77    dprintf(fd, "%s\n", ILLEGAL_INFO.c_str());
78}
79} // namespace MiscServices
80} // namespace OHOS