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 HI_APP_EVENT_BASE_H
17 #define HI_APP_EVENT_BASE_H
18 
19 #include <ctime>
20 #include <list>
21 #include <memory>
22 #include <string>
23 #include <vector>
24 
25 namespace OHOS {
26 namespace HiviewDFX {
27 /**
28  * HiAppEvent write app event error code
29  */
30 namespace ErrorCode {
31 const int HIAPPEVENT_VERIFY_SUCCESSFUL = 0;
32 const int ERROR_INVALID_EVENT_NAME = -1;
33 const int ERROR_INVALID_PARAM_TYPE_JS = -2;
34 const int ERROR_INVALID_PARAM_NUM_JS = -3;
35 const int ERROR_INVALID_EVENT_DOMAIN = -4;
36 const int ERROR_INVALID_WATCHER = -5;
37 const int ERROR_WATCHER_NOT_ADDED = -6;
38 const int ERROR_INVALID_PROCESSOR = -7;
39 const int ERROR_PROCESSOR_NOT_ADDED = -8;
40 const int ERROR_INVALID_PARAM_VALUE = -9;
41 const int ERROR_INVALID_PARAM_NAME = 1;
42 const int ERROR_INVALID_PARAM_KEY_TYPE = 2;
43 const int ERROR_INVALID_PARAM_VALUE_TYPE = 3;
44 const int ERROR_INVALID_PARAM_VALUE_LENGTH = 4;
45 const int ERROR_INVALID_PARAM_NUM = 5;
46 const int ERROR_INVALID_LIST_PARAM_SIZE = 6;
47 const int ERROR_INVALID_LIST_PARAM_TYPE = 7;
48 const int ERROR_DUPLICATE_PARAM = 8;
49 const int ERROR_INVALID_CUSTOM_PARAM_NUM = 9;
50 const int ERROR_HIAPPEVENT_DISABLE = -99;
51 const int ERROR_UNKNOWN = -100;
52 const int ERROR_NOT_APP = -200;
53 } // namespace ErrorCode
54 
55 /**
56  * HiLog hiappevent domain code
57  */
58 const unsigned int HIAPPEVENT_DOMAIN = 0xD002D07;
59 
60 enum AppEventParamType {
61     EMPTY = 0,
62     BOOL = 1,
63     CHAR = 2,
64     SHORT = 3,
65     INTEGER = 4,
66     LONGLONG = 5,
67     FLOAT = 6,
68     DOUBLE = 7,
69     STRING = 8,
70     BVECTOR = 9,
71     CVECTOR = 10,
72     SHVECTOR = 11,
73     IVECTOR = 12,
74     LLVECTOR = 13,
75     FVECTOR = 14,
76     DVECTOR = 15,
77     STRVECTOR = 16
78 };
79 
80 struct AppEventParamValue {
81     AppEventParamType type;
82     union ValueUnion {
83         bool b_;
84         char c_;
85         int16_t sh_;
86         int i_;
87         int64_t ll_;
88         float f_;
89         double d_;
90         std::string str_;
91         std::vector<bool> bs_;
92         std::vector<char> cs_;
93         std::vector<int16_t> shs_;
94         std::vector<int> is_;
95         std::vector<int64_t> lls_;
96         std::vector<float> fs_;
97         std::vector<double> ds_;
98         std::vector<std::string> strs_;
99 
ValueUnion()100         ValueUnion() {}
101 
ValueUnion(AppEventParamType type)102         ValueUnion(AppEventParamType type)
103         {
104             switch (type) {
105                 case AppEventParamType::STRING:
106                     new (&str_) std::string;
107                     break;
108                 case AppEventParamType::BVECTOR:
109                     new (&bs_) std::vector<bool>;
110                     break;
111                 case AppEventParamType::CVECTOR:
112                     new (&cs_) std::vector<char>;
113                     break;
114                 case AppEventParamType::SHVECTOR:
115                     new (&shs_) std::vector<int16_t>;
116                     break;
117                 case AppEventParamType::IVECTOR:
118                     new (&is_) std::vector<int>;
119                     break;
120                 case AppEventParamType::LLVECTOR:
121                     new (&lls_) std::vector<int64_t>;
122                     break;
123                 case AppEventParamType::FVECTOR:
124                     new (&fs_) std::vector<float>;
125                     break;
126                 case AppEventParamType::DVECTOR:
127                     new (&ds_) std::vector<double>;
128                     break;
129                 case AppEventParamType::STRVECTOR:
130                     new (&strs_) std::vector<std::string>;
131                     break;
132                 default:
133                     break;
134             }
135         }
136 
~ValueUnion()137         ~ValueUnion() {}
138     } valueUnion;
139 
140     explicit AppEventParamValue(AppEventParamType t);
141     AppEventParamValue(const AppEventParamValue& value);
142     ~AppEventParamValue();
143 };
144 using AppEventParamValue = struct AppEventParamValue;
145 
146 struct AppEventParam {
147     std::string name;
148     AppEventParamType type;
149     AppEventParamValue value;
150 
151     AppEventParam(std::string n, AppEventParamType t);
152     AppEventParam(const AppEventParam& param);
153     ~AppEventParam();
154 };
155 using AppEventParam = struct AppEventParam;
156 
157 struct CustomEventParam {
158     std::string key;
159     std::string value;
160     int type = 0;
161 };
162 using CustomEventParam = struct CustomEventParam;
163 
164 class AppEventPack {
165 public:
166     AppEventPack() = default;
167     AppEventPack(const std::string& name, int type);
168     AppEventPack(const std::string& domain, const std::string& name, int type = 0);
~AppEventPack()169     ~AppEventPack() {}
170 
171 public:
172     void AddParam(const std::string& key);
173     void AddParam(const std::string& key, bool b);
174     void AddParam(const std::string& key, int8_t num);
175     void AddParam(const std::string& key, char c);
176     void AddParam(const std::string& key, int16_t s);
177     void AddParam(const std::string& key, int i);
178     void AddParam(const std::string& key, int64_t ll);
179     void AddParam(const std::string& key, float f);
180     void AddParam(const std::string& key, double d);
181     void AddParam(const std::string& key, const char *s);
182     void AddParam(const std::string& key, const std::string& s);
183     void AddParam(const std::string& key, const std::vector<bool>& bs);
184     void AddParam(const std::string& key, const std::vector<int8_t>& bs);
185     void AddParam(const std::string& key, const std::vector<char>& cs);
186     void AddParam(const std::string& key, const std::vector<int16_t>& shs);
187     void AddParam(const std::string& key, const std::vector<int>& is);
188     void AddParam(const std::string& key, const std::vector<int64_t>& lls);
189     void AddParam(const std::string& key, const std::vector<float>& fs);
190     void AddParam(const std::string& key, const std::vector<double>& ds);
191     void AddParam(const std::string& key, const std::vector<const char*>& cps);
192     void AddParam(const std::string& key, const std::vector<std::string>& strs);
193     void AddCustomParams(const std::unordered_map<std::string, std::string>& customParams);
194 
195     int64_t GetSeq() const;
196     std::string GetDomain() const;
197     std::string GetName() const;
198     int GetType() const;
199     uint64_t GetTime() const;
200     std::string GetTimeZone() const;
201     int GetPid() const;
202     int GetTid() const;
203     int64_t GetTraceId() const;
204     int64_t GetSpanId() const;
205     int64_t GetPspanId() const;
206     int GetTraceFlag() const;
207     std::string GetEventStr() const;
208     std::string GetParamStr() const;
209     std::string GetRunningId() const;
210     void GetCustomParams(std::vector<CustomEventParam>& customParams) const;
211 
212     void SetSeq(int64_t seq);
213     void SetDomain(const std::string& domain);
214     void SetName(const std::string& name);
215     void SetType(int type);
216     void SetTime(uint64_t time);
217     void SetTimeZone(const std::string& timeZone);
218     void SetPid(int pid);
219     void SetTid(int tid);
220     void SetTraceId(int64_t traceId);
221     void SetSpanId(int64_t spanId);
222     void SetPspanId(int64_t pspanId);
223     void SetTraceFlag(int traceFlag);
224     void SetParamStr(const std::string& paramStr);
225     void SetRunningId(const std::string& runningId);
226 
227     friend int VerifyAppEvent(std::shared_ptr<AppEventPack> appEventPack);
228     friend int VerifyCustomEventParams(std::shared_ptr<AppEventPack> event);
229 
230 private:
231     void InitTime();
232     void InitTimeZone();
233     void InitProcessInfo();
234     void InitTraceInfo();
235     void InitRunningId();
236     void AddBaseInfoToJsonString(std::stringstream& jsonStr) const;
237     void AddTraceInfoToJsonString(std::stringstream& jsonStr) const;
238     void AddParamsInfoToJsonString(std::stringstream& jsonStr) const;
239     void AddParamsToJsonString(std::stringstream& jsonStr) const;
240 
241 private:
242     int64_t seq_ = 0;
243     std::string domain_;
244     std::string name_;
245     int type_ = 0;
246     uint64_t time_ = 0;
247     std::string timeZone_;
248     int pid_ = 0;
249     int tid_ = 0;
250     int64_t traceId_ = 0;
251     int64_t spanId_ = 0;
252     int64_t pspanId_ = 0;
253     int traceFlag_ = 0;
254     std::string runningId_;
255     std::list<AppEventParam> baseParams_;
256     std::string paramStr_;
257 };
258 } // namespace HiviewDFX
259 } // namespace OHOS
260 #endif // HI_APP_EVENT_BASE_H
261