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_SYS_EVENT_H 17 #define HI_SYS_EVENT_H 18 19 #include <iostream> 20 #include <string> 21 #include <sstream> 22 #include <vector> 23 24 namespace OHOS { 25 namespace HiviewDFX { 26 class HiSysEvent { 27 public: 28 friend class HiSysEventJNI; 29 // system event domain list 30 class Domain { 31 public: 32 static constexpr char AAFWK[] = "AAFWK"; 33 static constexpr char APPEXECFWK[] = "APPEXECFWK"; 34 static constexpr char ACCOUNT[] = "ACCOUNT"; 35 static constexpr char ACE[] = "ACE"; 36 static constexpr char AI[] = "AI"; 37 static constexpr char BARRIER_FREE[] = "BARRIERFREE"; 38 static constexpr char BIOMETRICS[] = "BIOMETRICS"; 39 static constexpr char CCRUNTIME[] = "CCRUNTIME"; 40 static constexpr char COMMUNICATION[] = "COMMUNICATION"; 41 static constexpr char DEVELOPTOOLS[] = "DEVELOPTOOLS"; 42 static constexpr char DISTRIBUTED_DATAMGR[] = "DISTDATAMGR"; 43 static constexpr char DISTRIBUTED_SCHEDULE[] = "DISTSCHEDULE"; 44 static constexpr char GLOBAL[] = "GLOBAL"; 45 static constexpr char GRAPHIC[] = "GRAPHIC"; 46 static constexpr char HIVIEWDFX[] = "HIVIEWDFX"; 47 static constexpr char IAWARE[] = "IAWARE"; 48 static constexpr char INTELLI_ACCESSORIES[] = "INTELLIACC"; 49 static constexpr char INTELLI_TV[] = "INTELLITV"; 50 static constexpr char IVI_HARDWARE[] = "IVIHARDWARE"; 51 static constexpr char LOCATION[] = "LOCATION"; 52 static constexpr char MSDP[] = "MSDP"; 53 static constexpr char MULTI_MEDIA[] = "MULTIMEDIA"; 54 static constexpr char MULTI_MODAL_INPUT[] = "MULTIMODALINPUT"; 55 static constexpr char NOTIFICATION[] = "NOTIFICATION"; 56 static constexpr char POWERMGR[] = "POWERMGR"; 57 static constexpr char ROUTER[] = "ROUTER"; 58 static constexpr char SECURITY[] = "SECURITY"; 59 static constexpr char SENSORS[] = "SENSORS"; 60 static constexpr char SOURCE_CODE_TRANSFORMER[] = "SRCTRANSFORMER"; 61 static constexpr char STARTUP[] = "STARTUP"; 62 static constexpr char TELEPHONY[] = "TELEPHONY"; 63 static constexpr char UPDATE[] = "UPDATE"; 64 static constexpr char USB[] = "USB"; 65 static constexpr char WEARABLE_HARDWARE[] = "WEARABLEHW"; 66 static constexpr char WEARABLE[] = "WEARABLE"; 67 static constexpr char WINDOW_MANAGER[] = "WINDOWMANAGER"; 68 static constexpr char OTHERS[] = "OTHERS"; 69 }; 70 71 enum EventType { 72 FAULT = 1, // system fault event 73 STATISTIC = 2, // system statistic event 74 SECURITY = 3, // system security event 75 BEHAVIOR = 4 // system behavior event 76 }; 77 78 public: 79 /** 80 * @brief write system event 81 * @param domain system event domain name 82 * @param eventName system event name 83 * @param type system event type 84 * @param keyValues system event parameter name or value 85 * @return 0 success, other fail 86 */ 87 template<typename... Types> Write(const std::string & domain,const std::string & eventName,EventType type,Types...keyValues)88 static int Write(const std::string &domain, const std::string &eventName, 89 EventType type, Types... keyValues) 90 { 91 EventBase eventBase(domain, eventName, type); 92 eventBase.jsonStr_ << "{"; 93 WritebaseInfo(eventBase); 94 if (IsError(eventBase)) { 95 ExplainRetCode(eventBase); 96 return eventBase.retCode_; 97 } 98 99 InnerWrite(eventBase, keyValues...); 100 if (IsError(eventBase)) { 101 ExplainRetCode(eventBase); 102 return eventBase.retCode_; 103 } 104 eventBase.jsonStr_ << "}"; 105 106 SendSysEvent(eventBase); 107 return eventBase.retCode_; 108 } 109 110 private: 111 class EventBase { 112 public: EventBase(const std::string & domain,const std::string & eventName,int type)113 EventBase(const std::string &domain, const std::string &eventName, int type) 114 : retCode_(0), keyCnt_(0), domain_(domain), eventName_(eventName), type_(type) 115 {}; ~EventBase()116 ~EventBase() {} 117 public: 118 int retCode_; 119 unsigned int keyCnt_; 120 std::stringstream jsonStr_; 121 const std::string domain_; 122 const std::string eventName_; 123 const int type_; 124 }; 125 126 private: 127 template<typename T> AppendData(EventBase & eventBase,const std::string & key,T value)128 static void AppendData(EventBase &eventBase, const std::string &key, T value) 129 { 130 if (IsWarnAndUpdate(CheckKey(key), eventBase)) { 131 return; 132 } 133 if (UpdateAndCheckKeyNumIsOver(eventBase)) { 134 return; 135 } 136 eventBase.jsonStr_ << "\"" << key << "\":"; 137 AppendValue(eventBase, value); 138 eventBase.jsonStr_ << ","; 139 } 140 141 template<typename T> AppendArrayData(EventBase & eventBase,const std::string & key,const std::vector<T> & value)142 static void AppendArrayData(EventBase &eventBase, const std::string &key, const std::vector<T> &value) 143 { 144 if (IsWarnAndUpdate(CheckKey(key), eventBase)) { 145 return; 146 } 147 if (UpdateAndCheckKeyNumIsOver(eventBase)) { 148 return; 149 } 150 if (value.empty()) { 151 eventBase.jsonStr_ << "\"" << key << "\":[],"; 152 return; 153 } 154 IsWarnAndUpdate(CheckArraySize(value.size()), eventBase); 155 156 unsigned int index = 0; 157 unsigned int arrayMax = GetArrayMax(); 158 eventBase.jsonStr_ << "\"" << key << "\":["; 159 for (auto item = value.begin(); item != value.end(); item++) { 160 index++; 161 if (index > arrayMax) { 162 break; 163 } 164 AppendValue(eventBase, *item); 165 eventBase.jsonStr_ << ","; 166 } 167 if (eventBase.jsonStr_.tellp() != 0) { 168 eventBase.jsonStr_.seekp(-1, std::ios_base::end); 169 } 170 eventBase.jsonStr_ << "],"; 171 } 172 173 template<typename... Types> InnerWrite(EventBase & eventBase,const std::string & key,bool value,Types...keyValues)174 static void InnerWrite(EventBase &eventBase, const std::string &key, bool value, Types... keyValues) 175 { 176 AppendData<bool>(eventBase, key, value); 177 InnerWrite(eventBase, keyValues...); 178 } 179 180 template<typename... Types> InnerWrite(EventBase & eventBase,const std::string & key,const char value,Types...keyValues)181 static void InnerWrite(EventBase &eventBase, const std::string &key, const char value, Types... keyValues) 182 { 183 AppendData<short>(eventBase, key, value); 184 InnerWrite(eventBase, keyValues...); 185 } 186 187 template<typename... Types> InnerWrite(EventBase & eventBase,const std::string & key,const unsigned char value,Types...keyValues)188 static void InnerWrite(EventBase &eventBase, const std::string &key, const unsigned char value, Types... keyValues) 189 { 190 AppendData<unsigned short>(eventBase, key, value); 191 InnerWrite(eventBase, keyValues...); 192 } 193 194 template<typename... Types> InnerWrite(EventBase & eventBase,const std::string & key,const short value,Types...keyValues)195 static void InnerWrite(EventBase &eventBase, const std::string &key, const short value, Types... keyValues) 196 { 197 AppendData<short>(eventBase, key, value); 198 InnerWrite(eventBase, keyValues...); 199 } 200 201 template<typename... Types> InnerWrite(EventBase & eventBase,const std::string & key,const unsigned short value,Types...keyValues)202 static void InnerWrite(EventBase &eventBase, const std::string &key, const unsigned short value, Types... keyValues) 203 { 204 AppendData<unsigned short>(eventBase, key, value); 205 InnerWrite(eventBase, keyValues...); 206 } 207 208 template<typename... Types> InnerWrite(EventBase & eventBase,const std::string & key,const int value,Types...keyValues)209 static void InnerWrite(EventBase &eventBase, const std::string &key, const int value, Types... keyValues) 210 { 211 AppendData<int>(eventBase, key, value); 212 InnerWrite(eventBase, keyValues...); 213 } 214 215 template<typename... Types> InnerWrite(EventBase & eventBase,const std::string & key,const unsigned int value,Types...keyValues)216 static void InnerWrite(EventBase &eventBase, const std::string &key, const unsigned int value, Types... keyValues) 217 { 218 AppendData<unsigned int>(eventBase, key, value); 219 InnerWrite(eventBase, keyValues...); 220 } 221 222 template<typename... Types> InnerWrite(EventBase & eventBase,const std::string & key,const long value,Types...keyValues)223 static void InnerWrite(EventBase &eventBase, const std::string &key, const long value, Types... keyValues) 224 { 225 AppendData<long>(eventBase, key, value); 226 InnerWrite(eventBase, keyValues...); 227 } 228 229 template<typename... Types> InnerWrite(EventBase & eventBase,const std::string & key,const unsigned long value,Types...keyValues)230 static void InnerWrite(EventBase &eventBase, const std::string &key, const unsigned long value, Types... keyValues) 231 { 232 AppendData<unsigned long>(eventBase, key, value); 233 InnerWrite(eventBase, keyValues...); 234 } 235 236 template<typename... Types> InnerWrite(EventBase & eventBase,const std::string & key,const long long value,Types...keyValues)237 static void InnerWrite(EventBase &eventBase, const std::string &key, const long long value, Types... keyValues) 238 { 239 AppendData<long long>(eventBase, key, value); 240 InnerWrite(eventBase, keyValues...); 241 } 242 243 template<typename... Types> InnerWrite(EventBase & eventBase,const std::string & key,const unsigned long long value,Types...keyValues)244 static void InnerWrite(EventBase &eventBase, 245 const std::string &key, const unsigned long long value, Types... keyValues) 246 { 247 AppendData<unsigned long long>(eventBase, key, value); 248 InnerWrite(eventBase, keyValues...); 249 } 250 251 template<typename... Types> InnerWrite(EventBase & eventBase,const std::string & key,const float value,Types...keyValues)252 static void InnerWrite(EventBase &eventBase, const std::string &key, const float value, Types... keyValues) 253 { 254 AppendData<float>(eventBase, key, value); 255 InnerWrite(eventBase, keyValues...); 256 } 257 258 template<typename... Types> InnerWrite(EventBase & eventBase,const std::string & key,const double value,Types...keyValues)259 static void InnerWrite(EventBase &eventBase, const std::string &key, const double value, Types... keyValues) 260 { 261 AppendData<double>(eventBase, key, value); 262 InnerWrite(eventBase, keyValues...); 263 } 264 265 template<typename... Types> InnerWrite(EventBase & eventBase,const std::string & key,const std::string & value,Types...keyValues)266 static void InnerWrite(EventBase &eventBase, const std::string &key, const std::string &value, Types... keyValues) 267 { 268 AppendData(eventBase, key, value); 269 InnerWrite(eventBase, keyValues...); 270 } 271 272 template<typename... Types> InnerWrite(EventBase & eventBase,const std::string & key,const char * value,Types...keyValues)273 static void InnerWrite(EventBase &eventBase, const std::string &key, const char *value, Types... keyValues) 274 { 275 AppendData(eventBase, key, std::string(value)); 276 InnerWrite(eventBase, keyValues...); 277 } 278 279 template<typename... Types> InnerWrite(EventBase & eventBase,const std::string & key,const std::vector<bool> & value,Types...keyValues)280 static void InnerWrite(EventBase &eventBase, 281 const std::string &key, const std::vector<bool> &value, Types... keyValues) 282 { 283 AppendArrayData<bool>(eventBase, key, value); 284 InnerWrite(eventBase, keyValues...); 285 } 286 287 template<typename... Types> InnerWrite(EventBase & eventBase,const std::string & key,const std::vector<char> & value,Types...keyValues)288 static void InnerWrite(EventBase &eventBase, 289 const std::string &key, const std::vector<char> &value, Types... keyValues) 290 { 291 AppendArrayData(eventBase, key, value); 292 InnerWrite(eventBase, keyValues...); 293 } 294 295 template<typename... Types> InnerWrite(EventBase & eventBase,const std::string & key,const std::vector<unsigned char> & value,Types...keyValues)296 static void InnerWrite(EventBase &eventBase, 297 const std::string &key, const std::vector<unsigned char> &value, Types... keyValues) 298 { 299 AppendArrayData(eventBase, key, value); 300 InnerWrite(eventBase, keyValues...); 301 } 302 303 template<typename... Types> InnerWrite(EventBase & eventBase,const std::string & key,const std::vector<short> & value,Types...keyValues)304 static void InnerWrite(EventBase &eventBase, 305 const std::string &key, const std::vector<short> &value, Types... keyValues) 306 { 307 AppendArrayData<short>(eventBase, key, value); 308 InnerWrite(eventBase, keyValues...); 309 } 310 311 template<typename... Types> InnerWrite(EventBase & eventBase,const std::string & key,const std::vector<unsigned short> & value,Types...keyValues)312 static void InnerWrite(EventBase &eventBase, 313 const std::string &key, const std::vector<unsigned short> &value, Types... keyValues) 314 { 315 AppendArrayData<unsigned short>(eventBase, key, value); 316 InnerWrite(eventBase, keyValues...); 317 } 318 319 template<typename... Types> InnerWrite(EventBase & eventBase,const std::string & key,const std::vector<int> & value,Types...keyValues)320 static void InnerWrite(EventBase &eventBase, 321 const std::string &key, const std::vector<int> &value, Types... keyValues) 322 { 323 AppendArrayData<int>(eventBase, key, value); 324 InnerWrite(eventBase, keyValues...); 325 } 326 327 template<typename... Types> InnerWrite(EventBase & eventBase,const std::string & key,const std::vector<unsigned int> & value,Types...keyValues)328 static void InnerWrite(EventBase &eventBase, 329 const std::string &key, const std::vector<unsigned int> &value, Types... keyValues) 330 { 331 AppendArrayData<unsigned int>(eventBase, key, value); 332 InnerWrite(eventBase, keyValues...); 333 } 334 335 template<typename... Types> InnerWrite(EventBase & eventBase,const std::string & key,const std::vector<long> & value,Types...keyValues)336 static void InnerWrite(EventBase &eventBase, 337 const std::string &key, const std::vector<long> &value, Types... keyValues) 338 { 339 AppendArrayData<long>(eventBase, key, value); 340 InnerWrite(eventBase, keyValues...); 341 } 342 343 template<typename... Types> InnerWrite(EventBase & eventBase,const std::string & key,const std::vector<unsigned long> & value,Types...keyValues)344 static void InnerWrite(EventBase &eventBase, 345 const std::string &key, const std::vector<unsigned long> &value, Types... keyValues) 346 { 347 AppendArrayData<unsigned long>(eventBase, key, value); 348 InnerWrite(eventBase, keyValues...); 349 } 350 351 template<typename... Types> InnerWrite(EventBase & eventBase,const std::string & key,const std::vector<long long> & value,Types...keyValues)352 static void InnerWrite(EventBase &eventBase, 353 const std::string &key, const std::vector<long long> &value, Types... keyValues) 354 { 355 AppendArrayData<long long>(eventBase, key, value); 356 InnerWrite(eventBase, keyValues...); 357 } 358 359 template<typename... Types> InnerWrite(EventBase & eventBase,const std::string & key,const std::vector<unsigned long long> & value,Types...keyValues)360 static void InnerWrite(EventBase &eventBase, 361 const std::string &key, const std::vector<unsigned long long> &value, Types... keyValues) 362 { 363 AppendArrayData<unsigned long long>(eventBase, key, value); 364 InnerWrite(eventBase, keyValues...); 365 } 366 367 template<typename... Types> InnerWrite(EventBase & eventBase,const std::string & key,const std::vector<float> & value,Types...keyValues)368 static void InnerWrite(EventBase &eventBase, 369 const std::string &key, const std::vector<float> &value, Types... keyValues) 370 { 371 AppendArrayData<float>(eventBase, key, value); 372 InnerWrite(eventBase, keyValues...); 373 } 374 375 template<typename... Types> InnerWrite(EventBase & eventBase,const std::string & key,const std::vector<double> & value,Types...keyValues)376 static void InnerWrite(EventBase &eventBase, 377 const std::string &key, const std::vector<double> &value, Types... keyValues) 378 { 379 AppendArrayData<double>(eventBase, key, value); 380 InnerWrite(eventBase, keyValues...); 381 } 382 383 template<typename... Types> InnerWrite(EventBase & eventBase,const std::string & key,const std::vector<std::string> & value,Types...keyValues)384 static void InnerWrite(EventBase &eventBase, 385 const std::string &key, const std::vector<std::string> &value, Types... keyValues) 386 { 387 AppendArrayData(eventBase, key, value); 388 InnerWrite(eventBase, keyValues...); 389 } 390 391 template<typename T> AppendValue(EventBase & eventBase,const T item)392 static void AppendValue(EventBase &eventBase, const T item) 393 { 394 eventBase.jsonStr_ << item; 395 } 396 397 static void AppendValue(EventBase &eventBase, const std::string &item); 398 static void AppendValue(EventBase &eventBase, const char item); 399 static void AppendValue(EventBase &eventBase, const unsigned char item); 400 static void AppendHexData(EventBase &eventBase, const std::string &key, uint64_t value); 401 static void InnerWrite(EventBase &eventBase); 402 static void WritebaseInfo(EventBase &eventBase); 403 404 static int CheckKey(const std::string &key); 405 static int CheckValue(const std::string &value); 406 static int CheckArraySize(unsigned long size); 407 static bool IsErrorAndUpdate(int retCode, EventBase &eventBase); 408 static bool IsWarnAndUpdate(int retCode, EventBase &eventBase); 409 static bool UpdateAndCheckKeyNumIsOver(EventBase &eventBase); 410 static bool IsError(EventBase &eventBase); 411 static void ExplainRetCode(EventBase &eventBase); 412 413 static unsigned int GetArrayMax(); 414 static void SendSysEvent(EventBase &eventBase); 415 }; // HiSysEvent 416 } // namespace HiviewDFX 417 } // namespace OHOS 418 419 #endif // HI_SYS_EVENT_H