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