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 "sysevent_listener.h"
17
18 #include "csv_utils.h"
19 #include "hisysevent.h"
20 #include "report.h"
21
22 namespace OHOS {
23 namespace WuKong {
24 using nlohmann::json;
25 using OHOS::HiviewDFX::HiSysEvent;
26
27 namespace {
28 template <typename ValueType>
ValueGet(const json & jsonData,const std::string & key,const json::value_t vt,ValueType & data)29 void ValueGet(const json& jsonData, const std::string& key, const json::value_t vt, ValueType& data)
30 {
31 if (jsonData.contains(key)) {
32 if (jsonData[key].type() == vt) {
33 data = jsonData[key].get<ValueType>();
34 }
35 }
36 TRACK_LOG_END();
37 }
38 } // namespace
39
OnEvent(std::shared_ptr<HiviewDFX::HiSysEventRecord> sysEvent)40 void SysEventListener::OnEvent(std::shared_ptr<HiviewDFX::HiSysEventRecord> sysEvent)
41 {
42 if (sysEvent == nullptr) {
43 return;
44 }
45 std::string domain = sysEvent->GetDomain();
46 std::string eventName = sysEvent->GetEventName();
47 OHOS::HiviewDFX::HiSysEvent::EventType eventType = sysEvent->GetEventType();
48 std::string eventDetail = sysEvent->AsJson();
49
50 TRACK_LOG("----------Exception caught----------");
51 TRACK_LOG_STR("domain: %s", domain.c_str());
52 TRACK_LOG_STR("eventName: %s", eventName.c_str());
53 TRACK_LOG_STR("eventType: %d", eventType);
54 TRACK_LOG("------------------------------------");
55 CsvUtils::OneLineData data;
56 data.domain = domain;
57 data.name = eventName;
58 switch (eventType) {
59 case HiSysEvent::EventType::FAULT:
60 data.type = "FAULT";
61 break;
62 case HiSysEvent::EventType::STATISTIC:
63 data.type = "STATISTIC";
64 break;
65 case HiSysEvent::EventType::SECURITY:
66 data.type = "SECURITY";
67 break;
68 case HiSysEvent::EventType::BEHAVIOR:
69 data.type = "BEHAVIOR";
70 break;
71 default:
72 data.type = "UNKNOWN";
73 }
74 json jsonData = json::parse(eventDetail, nullptr, false);
75 if (jsonData == json::value_t::discarded) {
76 ERROR_LOG_STR("event detail parse error, the content: %s", eventDetail.c_str());
77 } else {
78 ValueGet<uint64_t>(jsonData, "time_", json::value_t::number_unsigned, data.time);
79 ValueGet<std::string>(jsonData, "tz_", json::value_t::string, data.timeZone);
80 ValueGet<uint64_t>(jsonData, "pid_", json::value_t::number_unsigned, data.pid);
81 ValueGet<uint64_t>(jsonData, "tid_", json::value_t::number_unsigned, data.tid);
82 ValueGet<uint64_t>(jsonData, "uid_", json::value_t::number_unsigned, data.uid);
83 }
84 CsvUtils::WriteOneLine(csvFile, data);
85 }
OnServiceDied()86 void SysEventListener::OnServiceDied()
87 {
88 ERROR_LOG("Listener service Died");
89 }
90 } // namespace WuKong
91 } // namespace OHOS
92