1020a203aSopenharmony_ci/*
2020a203aSopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd.
3020a203aSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4020a203aSopenharmony_ci * you may not use this file except in compliance with the License.
5020a203aSopenharmony_ci * You may obtain a copy of the License at
6020a203aSopenharmony_ci *
7020a203aSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8020a203aSopenharmony_ci *
9020a203aSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10020a203aSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11020a203aSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12020a203aSopenharmony_ci * See the License for the specific language governing permissions and
13020a203aSopenharmony_ci * limitations under the License.
14020a203aSopenharmony_ci */
15020a203aSopenharmony_ci#include <chrono>
16020a203aSopenharmony_ci#include <iostream>
17020a203aSopenharmony_ci#include <string>
18020a203aSopenharmony_ci
19020a203aSopenharmony_ci#include "feature_analysis.h"
20020a203aSopenharmony_ci#include "file_util.h"
21020a203aSopenharmony_ci#include "hiview_logger.h"
22020a203aSopenharmony_ci#include "rule.h"
23020a203aSopenharmony_ci#include "smart_parser.h"
24020a203aSopenharmony_ci#include "tbox.h"
25020a203aSopenharmony_ci
26020a203aSopenharmony_ciDEFINE_LOG_TAG("SmartParser");
27020a203aSopenharmony_ci
28020a203aSopenharmony_cistatic const char* const SMART_PARSER_PATH = "/system/etc/hiview/";
29020a203aSopenharmony_ci
30020a203aSopenharmony_cistatic void PrintEventInfo(const std::map<std::string, std::string>& eventInfo, const std::string& msg)
31020a203aSopenharmony_ci{
32020a203aSopenharmony_ci    HIVIEW_LOGI("%{public}s >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>", msg.c_str());
33020a203aSopenharmony_ci    HIVIEW_LOGI("eventInfo size : %{public}s", std::to_string(eventInfo.size()).c_str());
34020a203aSopenharmony_ci    for (auto &[key, val] : eventInfo) {
35020a203aSopenharmony_ci        std::istringstream iss(val);
36020a203aSopenharmony_ci        std::string line;
37020a203aSopenharmony_ci
38020a203aSopenharmony_ci        while (std::getline(iss, line)) {
39020a203aSopenharmony_ci            HIVIEW_LOGI("[%{public}s] : %{public}s", key.c_str(), line.c_str());
40020a203aSopenharmony_ci        }
41020a203aSopenharmony_ci    }
42020a203aSopenharmony_ci}
43020a203aSopenharmony_ci
44020a203aSopenharmony_cistatic std::map<std::string, std::string> SmartParser(const std::string& eventPath, const std::string& eventType)
45020a203aSopenharmony_ci{
46020a203aSopenharmony_ci    auto startTime = std::chrono::high_resolution_clock::now();
47020a203aSopenharmony_ci    auto eventInfos = OHOS::HiviewDFX::SmartParser::Analysis(eventPath, SMART_PARSER_PATH, eventType);
48020a203aSopenharmony_ci    auto endTime = std::chrono::high_resolution_clock::now();
49020a203aSopenharmony_ci    auto diffTime = std::chrono::duration_cast<std::chrono::milliseconds>(endTime - startTime).count();
50020a203aSopenharmony_ci    std::cout << "SmartParser::Analysis running time : " << diffTime << " ms" << std::endl;
51020a203aSopenharmony_ci
52020a203aSopenharmony_ci    PrintEventInfo(eventInfos, "SmartParser::Analysis result:");
53020a203aSopenharmony_ci
54020a203aSopenharmony_ci    return eventInfos;
55020a203aSopenharmony_ci}
56020a203aSopenharmony_ci
57020a203aSopenharmony_cistatic void Tbox(std::map<std::string, std::string>& eventInfo, std::string& eventType)
58020a203aSopenharmony_ci{
59020a203aSopenharmony_ci    auto startTime = std::chrono::high_resolution_clock::now();
60020a203aSopenharmony_ci    OHOS::HiviewDFX::Tbox::FilterTrace(eventInfo, eventType);
61020a203aSopenharmony_ci    auto endTime = std::chrono::high_resolution_clock::now();
62020a203aSopenharmony_ci    auto diffTime = std::chrono::duration_cast<std::chrono::milliseconds>(endTime - startTime).count();
63020a203aSopenharmony_ci    std::cout << "Tbox::FilterTrace running time : " << diffTime << " ms" << std::endl;
64020a203aSopenharmony_ci
65020a203aSopenharmony_ci    PrintEventInfo(eventInfo, "Tbox::FilterTrace result:");
66020a203aSopenharmony_ci}
67020a203aSopenharmony_ci
68020a203aSopenharmony_ciint main(int argc, char *argv[])
69020a203aSopenharmony_ci{
70020a203aSopenharmony_ci    std::string eventType;
71020a203aSopenharmony_ci    std::string logPath;
72020a203aSopenharmony_ci    for (int i = 1; i < argc; i++) {
73020a203aSopenharmony_ci        if (std::string(argv[i]) == "-t") {
74020a203aSopenharmony_ci            if (i + 1 < argc) {
75020a203aSopenharmony_ci                eventType = argv[i + 1];
76020a203aSopenharmony_ci            }
77020a203aSopenharmony_ci        } else if (std::string(argv[i]) == "-f") {
78020a203aSopenharmony_ci            if (i + 1 < argc) {
79020a203aSopenharmony_ci                logPath = argv[i + 1];
80020a203aSopenharmony_ci            }
81020a203aSopenharmony_ci        }
82020a203aSopenharmony_ci    }
83020a203aSopenharmony_ci
84020a203aSopenharmony_ci    if (eventType.empty() || logPath.empty()) {
85020a203aSopenharmony_ci        std::cout << "Usage:" << std::endl;
86020a203aSopenharmony_ci        std::cout << "\t" << argv[0] << "-t eventType -f filePath" << std::endl;
87020a203aSopenharmony_ci        std::cout << "\teventType\t" <<
88020a203aSopenharmony_ci            "The event name must match the event name configured in the configuration file." << std::endl;
89020a203aSopenharmony_ci        std::cout << "\tfilePath\t" <<
90020a203aSopenharmony_ci            "The parsed file path must match the path matching rule configured in the configuration file." << std::endl;
91020a203aSopenharmony_ci        return -1;
92020a203aSopenharmony_ci    }
93020a203aSopenharmony_ci    std::cout << ">>>>> eventType : " << eventType << std::endl;
94020a203aSopenharmony_ci    std::cout << ">>>>> logPath : " << logPath << std::endl;
95020a203aSopenharmony_ci    std::cout << std::endl;
96020a203aSopenharmony_ci
97020a203aSopenharmony_ci    auto eventInfos = SmartParser(logPath, eventType);
98020a203aSopenharmony_ci    Tbox(eventInfos, eventType);
99020a203aSopenharmony_ci
100020a203aSopenharmony_ci    return 0;
101020a203aSopenharmony_ci}
102