1 /* 2 * Copyright (c) 2021-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 16 #ifndef HIVIEW_PLUGINS_EVENT_SERVICE_INCLUDE_EVENT_JSON_PARSER_H 17 #define HIVIEW_PLUGINS_EVENT_SERVICE_INCLUDE_EVENT_JSON_PARSER_H 18 19 #include <cstdint> 20 #include <functional> 21 #include <list> 22 #include <string> 23 #include <unordered_map> 24 #include <vector> 25 26 #include "json/json.h" 27 #include "sys_event.h" 28 29 namespace OHOS { 30 namespace HiviewDFX { 31 struct BaseInfo { 32 uint8_t type; 33 std::string level; 34 std::string tag; 35 bool preserve = true; 36 }; 37 using NAME_INFO_MAP = std::unordered_map<std::string, BaseInfo>; 38 using DOMAIN_INFO_MAP = std::unordered_map<std::string, NAME_INFO_MAP>; 39 using JSON_VALUE_LOOP_HANDLER = std::function<void(const std::string&, const Json::Value&)>; 40 using FILTER_SIZE_TYPE = std::list<std::string>::size_type; 41 42 class DuplicateIdFilter { 43 public: 44 bool IsDuplicateEvent(const uint64_t sysEventId); 45 46 private: 47 std::list<uint64_t> sysEventIds; 48 }; 49 50 class EventJsonParser { 51 public: 52 EventJsonParser(std::vector<std::string>& paths); ~EventJsonParser()53 ~EventJsonParser() {}; 54 55 public: 56 std::string GetTagByDomainAndName(const std::string& domain, const std::string& name) const; 57 int GetTypeByDomainAndName(const std::string& domain, const std::string& name) const; 58 bool GetPreserveByDomainAndName(const std::string& domain, const std::string& name) const; 59 bool HandleEventJson(const std::shared_ptr<SysEvent>& event); 60 61 private: 62 void AppendExtensiveInfo(std::shared_ptr<SysEvent> event, uint64_t sysEventId) const; 63 bool CheckBaseInfoValidity(const BaseInfo& baseInfo, std::shared_ptr<SysEvent> event) const; 64 bool CheckEventValidity(std::shared_ptr<SysEvent> event) const; 65 bool CheckTypeValidity(const BaseInfo& baseInfo, std::shared_ptr<SysEvent> event) const; 66 BaseInfo GetDefinedBaseInfoByDomainName(const std::string& domain, const std::string& name) const; 67 bool HasIntMember(const Json::Value& jsonObj, const std::string& name) const; 68 bool HasStringMember(const Json::Value& jsonObj, const std::string& name) const; 69 bool HasBoolMember(const Json::Value& jsonObj, const std::string& name) const; 70 void InitEventInfoMapRef(const Json::Value& jsonObj, JSON_VALUE_LOOP_HANDLER handler) const; 71 BaseInfo ParseBaseConfig(const Json::Value& eventNameJson) const; 72 void ParseHiSysEventDef(const Json::Value& hiSysEventDef); 73 NAME_INFO_MAP ParseNameConfig(const Json::Value& domainJson) const; 74 std::string GetSequenceFile() const; 75 void ReadSeqFromFile(int64_t& seq); 76 void WriteSeqToFile(int64_t seq) const; 77 void WatchParameterAndReadLatestSeq(); 78 79 private: 80 DOMAIN_INFO_MAP hiSysEventDef_; 81 DuplicateIdFilter filter_; 82 int64_t curSeq_ = 0; 83 }; // EventJsonParser 84 } // namespace HiviewDFX 85 } // namespace OHOS 86 #endif 87