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
26 namespace OHOS {
27 namespace MiscServices {
28 constexpr int32_t MAX_RECORD_ERROR = 10;
29 constexpr int32_t SUB_CMD_NAME = 0;
30 constexpr int32_t CMD_ONE_PARAM = 1;
31 constexpr const char *CMD_HELP = "-h";
32 constexpr const char *CMD_ALL_DUMP = "-a";
33 static const std::string ILLEGAL_INFO = "input dump parameter error,enter '-h' for usage.\n";
34
AddDumpAllMethod(const DumpNoParamFunc dumpAllMethod)35 void InputmethodDump::AddDumpAllMethod(const DumpNoParamFunc dumpAllMethod)
36 {
37 if (dumpAllMethod == nullptr) {
38 return;
39 }
40 dumpAllMethod_ = dumpAllMethod;
41 }
42
AddErrorInfo(const std::string & error)43 void InputmethodDump::AddErrorInfo(const std::string &error)
44 {
45 std::lock_guard<std::mutex> lock(hidumperMutex_);
46 if (errorInfo_.size() + 1 > MAX_RECORD_ERROR) {
47 errorInfo_.pop_front();
48 errorInfo_.push_back(error);
49 } else {
50 errorInfo_.push_back(error);
51 }
52 }
53
Dump(int fd,const std::vector<std::string> & args)54 bool InputmethodDump::Dump(int fd, const std::vector<std::string> &args)
55 {
56 IMSA_HILOGI("InputmethodDump::Dump start.");
57 std::string command = "";
58 if (args.size() == CMD_ONE_PARAM) {
59 command = args.at(SUB_CMD_NAME);
60 } else {
61 ShowIllealInformation(fd);
62 }
63 if (command == CMD_HELP) {
64 ShowHelp(fd);
65 } else if (command == CMD_ALL_DUMP) {
66 if (!dumpAllMethod_) {
67 return false;
68 }
69 dumpAllMethod_(fd);
70 } else {
71 ShowIllealInformation(fd);
72 }
73 IMSA_HILOGI("InputmethodDump::Dump command=%{public}s.", command.c_str());
74 return true;
75 }
76
ShowHelp(int fd)77 void InputmethodDump::ShowHelp(int fd)
78 {
79 std::string result;
80 result.append("Usage:dump <command> [options]\n")
81 .append("Description:\n")
82 .append("-h show help\n")
83 .append("-a dump all input methods\n");
84 dprintf(fd, "%s\n", result.c_str());
85 }
86
ShowIllealInformation(int fd)87 void InputmethodDump::ShowIllealInformation(int fd)
88 {
89 dprintf(fd, "%s\n", ILLEGAL_INFO.c_str());
90 }
91 } // namespace MiscServices
92 } // namespace OHOS