1 /* 2 * Copyright (c) 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 HISYSEVENT_OPERATION_H 17 #define HISYSEVENT_OPERATION_H 18 19 #include <string> 20 #include <sstream> 21 #include <vector> 22 23 namespace OHOS { 24 namespace PowerMgr { 25 class HiSysEventOperation { 26 public: 27 template<typename... Types> CombineHiSysEvent(const std::string & domain,const std::string & eventName,int type,Types...keyValues)28 static std::string CombineHiSysEvent(const std::string &domain, const std::string &eventName, int type, 29 Types... keyValues) 30 { 31 return InnerCombine(domain, eventName, type, keyValues...); 32 } 33 34 private: 35 class EventBase { 36 public: EventBase(const std::string & domain,const std::string & eventName,int type)37 EventBase(const std::string &domain, const std::string &eventName, int type) 38 : retCode_(0), keyCnt_(0), domain_(domain), eventName_(eventName), type_(type) 39 {}; ~EventBase()40 ~EventBase() {} 41 public: 42 int retCode_; 43 unsigned int keyCnt_; 44 std::stringstream jsonStr_; 45 const std::string domain_; 46 const std::string eventName_; 47 const int type_; 48 }; 49 50 private: 51 template<typename... Types> InnerCombine(const std::string & domain,const std::string & eventName,int type,Types...keyValues)52 static std::string InnerCombine(const std::string &domain, const std::string &eventName, int type, 53 Types... keyValues) 54 { 55 EventBase eventBase(domain, eventName, type); 56 eventBase.jsonStr_ << "{"; 57 CombineBaseInfo(eventBase); 58 if (IsError(eventBase)) { 59 ExplainRetCode(eventBase); 60 return eventBase.jsonStr_.str(); 61 } 62 63 InnerCombine(eventBase, keyValues...); 64 if (IsError(eventBase)) { 65 ExplainRetCode(eventBase); 66 return eventBase.jsonStr_.str(); 67 } 68 eventBase.jsonStr_ << "}"; 69 return eventBase.jsonStr_.str(); 70 } 71 72 template<typename T> AppendData(EventBase & eventBase,const std::string & key,T value)73 static void AppendData(EventBase &eventBase, const std::string &key, T value) 74 { 75 if (IsWarnAndUpdate(CheckKey(key), eventBase)) { 76 return; 77 } 78 if (UpdateAndCheckKeyNumIsOver(eventBase)) { 79 return; 80 } 81 eventBase.jsonStr_ << "\"" << key << "\":"; 82 AppendValue(eventBase, value); 83 eventBase.jsonStr_ << ","; 84 } 85 86 template<typename... Types> InnerCombine(EventBase & eventBase,const std::string & key,bool value,Types...keyValues)87 static void InnerCombine(EventBase &eventBase, const std::string &key, bool value, Types... keyValues) 88 { 89 AppendData<bool>(eventBase, key, value); 90 InnerCombine(eventBase, keyValues...); 91 } 92 93 template<typename... Types> InnerCombine(EventBase & eventBase,const std::string & key,const char value,Types...keyValues)94 static void InnerCombine(EventBase &eventBase, const std::string &key, const char value, Types... keyValues) 95 { 96 AppendData<short>(eventBase, key, value); 97 InnerCombine(eventBase, keyValues...); 98 } 99 100 template<typename... Types> InnerCombine(EventBase & eventBase,const std::string & key,const unsigned char value,Types...keyValues)101 static void InnerCombine(EventBase &eventBase, const std::string &key, 102 const unsigned char value, Types... keyValues) 103 { 104 AppendData<unsigned short>(eventBase, key, value); 105 InnerCombine(eventBase, keyValues...); 106 } 107 108 template<typename... Types> InnerCombine(EventBase & eventBase,const std::string & key,const short value,Types...keyValues)109 static void InnerCombine(EventBase &eventBase, const std::string &key, const short value, Types... keyValues) 110 { 111 AppendData<short>(eventBase, key, value); 112 InnerCombine(eventBase, keyValues...); 113 } 114 115 template<typename... Types> InnerCombine(EventBase & eventBase,const std::string & key,const unsigned short value,Types...keyValues)116 static void InnerCombine(EventBase &eventBase, const std::string &key, 117 const unsigned short value, Types... keyValues) 118 { 119 AppendData<unsigned short>(eventBase, key, value); 120 InnerCombine(eventBase, keyValues...); 121 } 122 123 template<typename... Types> InnerCombine(EventBase & eventBase,const std::string & key,const int value,Types...keyValues)124 static void InnerCombine(EventBase &eventBase, const std::string &key, const int value, Types... keyValues) 125 { 126 AppendData<int>(eventBase, key, value); 127 InnerCombine(eventBase, keyValues...); 128 } 129 130 template<typename... Types> InnerCombine(EventBase & eventBase,const std::string & key,const unsigned int value,Types...keyValues)131 static void InnerCombine(EventBase &eventBase, const std::string &key, const unsigned int value, Types... keyValues) 132 { 133 AppendData<unsigned int>(eventBase, key, value); 134 InnerCombine(eventBase, keyValues...); 135 } 136 137 template<typename... Types> InnerCombine(EventBase & eventBase,const std::string & key,const long value,Types...keyValues)138 static void InnerCombine(EventBase &eventBase, const std::string &key, const long value, Types... keyValues) 139 { 140 AppendData<long>(eventBase, key, value); 141 InnerCombine(eventBase, keyValues...); 142 } 143 144 template<typename... Types> InnerCombine(EventBase & eventBase,const std::string & key,const unsigned long value,Types...keyValues)145 static void InnerCombine(EventBase &eventBase, const std::string &key, 146 const unsigned long value, Types... keyValues) 147 { 148 AppendData<unsigned long>(eventBase, key, value); 149 InnerCombine(eventBase, keyValues...); 150 } 151 152 template<typename... Types> InnerCombine(EventBase & eventBase,const std::string & key,const long long value,Types...keyValues)153 static void InnerCombine(EventBase &eventBase, const std::string &key, const long long value, Types... keyValues) 154 { 155 AppendData<long long>(eventBase, key, value); 156 InnerCombine(eventBase, keyValues...); 157 } 158 159 template<typename... Types> InnerCombine(EventBase & eventBase,const std::string & key,const unsigned long long value,Types...keyValues)160 static void InnerCombine(EventBase &eventBase, 161 const std::string &key, const unsigned long long value, Types... keyValues) 162 { 163 AppendData<unsigned long long>(eventBase, key, value); 164 InnerCombine(eventBase, keyValues...); 165 } 166 167 template<typename... Types> InnerCombine(EventBase & eventBase,const std::string & key,const float value,Types...keyValues)168 static void InnerCombine(EventBase &eventBase, const std::string &key, const float value, Types... keyValues) 169 { 170 AppendData<float>(eventBase, key, value); 171 InnerCombine(eventBase, keyValues...); 172 } 173 174 template<typename... Types> InnerCombine(EventBase & eventBase,const std::string & key,const double value,Types...keyValues)175 static void InnerCombine(EventBase &eventBase, const std::string &key, const double value, Types... keyValues) 176 { 177 AppendData<double>(eventBase, key, value); 178 InnerCombine(eventBase, keyValues...); 179 } 180 181 template<typename... Types> InnerCombine(EventBase & eventBase,const std::string & key,const std::string & value,Types...keyValues)182 static void InnerCombine(EventBase &eventBase, const std::string &key, const std::string &value, Types... keyValues) 183 { 184 AppendData(eventBase, key, value); 185 InnerCombine(eventBase, keyValues...); 186 } 187 188 template<typename... Types> InnerCombine(EventBase & eventBase,const std::string & key,const char * value,Types...keyValues)189 static void InnerCombine(EventBase &eventBase, const std::string &key, const char *value, Types... keyValues) 190 { 191 AppendData(eventBase, key, std::string(value)); 192 InnerCombine(eventBase, keyValues...); 193 } 194 195 template<typename T> AppendValue(EventBase & eventBase,const T item)196 static void AppendValue(EventBase &eventBase, const T item) 197 { 198 eventBase.jsonStr_ << item; 199 } 200 201 static void AppendValue(EventBase &eventBase, const std::string &item); 202 static void AppendValue(EventBase &eventBase, const char item); 203 static void AppendValue(EventBase &eventBase, const signed char item); 204 static void AppendValue(EventBase &eventBase, const unsigned char item); 205 static void InnerCombine(EventBase &eventBase); 206 static void CombineBaseInfo(EventBase &eventBase); 207 208 static int CheckKey(const std::string &key); 209 static bool IsError(EventBase &eventBase); 210 static void ExplainRetCode(EventBase &eventBase); 211 static bool IsWarnAndUpdate(int retCode, EventBase &eventBase); 212 static bool UpdateAndCheckKeyNumIsOver(EventBase &eventBase); 213 }; 214 } // namespace PowerMgr 215 } // namespace OHOS 216 #endif // HISYSEVENT_OPERATION_H