1 /*
2 * Copyright (c) 2024 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 #include <chrono>
16 #include <iostream>
17 #include <string>
18
19 #include "feature_analysis.h"
20 #include "file_util.h"
21 #include "hiview_logger.h"
22 #include "rule.h"
23 #include "smart_parser.h"
24 #include "tbox.h"
25
26 DEFINE_LOG_TAG("SmartParser");
27
28 static const char* const SMART_PARSER_PATH = "/system/etc/hiview/";
29
PrintEventInfo(const std::map<std::string, std::string>& eventInfo, const std::string& msg)30 static void PrintEventInfo(const std::map<std::string, std::string>& eventInfo, const std::string& msg)
31 {
32 HIVIEW_LOGI("%{public}s >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>", msg.c_str());
33 HIVIEW_LOGI("eventInfo size : %{public}s", std::to_string(eventInfo.size()).c_str());
34 for (auto &[key, val] : eventInfo) {
35 std::istringstream iss(val);
36 std::string line;
37
38 while (std::getline(iss, line)) {
39 HIVIEW_LOGI("[%{public}s] : %{public}s", key.c_str(), line.c_str());
40 }
41 }
42 }
43
SmartParser(const std::string& eventPath, const std::string& eventType)44 static std::map<std::string, std::string> SmartParser(const std::string& eventPath, const std::string& eventType)
45 {
46 auto startTime = std::chrono::high_resolution_clock::now();
47 auto eventInfos = OHOS::HiviewDFX::SmartParser::Analysis(eventPath, SMART_PARSER_PATH, eventType);
48 auto endTime = std::chrono::high_resolution_clock::now();
49 auto diffTime = std::chrono::duration_cast<std::chrono::milliseconds>(endTime - startTime).count();
50 std::cout << "SmartParser::Analysis running time : " << diffTime << " ms" << std::endl;
51
52 PrintEventInfo(eventInfos, "SmartParser::Analysis result:");
53
54 return eventInfos;
55 }
56
Tbox(std::map<std::string, std::string>& eventInfo, std::string& eventType)57 static void Tbox(std::map<std::string, std::string>& eventInfo, std::string& eventType)
58 {
59 auto startTime = std::chrono::high_resolution_clock::now();
60 OHOS::HiviewDFX::Tbox::FilterTrace(eventInfo, eventType);
61 auto endTime = std::chrono::high_resolution_clock::now();
62 auto diffTime = std::chrono::duration_cast<std::chrono::milliseconds>(endTime - startTime).count();
63 std::cout << "Tbox::FilterTrace running time : " << diffTime << " ms" << std::endl;
64
65 PrintEventInfo(eventInfo, "Tbox::FilterTrace result:");
66 }
67
main(int argc, char *argv[])68 int main(int argc, char *argv[])
69 {
70 std::string eventType;
71 std::string logPath;
72 for (int i = 1; i < argc; i++) {
73 if (std::string(argv[i]) == "-t") {
74 if (i + 1 < argc) {
75 eventType = argv[i + 1];
76 }
77 } else if (std::string(argv[i]) == "-f") {
78 if (i + 1 < argc) {
79 logPath = argv[i + 1];
80 }
81 }
82 }
83
84 if (eventType.empty() || logPath.empty()) {
85 std::cout << "Usage:" << std::endl;
86 std::cout << "\t" << argv[0] << "-t eventType -f filePath" << std::endl;
87 std::cout << "\teventType\t" <<
88 "The event name must match the event name configured in the configuration file." << std::endl;
89 std::cout << "\tfilePath\t" <<
90 "The parsed file path must match the path matching rule configured in the configuration file." << std::endl;
91 return -1;
92 }
93 std::cout << ">>>>> eventType : " << eventType << std::endl;
94 std::cout << ">>>>> logPath : " << logPath << std::endl;
95 std::cout << std::endl;
96
97 auto eventInfos = SmartParser(logPath, eventType);
98 Tbox(eventInfos, eventType);
99
100 return 0;
101 }
102