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 "dump_helper.h"
17 #include "kvstore_data_service.h"
18 #include "log_print.h"
19
20 namespace OHOS {
21 namespace DistributedKv {
22 namespace {
23 constexpr int32_t MAX_RECORED_ERROR = 10;
24 constexpr int32_t SUB_CMD_NAME = 0;
25 constexpr int32_t SUB_CMD_PARAM = 1;
26 constexpr int32_t CMD_NO_PARAM = 1;
27 constexpr int32_t CMD_HAS_PARAM = 2;
28 constexpr const char *CMD_HELP = "-h";
29 constexpr const char *CMD_USER_INFO = "-userInfo";
30 constexpr const char *CMD_APP_INFO = "-appInfo";
31 constexpr const char *CMD_STORE_INFO = "-storeInfo";
32 constexpr const char *CMD_ERROR_INFO = "-errorInfo";
33 constexpr const char *ILLEGAL_INFOMATION = "The arguments are illegal and you can enter '-h' for help.\n";
34 }
35
AddDumpOperation(const DumpNoParamFunc & dumpAll,const DumpNoParamFunc & dumpUserInfo,const DumpWithParamFunc & dumpAppInfo,const DumpWithParamFunc & dumpStoreInfo)36 void DumpHelper::AddDumpOperation(const DumpNoParamFunc &dumpAll, const DumpNoParamFunc &dumpUserInfo,
37 const DumpWithParamFunc &dumpAppInfo, const DumpWithParamFunc &dumpStoreInfo)
38 {
39 if (dumpAll == nullptr || dumpUserInfo == nullptr || dumpAppInfo == nullptr || dumpStoreInfo == nullptr) {
40 return;
41 }
42 dumpAll_ = dumpAll;
43 dumpUserInfo_ = dumpUserInfo;
44 dumpAppInfo_ = dumpAppInfo;
45 dumpStoreInfo_ = dumpStoreInfo;
46 }
47
AddErrorInfo(const std::string & error)48 void DumpHelper::AddErrorInfo(const std::string &error)
49 {
50 std::lock_guard<std::mutex> lock(hidumperMutex_);
51 if (g_errorInfo.size() + 1 > MAX_RECORED_ERROR) {
52 g_errorInfo.pop_front();
53 g_errorInfo.push_back(error);
54 } else {
55 g_errorInfo.push_back(error);
56 }
57 }
58
ShowError(int fd)59 void DumpHelper::ShowError(int fd)
60 {
61 dprintf(fd, "The number of recent errors recorded is %zu\n", g_errorInfo.size());
62 int i = 0;
63 for (const auto &it : g_errorInfo) {
64 dprintf(fd, "Error ID: %d ErrorInfo: %s\n", ++i, it.c_str());
65 }
66 }
67
Dump(int fd,const std::vector<std::string> & args)68 bool DumpHelper::Dump(int fd, const std::vector<std::string> &args)
69 {
70 std::string command = "";
71 std::string param = "";
72
73 if (args.size() == CMD_NO_PARAM) {
74 command = args.at(SUB_CMD_NAME);
75 } else if (args.size() == CMD_HAS_PARAM) {
76 command = args.at(SUB_CMD_NAME);
77 param = args.at(SUB_CMD_PARAM);
78 } else {
79 ShowError(fd);
80 if (!dumpAll_) {
81 return false;
82 }
83 dumpAll_(fd);
84 }
85
86 if (command == CMD_HELP) {
87 ShowHelp(fd);
88 } else if (command == CMD_ERROR_INFO) {
89 ShowError(fd);
90 } else if (command == CMD_USER_INFO) {
91 if (!dumpUserInfo_) {
92 return false;
93 }
94 dumpUserInfo_(fd);
95 } else if (command == CMD_APP_INFO) {
96 if (!dumpAppInfo_) {
97 return false;
98 }
99 dumpAppInfo_(fd, param);
100 } else if (command == CMD_STORE_INFO) {
101 if (!dumpStoreInfo_) {
102 return false;
103 }
104 dumpStoreInfo_(fd, param);
105 } else {
106 ShowIllealInfomation(fd);
107 }
108 return true;
109 }
110
ShowHelp(int fd)111 void DumpHelper::ShowHelp(int fd)
112 {
113 std::string result;
114 result.append("Usage:dump <command> [options]\n")
115 .append("Description:\n")
116 .append(CMD_USER_INFO)
117 .append(" ")
118 .append("dump all user information in the system\n")
119 .append(CMD_APP_INFO)
120 .append(" ")
121 .append("dump list of all app information in the system\n")
122 .append(CMD_APP_INFO)
123 .append(" [appID] ")
124 .append("dump information about the specified app in the system\n")
125 .append(CMD_STORE_INFO)
126 .append(" ")
127 .append("dump list of all store information in the system\n")
128 .append(CMD_STORE_INFO)
129 .append(" [storeID] ")
130 .append("dump information about the specified store in the system\n")
131 .append(CMD_ERROR_INFO)
132 .append(" ")
133 .append("dump the recent errors information in the system\n");
134 dprintf(fd, "%s\n", result.c_str());
135 }
136
ShowIllealInfomation(int fd)137 void DumpHelper::ShowIllealInfomation(int fd)
138 {
139 dprintf(fd, "%s\n", ILLEGAL_INFOMATION);
140 }
141 } // namespace DistributedKv
142 } // namespace OHOS
143
144