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