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) : taskId_(time(nullptr)), eventType_(eventType), lineCursor_(0), 45 errorCode_(DEFAULT), line_(""), featureSet_(featureSet), composeRule_(composeRule) {}; 46 ~FeatureAnalysis(); 47 FeatureAnalysis(const FeatureAnalysis&) = delete; 48 FeatureAnalysis& operator=(const FeatureAnalysis&) = delete; 49 50 // interface 51 bool AnalysisLog(); 52 void RawInfoPosition(std::stringstream& buffer); 53 bool CheckStartSegment(bool& segmentStart) const; GetErrorCode()54 int GetErrorCode() const { return errorCode_; }; GetReasult()55 std::map<std::string, std::string> GetReasult() const { return eventInfo_; }; GetParamSeekRecord()56 std::vector<std::pair<std::string, LineFeature>> GetParamSeekRecord() {return paramSeekRecord_;}; 57 58 private: 59 void Extract(); 60 bool IsSourceMatch(const std::string& line, const FeatureRule& rule) const; 61 bool ParseElementForParam(const std::string& src, FeatureRule& rule); 62 int GetSeekInfo(const std::string& param, std::string& value) const; 63 bool CheckVariableParam(FeatureRule& rule) const; 64 bool CheckVariable(const FeatureRule& rule, const std::string& leftTag, const std::string& rightTag) const; 65 void ReplaceVariable(FeatureRule& rule, const std::string& symbol, const std::string& value) const; 66 bool ReplaceVariable(const std::string& src, const std::string& param, const std::string& value, 67 std::string& des) const; 68 bool CheckDepend(const FeatureRule& rule) const; 69 void GetCursorInfo(std::stringstream& buff, const std::string& line); 70 LineFeature FormatLineFeature(const std::string& value, const std::string& regex) const; 71 void Compose(); 72 std::string ComposeTrace(const std::string& filePath, const std::string& param, 73 const std::vector<std::pair<std::string, LineFeature>>& lineFeatures, const std::string& regex) const; 74 std::string ComposeParam(const std::string& param) const; 75 std::vector<std::string> SplitParam(const std::string& param) const; 76 void ProcessReason(std::map<std::string, std::string>& info); 77 bool IsMatchOrExpression(const std::string& line, const std::string& src) const; 78 bool IsMatchAndExpression(const std::string& line, const std::string& src) const; 79 void SetStackRegex(const std::string& key, const std::string& regex); 80 void SetParamRecord(const std::string& key, const LineFeature& value, const int type); 81 82 private: 83 int taskId_; 84 std::string eventType_; 85 int lineCursor_; 86 int errorCode_; 87 const int MAX_SKIP_LINE = 32000; // 32000 max skip line 88 static const std::string COMPOSE_PLUS; 89 static const std::string COMPOSE_COLON; 90 std::string line_; 91 std::vector<std::string> deletePath_; 92 FeatureSet featureSet_; 93 std::map<std::string, std::string> stackRegex_; 94 std::vector<std::pair<std::string, LineFeature>> paramSeekRecord_; 95 std::map<std::string, std::string> composeRule_; 96 std::map<std::string, std::string> eventInfo_; 97 }; 98 } // namespace HiviewDFX 99 } // namespace OHOS 100 #endif /* FEATURE_ANALYSIS_H */