• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 <iomanip>
20 #include <memory>
21 #include <sstream>
22 #include <string>
23 #include <type_traits>
24 #include <vector>
25 
26 #include "pipeline.h"
27 
28 namespace OHOS {
29 namespace HiviewDFX {
30 class SysEventCreator;
31 enum ParseStatus {
32     STATE_PARSING_DOMAIN,
33     STATE_PARSING_NAME,
34     STATE_PARSING_TYPE,
35     STATE_PARSING_TIME,
36     STATE_PARSING_TZONE,
37     STATE_PARSING_PID,
38     STATE_PARSING_TID,
39     STATE_PARSING_UID,
40     STATE_PARSING_TRACE_ID,
41     STATE_PARSING_SPAN_ID,
42     STATE_PARSING_PARENT_SPAN_ID,
43     STATE_PARSING_TRACE_FLAG,
44 };
45 struct ParseItem {
46     const char* keyString;
47     const char* valueStart;
48     const char* valueEnd1;
49     const char* valueEnd2;
50     ParseStatus status;
51     bool parseContine;
52 };
53 
54 class SysEvent : public PipelineEvent {
55 public:
56     SysEvent(const std::string& sender, PipelineEventProducer* handler, const std::string& jsonStr);
57     SysEvent(const std::string& sender, PipelineEventProducer* handler, SysEventCreator& sysEventCreator);
58     ~SysEvent();
59 public:
60     int PaserJson();
61     int32_t GetPid() const;
62     int32_t GetTid() const;
63     int32_t GetUid() const;
64     int16_t GetTz() const;
65     void SetSeq(int64_t);
66     int64_t GetSeq() const;
67     std::string GetEventValue(const std::string& key);
68     uint64_t GetEventIntValue(const std::string& key);
69     void SetEventValue(const std::string& key, int64_t value);
70     void SetEventValue(const std::string& key, const std::string& value, bool append = false);
71 
72 private:
73     int64_t seq_;
74     int32_t pid_;
75     int32_t tid_;
76     int32_t uid_;
77     int16_t tz_;
78     void InitialMember(ParseStatus status, const std::string &content);
79 };
80 
81 class SysEventCreator {
82 public:
83     enum EventType {
84         FAULT     = 1,    // system fault event
85         STATISTIC = 2,    // system statistic event
86         SECURITY  = 3,    // system security event
87         BEHAVIOR  = 4     // system behavior event
88     };
89 public:
90     SysEventCreator(const std::string &domain, const std::string &eventName, EventType type);
91 
92     template <typename T, typename U, typename... Rest>
93     struct is_one_of : std::conditional_t<std::is_same_v<typename std::decay_t<T>, typename std::decay_t<U>>,
94         std::true_type, is_one_of<T, Rest...>> {};
95 
96     template <typename T, typename U>
97     struct is_one_of<T, U> : std::conditional_t<std::is_same_v<typename std::decay_t<T>, typename std::decay_t<U>>,
98         std::true_type, std::false_type> {};
99 
100     template <typename Inst, template <typename...> typename Tmpl>
101     struct is_instantiation_of : std::false_type {};
102 
103     template <template <typename...> typename Tmpl, typename... Args>
104     struct is_instantiation_of<Tmpl<Args...>, Tmpl> : std::true_type {};
105 
106     // supported types of the key
107     template <typename T>
108     struct is_type_key : is_one_of<T, char *, char const *, std::string>::type {};
109 
110     template <typename T>
111     inline static constexpr bool is_type_key_v = is_type_key<T>::value;
112 
113     // supported base types of the value
114     template <typename T>
115     struct is_type_value_base : is_one_of<T, bool, char, signed char, unsigned char, short, unsigned short, int,
116         unsigned int, long, unsigned long, long long, unsigned long long, float, double, char *, char const *,
117         std::string>::type {};
118 
119     template <typename T>
120     inline static constexpr bool is_type_value_base_v = is_type_value_base<T>::value;
121 
122     // supported vector types of the value
123     template <typename T>
124     inline static constexpr bool is_type_value_vector_v = []() {
125         if constexpr(is_instantiation_of<typename std::decay_t<T>, std::vector>::value) {
126             return is_type_value_base_v<typename std::decay_t<T>::value_type>;
127         }
128         return false;
129     }();
130 
131     template<typename T>
132     static decltype(auto) GetItem(T&& item)
133     {
134         if constexpr(is_one_of<T, char, signed char, unsigned char>::value) {
135             return static_cast<short>(item);
136         } else if constexpr(is_one_of<T, char *, char const *, std::string>::value) {
137             return std::quoted(EscapeStringValue(item));
138         } else {
139             return std::forward<T>(item);
140         }
141     }
142 
143     template<typename K, typename V>
144     SysEventCreator& SetKeyValue(K&& key, V&& value)
145     {
146         jsonStr_ << GetItem(std::forward<K>(key)) << ":";
147         if constexpr(is_type_value_base_v<V>) {
148             jsonStr_ << GetItem(std::forward<V>(value)) << ",";
149         } else if constexpr(is_type_value_vector_v<V>) {
150             jsonStr_ << "[";
151             for (const auto &it : value) {
152                 jsonStr_ << GetItem(it) << ",";
153             }
154             if (!value.empty()) {
155                 jsonStr_.seekp(-1, std::ios_base::end);
156             }
157             jsonStr_ << "],";
158         }
159         return *this;
160     }
161 
162 private:
163     friend class SysEvent;
164     std::string BuildSysEventJson();
165     static std::string EscapeStringValue(const std::string &value);
166     static std::string EscapeStringValue(const char* value);
167     static std::string EscapeStringValue(char* value);
168 private:
169     std::stringstream jsonStr_;
170 };
171 } // namespace HiviewDFX
172 } // namespace OHOS
173 #endif // HIVIEW_BASE_SYS_EVENT_H
174