/* * Copyright (c) 2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef BASE_EVENT_RAW_DECODE_INCLUDE_DECODED_EVENT_H #define BASE_EVENT_RAW_DECODE_INCLUDE_DECODED_EVENT_H #include #include #include #include #include #include "base/raw_data_base_def.h" #include "base/raw_data.h" #include "decoded_param.h" namespace OHOS { namespace HiviewDFX { namespace EventRaw { class DecodedEvent { public: DecodedEvent(uint8_t* rawData); ~DecodedEvent(); public: std::string AsJsonStr(); std::shared_ptr GetRawData(); bool IsValid(); const struct HiSysEventHeader& GetHeader(); const struct TraceInfo& GetTraceInfo(); const std::vector>& GetAllCustomizedValues(); private: void Parse(); void ParseHeader(const size_t maxLen); void ParseCustomizedParams(const size_t maxLen); std::shared_ptr ParseCustomizedParam(const size_t maxLen); private: template void AppendValue(std::stringstream& ss, const std::string& key, T val) { if constexpr (std::is_same_v, std::string>) { ss << "\"" << key << "\":\"" << val << "\","; return; } if constexpr (std::is_same_v, bool> || std::is_same_v, int8_t> || std::is_same_v, uint8_t> || std::is_same_v, int16_t> || std::is_same_v, uint16_t> || std::is_same_v, int32_t> || std::is_same_v, uint32_t> || std::is_same_v, int64_t> || std::is_same_v, uint64_t> || std::is_same_v, float> || std::is_same_v, double>) { ss << "\"" << key << "\":" << val << ","; return; } AppendArrayValue(ss, key, val); } template void AppendArrayValue(std::stringstream& ss, const std::string& key, T vals) { ss << "\"" << key << "\":"; ss << "["; bool arrayIsNotEmpty = false; if constexpr (std::is_same_v, std::vector>) { arrayIsNotEmpty = (vals.size() > 0); for (auto item : vals) { ss << "\"" << item << "\","; } } if constexpr (std::is_same_v, std::vector> || std::is_same_v, std::vector> || std::is_same_v, std::vector> || std::is_same_v, std::vector> || std::is_same_v, std::vector> || std::is_same_v, std::vector> || std::is_same_v, std::vector> || std::is_same_v, std::vector> || std::is_same_v, std::vector> || std::is_same_v, std::vector> || std::is_same_v, std::vector>) { arrayIsNotEmpty = (vals.size() > 0); for (auto item : vals) { ss << "" << item << ","; } } if (ss.tellp() != 0 && arrayIsNotEmpty) { ss.seekp(-1, std::ios_base::end); } ss << "],"; } void AppendBaseInfo(std::stringstream& ss); void AppendCustomizedArrayParam(std::stringstream& ss, std::shared_ptr param); void AppendCustomizedParam(std::stringstream& ss, std::shared_ptr param); void AppendCustomizedParams(std::stringstream& ss); private: std::shared_ptr CreateFloatingNumTypeDecodedParam(const size_t maxLen, const std::string& key, bool isArray); std::shared_ptr CreateSignedVarintTypeDecodedParam(const size_t maxLen, const std::string& key, bool isArray); std::shared_ptr CreateStringTypeDecodedParam(const size_t maxLen, const std::string& key, bool isArray); std::shared_ptr CreateUnsignedVarintTypeDecodedParam(const size_t maxLen, const std::string& key, bool isArray); private: uint8_t* rawData_ = nullptr; size_t pos_ = 0; struct HiSysEventHeader header_ = { .domain = {0}, .name = {0}, .timestamp = 0, .timeZone = 0, .uid = 0, .pid = 0, .tid = 0, .id = 0, .type = 0, .isTraceOpened = 0, .log = 0, }; struct TraceInfo traceInfo_ { .traceFlag = 0, .traceId = 0, .spanId = 0, .pSpanId = 0, }; bool isValid_ = false; std::vector> allParams_; }; } // namespace EventRaw } // namespace HiviewDFX } // namespace OHOS #endif // BASE_EVENT_RAW_DECODE_INCLUDE_DECODED_EVENT_H