1 /* 2 * Copyright (c) 2021-2024 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 constexpr uint8_t LOG_ALLOW_PACK = 0 << 5; 54 constexpr uint8_t LOG_NOT_ALLOW_PACK = 1 << 5; 55 constexpr uint8_t LOG_PACKED = 1; 56 constexpr uint8_t LOG_REPEAT = 1; 57 constexpr uint8_t LOG_THRESHOLD = 2; 58 59 class SysEventCreator; 60 class SysEvent : public PipelineEvent { 61 public: 62 SysEvent(const std::string& sender, PipelineEventProducer* handler, 63 std::shared_ptr<EventRaw::RawData> rawData, int64_t seq, const std::string& sysVersion); 64 SysEvent(const std::string& sender, PipelineEventProducer* handler, 65 std::shared_ptr<EventRaw::RawData> rawData, int64_t seq); 66 SysEvent(const std::string& sender, PipelineEventProducer* handler, std::shared_ptr<EventRaw::RawData> rawData); 67 SysEvent(const std::string& sender, PipelineEventProducer* handler, SysEventCreator& sysEventCreator); 68 SysEvent(const std::string& sender, PipelineEventProducer* handler, const std::string& jsonStr); 69 ~SysEvent(); 70 71 public: 72 void SetTag(const std::string& tag); 73 std::string GetTag() const; 74 void SetLevel(const std::string& level); 75 std::string GetLevel() const; 76 int32_t GetPid() const; 77 int32_t GetTid() const; 78 int32_t GetUid() const; 79 int16_t GetTz() const; 80 void SetSeq(int64_t seq); 81 int64_t GetSeq() const; 82 void SetEventSeq(int64_t eventSeq); 83 int64_t GetEventSeq() const; 84 int GetEventType() const; 85 void SetId(uint64_t id); 86 void SetLog(uint8_t log); 87 void SetPrivacy(uint8_t privacy); 88 uint8_t GetPrivacy() const; 89 90 std::string GetEventValue(const std::string& key); 91 int64_t GetEventIntValue(const std::string& key); 92 uint64_t GetEventUintValue(const std::string& key); 93 double GetEventDoubleValue(const std::string& key); 94 bool GetEventIntArrayValue(const std::string& key, std::vector<int64_t>& dest); 95 bool GetEventUintArrayValue(const std::string& key, std::vector<uint64_t>& dest); 96 bool GetEventDoubleArrayValue(const std::string& key, std::vector<double>& dest); 97 bool GetEventStringArrayValue(const std::string& key, std::vector<std::string>& dest); 98 std::string AsJsonStr(); 99 uint8_t* AsRawData(); 100 std::string GetSysVersion(); 101 102 public: 103 template<typename T> 104 void SetEventValue(const std::string& key, T value, bool appendValue = false) 105 { 106 if (!InitBuilder()) { 107 return; 108 } 109 if constexpr (std::is_same_v<std::decay_t<T>, std::string>) { 110 auto param = builder_->GetValue(key); 111 std::string paramValue; 112 if (appendValue && (param != nullptr) && param->AsString(paramValue)) { 113 paramValue = UnescapeJsonStringValue(paramValue); 114 paramValue.append(value); 115 value = paramValue; 116 } 117 value = EscapeJsonStringValue(value); 118 } 119 builder_->AppendValue(key, value); 120 isUpdated_ = true; 121 } 122 123 public: 124 int32_t eventType_; 125 bool preserve_; 126 uint8_t log_; 127 128 public: 129 static std::atomic<uint32_t> totalCount_; 130 static std::atomic<int64_t> totalSize_; 131 132 private: 133 void InitialMembers(); 134 bool InitBuilder(); 135 bool TryToUpdateRawData(); 136 std::shared_ptr<EventRaw::RawData> TansJsonStrToRawData(const std::string& jsonStr); 137 std::string EscapeJsonStringValue(const std::string& src); 138 std::string UnescapeJsonStringValue(const std::string& src); 139 140 private: 141 bool isUpdated_ = false; 142 int64_t seq_ = 0; 143 int32_t pid_ = 0; 144 int32_t tid_ = 0; 145 int32_t uid_ = 0; 146 int16_t tz_ = 0; 147 int64_t eventSeq_ = -1; 148 uint8_t privacy_ = 0; 149 std::string tag_; 150 std::string level_; 151 std::shared_ptr<EventRaw::RawDataBuilder> builder_; 152 std::string sysVersion_; 153 }; 154 155 class SysEventCreator { 156 public: 157 enum EventType { 158 FAULT = 1, // system fault event 159 STATISTIC = 2, // system statistic event 160 SECURITY = 3, // system security event 161 BEHAVIOR = 4 // system behavior event 162 }; 163 164 public: 165 SysEventCreator(const std::string &domain, const std::string &eventName, EventType type); 166 167 public: 168 template<typename T> SetKeyValue(const std::string & key,T value)169 void SetKeyValue(const std::string& key, T value) 170 { 171 if (builder_ == nullptr) { 172 return; 173 } 174 if constexpr (std::is_same_v<std::decay_t<T>, std::vector<std::string>>) { 175 std::vector<std::string> transVal; 176 for (auto item : value) { 177 transVal.emplace_back(EscapeJsonStringValue(item)); 178 } 179 builder_->AppendValue(key, transVal); 180 return; 181 } 182 if constexpr (std::is_same_v<std::decay_t<T>, std::string>) { 183 value = EscapeJsonStringValue(value); 184 } 185 builder_->AppendValue(key, value); 186 } 187 188 public: 189 std::shared_ptr<EventRaw::RawData> GetRawData(); 190 191 private: 192 std::string EscapeJsonStringValue(const std::string& src); 193 194 private: 195 std::shared_ptr<EventRaw::RawDataBuilder> builder_; 196 }; 197 } // namespace HiviewDFX 198 } // namespace OHOS 199 #endif // HIVIEW_BASE_SYS_EVENT_H 200