• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 "plugin.h"
17 
18 #include <regex>
19 
20 #include "file_util.h"
21 #include "hisysevent.h"
22 #include "logger.h"
23 #include "plugin_factory.h"
24 #include "sys_event_dao.h"
25 
26 namespace OHOS {
27 namespace HiviewDFX {
28 REGISTER(HiCollieCollector);
29 DEFINE_LOG_TAG("HiCollieCollector");
30 
GetListenerName()31 std::string HiCollieCollector::GetListenerName()
32 {
33     return name_;
34 }
35 
ReadyToLoad()36 bool HiCollieCollector::ReadyToLoad()
37 {
38     return true;
39 }
40 
CanProcessEvent(std::shared_ptr<Event> event)41 bool HiCollieCollector::CanProcessEvent(std::shared_ptr<Event> event)
42 {
43     return false;
44 }
45 
OnLoad()46 void HiCollieCollector::OnLoad()
47 {
48     SetName("HiCollieCollector");
49     SetVersion("HiCollieCollector 1.0");
50     HIVIEW_LOGI("OnLoad.");
51     AddListenerInfo(Event::MessageType::SYS_EVENT, STRINGID_SERVICE_TIMEOUT);
52     AddListenerInfo(Event::MessageType::SYS_EVENT, STRINGID_SERVICE_BLOCK);
53     GetHiviewContext()->RegisterUnorderedEventListener(
54         std::static_pointer_cast<HiCollieCollector>(shared_from_this()));
55 }
56 
OnUnload()57 void HiCollieCollector::OnUnload()
58 {
59     HIVIEW_LOGI("OnUnload.");
60 }
61 
OnEvent(std::shared_ptr<Event> & event)62 bool HiCollieCollector::OnEvent(std::shared_ptr<Event> &event)
63 {
64     return true;
65 }
66 
OnUnorderedEvent(const Event & event)67 void HiCollieCollector::OnUnorderedEvent(const Event &event)
68 {
69     HIVIEW_LOGI("received event domain=%{public}s, stringid=%{public}s.\n",
70         event.domain_.c_str(), event.eventName_.c_str());
71     if (GetHiviewContext() == nullptr) {
72         HIVIEW_LOGE("failed to get context.");
73         return;
74     }
75 
76     if (event.eventName_ != STRINGID_SERVICE_TIMEOUT && event.eventName_ != STRINGID_SERVICE_BLOCK) {
77         HIVIEW_LOGE("invalid stringid=%{public}s.\n", event.eventName_.c_str());
78         return;
79     }
80 
81     Event& eventRef = const_cast<Event&>(event);
82     SysEvent& sysEvent = static_cast<SysEvent&>(eventRef);
83     ProcessHiCollieEvent(sysEvent);
84 }
85 
ProcessHiCollieEvent(SysEvent & sysEvent)86 void HiCollieCollector::ProcessHiCollieEvent(SysEvent &sysEvent)
87 {
88     std::string path = "";
89     std::string info = sysEvent.GetEventValue(EventStore::EventCol::INFO);
90     std::regex reg("logPath:([^,]+)");
91     std::smatch result;
92     if (std::regex_search(info, result, reg)) {
93         path = result[1].str();
94         std::string desPath = FAULT_LOG_PATH + FileUtil::ExtractFileName(path);
95         int fileResult = FileUtil::CopyFile(path, desPath);
96         if (fileResult != 0) {
97             HIVIEW_LOGE("failed to copy file from %{public}s to %{public}s.\n", path.c_str(), desPath.c_str());
98         }
99     }
100 
101     std::vector<std::string> paths = {path};
102     HiSysEvent::Write("RELIABILITY", sysEvent.eventName_ + "_REPORT", HiSysEvent::FAULT,
103         "HIVIEW_LOG_FILE_PATHS", paths,
104         "PID", sysEvent.GetEventValue(EVENT_PID),
105         "TGID", sysEvent.GetEventValue(EVENT_TGID),
106         "MSG", sysEvent.GetEventValue(EVENT_MSG));
107     HIVIEW_LOGI("send event [%{public}s, %{public}s] msg:%{public}s path:%{public}s",
108         "RELIABILITY", sysEvent.eventName_.c_str(), sysEvent.GetEventValue(EVENT_MSG).c_str(), path.c_str());
109 }
110 } // namespace HiviewDFX
111 } // namespace OHOS
112