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