1 /*
2 * Copyright (c) 2023 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 #ifndef DISTRIBUTEDDATAMGR_SERVICE_DUMP_HELPER_H
16 #define DISTRIBUTEDDATAMGR_SERVICE_DUMP_HELPER_H
17 
18 #include <list>
19 #include <mutex>
20 #include <string>
21 #include <vector>
22 #include <set>
23 
24 #include "concurrent_map.h"
25 #include "dump/dump_manager.h"
26 #include "metadata/store_meta_data.h"
27 #include "types.h"
28 #include "visibility.h"
29 
30 namespace OHOS::DistributedData {
31 class DumpHelper {
32     using Handler = std::function<void(int, std::map<std::string, std::vector<std::string>> &)>;
33 
34 public:
35     struct CommandNode {
36         std::string dumpName;
37         std::vector<std::string> param;
38         uint32_t minParamsNum = 0;
39         uint32_t maxParamsNum = 0;
40         std::string parentNode;
41         std::string childNode;
42         std::vector<Handler> handlers;
43         std::shared_ptr<CommandNode> nextNode;
IsVoidCommandNode44         bool IsVoid()
45         {
46             auto config = DumpManager::GetInstance().GetConfig(this->dumpName);
47             return config.IsVoid();
48         }
49     };
50     struct ErrorInfo {
51         int32_t errorCode = 0;
52         std::string errorTime;
53         std::string errorInfo;
54     };
55     API_EXPORT static DumpHelper &GetInstance();
56     API_EXPORT void AddErrorInfo(int32_t errorCode, const std::string &errorInfo);
57     API_EXPORT bool Dump(int fd, const std::vector<std::string> &args);
58 
59 private:
60     DumpHelper();
61     ~DumpHelper() = default;
62     void GetCommandNodes(int fd, std::vector<std::shared_ptr<CommandNode>> &commands);
63     CommandNode GetCommand(const std::string &name);
64     void AddHeadNode(std::shared_ptr<CommandNode> &command, std::shared_ptr<CommandNode> &realHeadNode, bool &isAdded);
65     void AddNode(std::shared_ptr<CommandNode> &command, std::shared_ptr<CommandNode> &realHeadNode, bool &isAdded);
66     void ParseCommand(const std::vector<std::string> &args, std::vector<std::shared_ptr<CommandNode>> &filterInfo);
67     uint32_t GetFormatMaxSize();
68 
69     void RegisterErrorInfo();
70     void DumpErrorInfo(int fd, std::map<std::string, std::vector<std::string>> &params);
71     void RegisterHelpInfo();
72     void DumpHelpInfo(int fd, std::map<std::string, std::vector<std::string>> &params);
73     void RegisterAllInfo();
74     void DumpAllInfo(int fd, std::map<std::string, std::vector<std::string>> &params);
75 
76     std::string FormatHelpInfo(const std::string &cmdAbbr, const std::string &cmd, const std::string &paraExt,
77         const std::string &info, uint32_t &formatMaxSize);
78     mutable std::mutex hidumperMutex_;
79     static constexpr int32_t MAX_FILTER_COUNT = 3;
80     static constexpr int32_t MAX_RECORED_ERROR = 10;
81     static constexpr int32_t DUMP_SYSTEM_START_YEAR = 1900;
82     static constexpr int32_t FORMAT_BLANK_SIZE = 32;
83     static constexpr int32_t FORMAT_FILL_SIZE = 6;
84     static constexpr char FORMAT_BLANK_SPACE = ' ';
85     static constexpr int32_t DUMP_COMMAND_PREFIX_SIZE = 1;
86     static constexpr const char *DUMP_COMMAND_PREFIX = "-";
87     static constexpr const char *INDENTATION = "    ";
88     static constexpr const char *SEPARATOR = "_";
89     static constexpr int32_t PRINTF_COUNT_2 = 2;
90 
91     std::list<ErrorInfo> errorInfo_;
92 };
93 } // namespace OHOS::DistributedData
94 #endif // DISTRIBUTEDDATAMGR_SERVICE_DUMP_HELPER_H
95