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