1 /* 2 * Copyright (c) 2021-2025 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_BASE_SYS_EVENT_H 17 #define HIVIEW_BASE_SYS_EVENT_H 18 19 #include <atomic> 20 #include <iomanip> 21 #include <memory> 22 #include <sstream> 23 #include <string> 24 #include <type_traits> 25 #include <vector> 26 27 #include "encoded/encoded_param.h" 28 #include "decoded/decoded_event.h" 29 #include "pipeline.h" 30 #include "encoded/raw_data_builder.h" 31 #include "base/raw_data.h" 32 33 namespace OHOS { 34 namespace HiviewDFX { 35 namespace EventStore { 36 class EventCol { 37 public: 38 static std::string DOMAIN; 39 static std::string NAME; 40 static std::string TYPE; 41 static std::string TS; 42 static std::string TZ; 43 static std::string PID; 44 static std::string TID; 45 static std::string UID; 46 static std::string INFO; 47 static std::string LEVEL; 48 static std::string SEQ; 49 static std::string TAG; 50 }; 51 } 52 53 struct EventParamInfo { 54 std::string allowListFile; 55 uint8_t throwType = 0; EventParamInfoEventParamInfo56 EventParamInfo(const std::string& allowListFile, uint8_t throwType) 57 : allowListFile(allowListFile), throwType(throwType) {} 58 }; 59 60 struct EventPeriodSeqInfo { 61 // formatted time stamp: YYYYMMDDHH 62 std::string timeStamp; 63 64 // tag for event export: 65 bool isNeedExport = false; 66 67 // the sequence of event in current time range 68 uint64_t periodSeq = 0; 69 }; 70 71 // param info of one event, like <paramName, EventParamInfo> 72 using PARAM_INFO_MAP_PTR = std::shared_ptr<std::map<std::string, std::shared_ptr<EventParamInfo>>>; 73 74 constexpr uint8_t LOG_ALLOW_PACK = 0 << 5; 75 constexpr uint8_t LOG_NOT_ALLOW_PACK = 1 << 5; 76 constexpr uint8_t LOG_PACKED = 1; 77 constexpr uint8_t LOG_REPEAT = 1; 78 constexpr uint8_t LOG_THRESHOLD = 2; 79 80 constexpr int16_t UNINIT_REPORT_INTERVAL = -1; 81 82 class SysEventCreator; 83 class SysEvent : public PipelineEvent { 84 public: 85 SysEvent(const std::string& sender, PipelineEventProducer* handler, 86 std::shared_ptr<EventRaw::RawData> rawData, int64_t seq, 87 const std::string& sysVersion, const std::string& patchVersion); 88 SysEvent(const std::string& sender, PipelineEventProducer* handler, 89 std::shared_ptr<EventRaw::RawData> rawData, int64_t seq); 90 SysEvent(const std::string& sender, PipelineEventProducer* handler, std::shared_ptr<EventRaw::RawData> rawData); 91 SysEvent(const std::string& sender, PipelineEventProducer* handler, SysEventCreator& sysEventCreator); 92 SysEvent(const std::string& sender, PipelineEventProducer* handler, const std::string& jsonStr); 93 ~SysEvent(); 94 95 public: 96 void SetTag(const std::string& tag); 97 std::string GetTag() const; 98 void SetLevel(const std::string& level); 99 std::string GetLevel() const; 100 int32_t GetPid() const; 101 int32_t GetTid() const; 102 int32_t GetUid() const; 103 int16_t GetTz() const; 104 void SetSeq(int64_t seq); 105 int64_t GetSeq() const; 106 void SetEventSeq(int64_t eventSeq); 107 int64_t GetEventSeq() const; 108 int GetEventType() const; 109 void SetId(uint64_t id); 110 void SetLog(uint8_t log); 111 void SetPrivacy(uint8_t privacy); 112 uint8_t GetPrivacy() const; 113 114 std::string GetEventValue(const std::string& key); 115 int64_t GetEventIntValue(const std::string& key); 116 uint64_t GetEventUintValue(const std::string& key); 117 double GetEventDoubleValue(const std::string& key); 118 bool GetEventIntArrayValue(const std::string& key, std::vector<int64_t>& dest); 119 bool GetEventUintArrayValue(const std::string& key, std::vector<uint64_t>& dest); 120 bool GetEventDoubleArrayValue(const std::string& key, std::vector<double>& dest); 121 bool GetEventStringArrayValue(const std::string& key, std::vector<std::string>& dest); 122 bool IsParamExist(const std::string& paramName); 123 bool RemoveParam(const std::string& paramName); 124 void SetInvalidParams(PARAM_INFO_MAP_PTR invalidParams); 125 PARAM_INFO_MAP_PTR GetInvalidParams(); 126 std::string AsJsonStr(); 127 uint8_t* AsRawData(); 128 std::string GetSysVersion(); 129 std::string GetPatchVersion(); 130 void SetEventPeriodSeqInfo(const EventPeriodSeqInfo& info); 131 EventPeriodSeqInfo GetEventPeriodSeqInfo(); 132 void SetReportInterval(int16_t reportInterval); 133 int16_t GetReportInterval(); 134 135 public: 136 template<typename T> 137 void SetEventValue(const std::string& key, T value, bool appendValue = false) 138 { 139 if (!InitBuilder()) { 140 return; 141 } 142 if constexpr (std::is_same_v<std::decay_t<T>, std::string>) { 143 auto param = builder_->GetValue(key); 144 std::string paramValue; 145 if (appendValue && (param != nullptr) && param->AsString(paramValue)) { 146 paramValue = UnescapeJsonStringValue(paramValue); 147 paramValue.append(value); 148 value = paramValue; 149 } 150 value = EscapeJsonStringValue(value); 151 } 152 builder_->AppendValue(key, value); 153 isUpdated_ = true; 154 } 155 156 public: 157 int32_t eventType_; 158 bool preserve_; 159 uint8_t log_; 160 bool collect_ = false; 161 162 public: 163 static std::atomic<uint32_t> totalCount_; 164 static std::atomic<int64_t> totalSize_; 165 166 private: 167 void InitialMembers(); 168 bool InitBuilder(); 169 bool TryToUpdateRawData(); 170 std::shared_ptr<EventRaw::RawData> TansJsonStrToRawData(const std::string& jsonStr); 171 std::string EscapeJsonStringValue(const std::string& src); 172 std::string UnescapeJsonStringValue(const std::string& src); 173 174 private: 175 bool isUpdated_ = false; 176 int64_t seq_ = 0; 177 int32_t pid_ = 0; 178 int32_t tid_ = 0; 179 int32_t uid_ = 0; 180 int16_t tz_ = 0; 181 int64_t eventSeq_ = -1; 182 uint8_t privacy_ = 0; 183 std::string tag_; 184 std::string level_; 185 std::shared_ptr<EventRaw::RawDataBuilder> builder_; 186 std::string sysVersion_; 187 std::string patchVersion_; 188 PARAM_INFO_MAP_PTR invalidParams_; 189 int16_t reportInterval_ = UNINIT_REPORT_INTERVAL; 190 }; 191 192 class SysEventCreator { 193 public: 194 enum EventType { 195 FAULT = 1, // system fault event 196 STATISTIC = 2, // system statistic event 197 SECURITY = 3, // system security event 198 BEHAVIOR = 4 // system behavior event 199 }; 200 201 public: 202 SysEventCreator(const std::string &domain, const std::string &eventName, EventType type); 203 204 public: 205 template<typename T> SetKeyValue(const std::string & key,T value)206 void SetKeyValue(const std::string& key, T value) 207 { 208 if (builder_ == nullptr) { 209 return; 210 } 211 if constexpr (std::is_same_v<std::decay_t<T>, std::vector<std::string>>) { 212 std::vector<std::string> transVal; 213 for (auto item : value) { 214 transVal.emplace_back(EscapeJsonStringValue(item)); 215 } 216 builder_->AppendValue(key, transVal); 217 return; 218 } 219 if constexpr (std::is_same_v<std::decay_t<T>, std::string>) { 220 value = EscapeJsonStringValue(value); 221 } 222 builder_->AppendValue(key, value); 223 } 224 225 public: 226 std::shared_ptr<EventRaw::RawData> GetRawData(); 227 228 private: 229 std::string EscapeJsonStringValue(const std::string& src); 230 231 private: 232 std::shared_ptr<EventRaw::RawDataBuilder> builder_; 233 }; 234 } // namespace HiviewDFX 235 } // namespace OHOS 236 #endif // HIVIEW_BASE_SYS_EVENT_H 237