• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 class SysEventCreator;
81 class SysEvent : public PipelineEvent {
82 public:
83     SysEvent(const std::string& sender, PipelineEventProducer* handler,
84         std::shared_ptr<EventRaw::RawData> rawData, int64_t seq,
85         const std::string& sysVersion, const std::string& patchVersion);
86     SysEvent(const std::string& sender, PipelineEventProducer* handler,
87         std::shared_ptr<EventRaw::RawData> rawData, int64_t seq);
88     SysEvent(const std::string& sender, PipelineEventProducer* handler, std::shared_ptr<EventRaw::RawData> rawData);
89     SysEvent(const std::string& sender, PipelineEventProducer* handler, SysEventCreator& sysEventCreator);
90     SysEvent(const std::string& sender, PipelineEventProducer* handler, const std::string& jsonStr);
91     ~SysEvent();
92 
93 public:
94     void SetTag(const std::string& tag);
95     std::string GetTag() const;
96     void SetLevel(const std::string& level);
97     std::string GetLevel() const;
98     int32_t GetPid() const;
99     int32_t GetTid() const;
100     int32_t GetUid() const;
101     int16_t GetTz() const;
102     void SetSeq(int64_t seq);
103     int64_t GetSeq() const;
104     void SetEventSeq(int64_t eventSeq);
105     int64_t GetEventSeq() const;
106     int GetEventType() const;
107     void SetId(uint64_t id);
108     void SetLog(uint8_t log);
109     void SetPrivacy(uint8_t privacy);
110     uint8_t GetPrivacy() const;
111 
112     std::string GetEventValue(const std::string& key);
113     int64_t GetEventIntValue(const std::string& key);
114     uint64_t GetEventUintValue(const std::string& key);
115     double GetEventDoubleValue(const std::string& key);
116     bool GetEventIntArrayValue(const std::string& key, std::vector<int64_t>& dest);
117     bool GetEventUintArrayValue(const std::string& key, std::vector<uint64_t>& dest);
118     bool GetEventDoubleArrayValue(const std::string& key, std::vector<double>& dest);
119     bool GetEventStringArrayValue(const std::string& key, std::vector<std::string>& dest);
120     bool IsParamExist(const std::string& paramName);
121     bool RemoveParam(const std::string& paramName);
122     void SetInvalidParams(PARAM_INFO_MAP_PTR invalidParams);
123     PARAM_INFO_MAP_PTR GetInvalidParams();
124     std::string AsJsonStr();
125     uint8_t* AsRawData();
126     std::string GetSysVersion();
127     std::string GetPatchVersion();
128     void SetEventPeriodSeqInfo(const EventPeriodSeqInfo& info);
129     EventPeriodSeqInfo GetEventPeriodSeqInfo();
130 
131 public:
132     template<typename T>
133     void SetEventValue(const std::string& key, T value, bool appendValue = false)
134     {
135         if (!InitBuilder()) {
136             return;
137         }
138         if constexpr (std::is_same_v<std::decay_t<T>, std::string>) {
139             auto param = builder_->GetValue(key);
140             std::string paramValue;
141             if (appendValue && (param != nullptr) && param->AsString(paramValue)) {
142                 paramValue = UnescapeJsonStringValue(paramValue);
143                 paramValue.append(value);
144                 value = paramValue;
145             }
146             value = EscapeJsonStringValue(value);
147         }
148         builder_->AppendValue(key, value);
149         isUpdated_ = true;
150     }
151 
152 public:
153     int32_t eventType_;
154     bool preserve_;
155     uint8_t log_;
156     bool collect_ = false;
157 
158 public:
159     static std::atomic<uint32_t> totalCount_;
160     static std::atomic<int64_t> totalSize_;
161 
162 private:
163     void InitialMembers();
164     bool InitBuilder();
165     bool TryToUpdateRawData();
166     std::shared_ptr<EventRaw::RawData> TansJsonStrToRawData(const std::string& jsonStr);
167     std::string EscapeJsonStringValue(const std::string& src);
168     std::string UnescapeJsonStringValue(const std::string& src);
169 
170 private:
171     bool isUpdated_ = false;
172     int64_t seq_ = 0;
173     int32_t pid_ = 0;
174     int32_t tid_ = 0;
175     int32_t uid_ = 0;
176     int16_t tz_ = 0;
177     int64_t eventSeq_ = -1;
178     uint8_t privacy_ = 0;
179     std::string tag_;
180     std::string level_;
181     std::shared_ptr<EventRaw::RawDataBuilder> builder_;
182     std::string sysVersion_;
183     std::string patchVersion_;
184     PARAM_INFO_MAP_PTR invalidParams_;
185 };
186 
187 class SysEventCreator {
188 public:
189     enum EventType {
190         FAULT     = 1,    // system fault event
191         STATISTIC = 2,    // system statistic event
192         SECURITY  = 3,    // system security event
193         BEHAVIOR  = 4     // system behavior event
194     };
195 
196 public:
197     SysEventCreator(const std::string &domain, const std::string &eventName, EventType type);
198 
199 public:
200     template<typename T>
SetKeyValue(const std::string & key,T value)201     void SetKeyValue(const std::string& key, T value)
202     {
203         if (builder_ == nullptr) {
204             return;
205         }
206         if constexpr (std::is_same_v<std::decay_t<T>, std::vector<std::string>>) {
207             std::vector<std::string> transVal;
208             for (auto item : value) {
209                 transVal.emplace_back(EscapeJsonStringValue(item));
210             }
211             builder_->AppendValue(key, transVal);
212             return;
213         }
214         if constexpr (std::is_same_v<std::decay_t<T>, std::string>) {
215             value = EscapeJsonStringValue(value);
216         }
217         builder_->AppendValue(key, value);
218     }
219 
220 public:
221     std::shared_ptr<EventRaw::RawData> GetRawData();
222 
223 private:
224     std::string EscapeJsonStringValue(const std::string& src);
225 
226 private:
227     std::shared_ptr<EventRaw::RawDataBuilder> builder_;
228 };
229 } // namespace HiviewDFX
230 } // namespace OHOS
231 #endif // HIVIEW_BASE_SYS_EVENT_H
232