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