1 /* 2 * Copyright (c) 2021-2022 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_HIAPPEVENT_DISABLE = -99; 44 const int ERROR_UNKNOWN = -100; 45 } // namespace ErrorCode 46 47 /** 48 * HiLog hiappevent domain code 49 */ 50 const unsigned int HIAPPEVENT_DOMAIN = 0xD002D07; 51 52 enum AppEventParamType { 53 EMPTY = 0, 54 BOOL = 1, 55 CHAR = 2, 56 SHORT = 3, 57 INTEGER = 4, 58 LONGLONG = 5, 59 FLOAT = 6, 60 DOUBLE = 7, 61 STRING = 8, 62 BVECTOR = 9, 63 CVECTOR = 10, 64 SHVECTOR = 11, 65 IVECTOR = 12, 66 LLVECTOR = 13, 67 FVECTOR = 14, 68 DVECTOR = 15, 69 STRVECTOR = 16 70 }; 71 72 struct AppEventParamValue { 73 AppEventParamType type; 74 union ValueUnion { 75 bool b_; 76 char c_; 77 int16_t sh_; 78 int i_; 79 int64_t ll_; 80 float f_; 81 double d_; 82 std::string str_; 83 std::vector<bool> bs_; 84 std::vector<char> cs_; 85 std::vector<int16_t> shs_; 86 std::vector<int> is_; 87 std::vector<int64_t> lls_; 88 std::vector<float> fs_; 89 std::vector<double> ds_; 90 std::vector<std::string> strs_; 91 ValueUnion()92 ValueUnion() {} 93 ValueUnion(AppEventParamType type)94 ValueUnion(AppEventParamType type) 95 { 96 switch (type) { 97 case AppEventParamType::STRING: 98 new (&str_) std::string; 99 break; 100 case AppEventParamType::BVECTOR: 101 new (&bs_) std::vector<bool>; 102 break; 103 case AppEventParamType::CVECTOR: 104 new (&cs_) std::vector<char>; 105 break; 106 case AppEventParamType::SHVECTOR: 107 new (&shs_) std::vector<int16_t>; 108 break; 109 case AppEventParamType::IVECTOR: 110 new (&is_) std::vector<int>; 111 break; 112 case AppEventParamType::LLVECTOR: 113 new (&lls_) std::vector<int64_t>; 114 break; 115 case AppEventParamType::FVECTOR: 116 new (&fs_) std::vector<float>; 117 break; 118 case AppEventParamType::DVECTOR: 119 new (&ds_) std::vector<double>; 120 break; 121 case AppEventParamType::STRVECTOR: 122 new (&strs_) std::vector<std::string>; 123 break; 124 default: 125 break; 126 } 127 } 128 ~ValueUnion()129 ~ValueUnion() {} 130 } valueUnion; 131 132 explicit AppEventParamValue(AppEventParamType t); 133 AppEventParamValue(const AppEventParamValue& value); 134 ~AppEventParamValue(); 135 }; 136 using AppEventParamValue = struct AppEventParamValue; 137 138 struct AppEventParam { 139 std::string name; 140 AppEventParamType type; 141 AppEventParamValue value; 142 143 AppEventParam(std::string n, AppEventParamType t); 144 AppEventParam(const AppEventParam& param); 145 ~AppEventParam(); 146 }; 147 using AppEventParam = struct AppEventParam; 148 149 class AppEventPack { 150 public: 151 AppEventPack(const std::string& name, int type); 152 AppEventPack(const std::string& domain, const std::string& name, int type); ~AppEventPack()153 ~AppEventPack() {} 154 155 public: 156 void AddParam(const std::string& key); 157 void AddParam(const std::string& key, bool b); 158 void AddParam(const std::string& key, int8_t num); 159 void AddParam(const std::string& key, char c); 160 void AddParam(const std::string& key, int16_t s); 161 void AddParam(const std::string& key, int i); 162 void AddParam(const std::string& key, int64_t ll); 163 void AddParam(const std::string& key, float f); 164 void AddParam(const std::string& key, double d); 165 void AddParam(const std::string& key, const char *s); 166 void AddParam(const std::string& key, const std::string& s); 167 void AddParam(const std::string& key, const std::vector<bool>& bs); 168 void AddParam(const std::string& key, const std::vector<int8_t>& bs); 169 void AddParam(const std::string& key, const std::vector<char>& cs); 170 void AddParam(const std::string& key, const std::vector<int16_t>& shs); 171 void AddParam(const std::string& key, const std::vector<int>& is); 172 void AddParam(const std::string& key, const std::vector<int64_t>& lls); 173 void AddParam(const std::string& key, const std::vector<float>& fs); 174 void AddParam(const std::string& key, const std::vector<double>& ds); 175 void AddParam(const std::string& key, const std::vector<const char*>& cps); 176 void AddParam(const std::string& key, const std::vector<std::string>& strs); 177 const std::string GetEventDomain() const; 178 const std::string GetEventName() const; 179 int GetType() const; 180 std::string GetJsonString() const; 181 friend int VerifyAppEvent(std::shared_ptr<AppEventPack>& appEventPack); 182 183 private: 184 void AddBaseInfoToJsonString(std::stringstream& jsonStr) const; 185 void AddVectorToJsonString(std::stringstream& jsonStr, const std::vector<bool>& bs) const; 186 void AddVectorToJsonString(std::stringstream& jsonStr, const std::vector<char>& cs) const; 187 void AddVectorToJsonString(std::stringstream& jsonStr, const std::vector<int16_t>& shs) const; 188 void AddVectorToJsonString(std::stringstream& jsonStr, const std::vector<int>& is) const; 189 void AddVectorToJsonString(std::stringstream& jsonStr, const std::vector<int64_t>& lls) const; 190 void AddVectorToJsonString(std::stringstream& jsonStr, const std::vector<float>& fs) const; 191 void AddVectorToJsonString(std::stringstream& jsonStr, const std::vector<double>& ds) const; 192 void AddVectorToJsonString(std::stringstream& jsonStr, const std::vector<std::string>& strs) const; 193 void AddOthersToJsonString(std::stringstream& jsonStr, const AppEventParam param) const; 194 195 private: 196 std::string domain_; 197 std::string name_; 198 int type_; 199 std::list<AppEventParam> baseParams_; 200 }; 201 } // namespace HiviewDFX 202 } // namespace OHOS 203 #endif // HI_APP_EVENT_BASE_H 204