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_RECORD_H 17 #define HISYSEVENT_RECORD_H 18 19 #include <functional> 20 #include <memory> 21 #include <string> 22 #include <vector> 23 24 #include "hisysevent.h" 25 26 namespace OHOS { 27 namespace HiviewDFX { 28 constexpr int VALUE_PARSED_SUCCEED = 0; 29 constexpr int ERR_INIT_FAILED = -1; 30 constexpr int ERR_KEY_NOT_EXIST = -2; 31 constexpr int ERR_TYPE_NOT_MATCH = -3; 32 class HiSysEventValue; 33 class HiSysEventRecord { 34 public: HiSysEventRecord(std::string jsonStr)35 HiSysEventRecord(std::string jsonStr) 36 { 37 ParseJsonStr(jsonStr); 38 } ~HiSysEventRecord()39 ~HiSysEventRecord() {} 40 41 public: 42 std::string AsJson() const; 43 std::string GetDomain() const; 44 std::string GetEventName() const; 45 std::string GetLevel() const; 46 std::string GetTag() const; 47 std::string GetTimeZone() const; 48 HiSysEvent::EventType GetEventType() const; 49 int GetTraceFlag() const; 50 int64_t GetPid() const; 51 int64_t GetTid() const; 52 int64_t GetUid() const; 53 uint64_t GetPspanId() const; 54 uint64_t GetSpanId() const; 55 uint64_t GetTime() const; 56 uint64_t GetTraceId() const; 57 void GetParamNames(std::vector<std::string>& params) const; 58 59 public: 60 int GetParamValue(const std::string& param, int64_t& value) const; 61 int GetParamValue(const std::string& param, uint64_t& value) const; 62 int GetParamValue(const std::string& param, double& value) const; 63 int GetParamValue(const std::string& param, std::string& value) const; 64 int GetParamValue(const std::string& param, std::vector<int64_t>& value) const; 65 int GetParamValue(const std::string& param, std::vector<uint64_t>& value) const; 66 int GetParamValue(const std::string& param, std::vector<double>& value) const; 67 int GetParamValue(const std::string& param, std::vector<std::string>& value) const; 68 69 private: 70 int GetIntValueByKey(const std::string key) const; 71 int64_t GetInt64ValueByKey(const std::string key) const; 72 uint64_t GetUInt64ValueByKey(const std::string key) const; 73 std::string GetStringValueByKey(const std::string key) const; 74 75 private: 76 using JsonValue = std::shared_ptr<HiSysEventValue>; 77 using TypeFilter = std::function<bool(JsonValue)>; 78 using ValueAssigner = std::function<void(JsonValue)>; 79 bool IsInt64ValueType(const JsonValue val) const; 80 bool IsUInt64ValueType(const JsonValue val) const; 81 bool IsDoubleValueType(const JsonValue val) const; 82 bool IsStringValueType(const JsonValue val) const; 83 bool IsArray(const JsonValue val, const TypeFilter filterFunc) const; 84 void ParseJsonStr(const std::string jsonStr); 85 int GetParamValue(const std::string& param, const TypeFilter filterFunc, const ValueAssigner assignFunc) const; 86 87 private: 88 std::string jsonStr_; 89 JsonValue jsonVal_; 90 }; 91 } // namespace HiviewDFX 92 } // OHOS 93 94 #endif // HISYSEVENT_RECORD_H