1 /*
2  * Copyright (c) 2021 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 FEATURE_ANALYSIS_H
16 #define FEATURE_ANALYSIS_H
17 
18 #include <ctime>
19 #include <list>
20 #include <map>
21 #include <memory>
22 #include <string>
23 #include <unordered_map>
24 #include <vector>
25 
26 #include "syntax_rules.h"
27 namespace OHOS {
28 namespace HiviewDFX {
29 enum SeekType {
30     FIRST_MATCH = 0,
31     LAST_MATCH,
32 };
33 
34 class FeatureAnalysis {
35     enum ErrorCode {
36         DEFAULT = -1,
37         SUCCESS = 0,
38         BUFFER_ERROR,
39         EXTRACT_ERROR,
40         COMPOSE_ERROR
41     };
42 public:
FeatureAnalysis(FeatureSet featureSet,std::map<std::string,std::string> composeRule,const std::string & eventType)43     FeatureAnalysis(FeatureSet featureSet, std::map<std::string, std::string> composeRule,
44         const std::string& eventType)
45         : eventType_(eventType), lineCursor_(0), errorCode_(DEFAULT), line_(""),
46           featureSet_(featureSet), composeRule_(composeRule)
47     {
48         time_t epochTime = time(nullptr);
49         if (epochTime > 0) {
50             taskId_ = epochTime;
51         } else {
52             taskId_ = 0;
53         }
54     };
55     ~FeatureAnalysis();
56     FeatureAnalysis(const FeatureAnalysis&) = delete;
57     FeatureAnalysis& operator=(const FeatureAnalysis&) = delete;
58 
59     // interface
60     bool AnalysisLog();
61     void RawInfoPosition(std::stringstream& buffer);
62     bool CheckStartSegment(bool& segmentStart) const;
GetErrorCode()63     int GetErrorCode() const { return errorCode_; };
GetReasult()64     std::map<std::string, std::string> GetReasult() const { return eventInfo_; };
GetParamSeekRecord()65     std::vector<std::pair<std::string, LineFeature>> GetParamSeekRecord() {return paramSeekRecord_;};
66 
67 private:
68     void Extract();
69     bool IsSourceMatch(const std::string& line, const FeatureRule& rule) const;
70     bool ParseElementForParam(const std::string& src, FeatureRule& rule);
71     int GetSeekInfo(const std::string& param, std::string& value) const;
72     bool CheckVariableParam(FeatureRule& rule) const;
73     bool CheckVariable(const FeatureRule& rule, const std::string& leftTag, const std::string& rightTag) const;
74     void ReplaceVariable(FeatureRule& rule, const std::string& symbol, const std::string& value) const;
75     bool ReplaceVariable(const std::string& src, const std::string& param, const std::string& value,
76         std::string& des) const;
77     bool CheckDepend(const FeatureRule& rule) const;
78     void GetCursorInfo(std::stringstream& buff, const std::string& line);
79     LineFeature FormatLineFeature(const std::string& value, const std::string& regex) const;
80     void Compose();
81     std::string ComposeTrace(const std::string& filePath, const std::string& param,
82         const std::vector<std::pair<std::string, LineFeature>>& lineFeatures, const std::string& regex) const;
83     std::string ComposeParam(const std::string& param) const;
84     std::vector<std::string> SplitParam(const std::string& param) const;
85     void ProcessReason(std::map<std::string, std::string>& info);
86     bool IsMatchOrExpression(const std::string& line, const std::string& src) const;
87     bool IsMatchAndExpression(const std::string& line, const std::string& src) const;
88     void SetStackRegex(const std::string& key, const std::string& regex);
89     void SetParamRecord(const std::string& key, const LineFeature& value, const int type);
90 
91 private:
92     int taskId_;
93     std::string eventType_;
94     int lineCursor_;
95     int errorCode_;
96     const int MAX_SKIP_LINE = 32000; // 32000 max skip line
97     static const std::string COMPOSE_PLUS;
98     static const std::string COMPOSE_COLON;
99     std::string line_;
100     std::vector<std::string> deletePath_;
101     FeatureSet featureSet_;
102     std::map<std::string, std::string> stackRegex_;
103     std::vector<std::pair<std::string, LineFeature>> paramSeekRecord_;
104     std::map<std::string, std::string> composeRule_;
105     std::map<std::string, std::string> eventInfo_;
106 };
107 } // namespace HiviewDFX
108 } // namespace OHOS
109 #endif /* FEATURE_ANALYSIS_H */
110