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