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 #include "wukong_sysevent_listener.h" 17 18 #include <iostream> 19 #include <nlohmann/json.hpp> 20 #include "wukong_csv_utils.h" 21 #include "hisysevent.h" 22 23 namespace OHOS { 24 namespace AppExecFwk { 25 using nlohmann::json; 26 using OHOS::HiviewDFX::HiSysEvent; 27 28 namespace { 29 template<typename ValueType> ValueGet(const json & jsonData,std::string && key,ValueType & data)30 void ValueGet(const json& jsonData, std::string&& key, ValueType& data) 31 { 32 const ValueType* valuePtr = jsonData[key].get<const ValueType *>(); 33 if (valuePtr != nullptr) { 34 data = *valuePtr; 35 } 36 } 37 } 38 OnHandle(const std::string & domain,const std::string & eventName,const int eventType,const std::string & eventDetail)39 void WuKongSysEventListener::OnHandle(const std::string& domain, 40 const std::string& eventName, 41 const int eventType, 42 const std::string& eventDetail) 43 { 44 std::cout << "----------Exception caught----------" << std::endl; 45 std::cout << "domain:" << domain << std::endl; 46 std::cout << "eventName:" << eventName << std::endl; 47 std::cout << "eventType:" << eventType << std::endl; 48 std::cout << "eventDetail:" << eventDetail << std::endl; 49 std::cout << "------------------------------------" << std::endl; 50 WuKongCsvUtils::OneLineData data {}; 51 data.domain = domain; 52 data.name = eventName; 53 switch (eventType) { 54 case HiSysEvent::EventType::FAULT: 55 data.type = "FAULT"; 56 break; 57 case HiSysEvent::EventType::STATISTIC: 58 data.type = "STATISTIC"; 59 break; 60 case HiSysEvent::EventType::SECURITY: 61 data.type = "SECURITY"; 62 break; 63 case HiSysEvent::EventType::BEHAVIOR: 64 data.type = "BEHAVIOR"; 65 break; 66 default: 67 data.type = "UNKNOWN"; 68 } 69 70 json jsonData = json::parse(eventDetail, nullptr, false); 71 if (jsonData == json::value_t::discarded) { 72 std::cerr << "event detail parse error, the content:" << eventDetail << std::endl; 73 } else { 74 ValueGet<uint64_t>(jsonData, "time_", data.time); 75 ValueGet<std::string>(jsonData, "tz_", data.timeZone); 76 ValueGet<uint64_t>(jsonData, "pid_", data.pid); 77 ValueGet<uint64_t>(jsonData, "tid_", data.tid); 78 ValueGet<uint64_t>(jsonData, "uid_", data.uid); 79 ValueGet<uint64_t>(jsonData, "traceid_", data.traceId); 80 ValueGet<uint64_t>(jsonData, "spanid_", data.spanId); 81 ValueGet<uint64_t>(jsonData, "pspanid_", data.pspanId); 82 ValueGet<uint64_t>(jsonData, "trace_flag_", data.traceFlag); 83 } 84 WuKongCsvUtils::WriteOneLine(csvFile, data); 85 } 86 OnServiceDied()87 void WuKongSysEventListener::OnServiceDied() 88 { 89 std::cout << "Listener service Died" << std::endl; 90 } 91 } // namespace AppExecFwk 92 } // namespace OHOS