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