• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 "sys_event_service.h"
17 
18 #include <cstdio>
19 #include <memory>
20 
21 #include "event.h"
22 #include "hiview_global.h"
23 #include "logger.h"
24 #include "plugin_factory.h"
25 #include "sys_event.h"
26 #include "sys_event_db_mgr.h"
27 
28 namespace OHOS {
29 namespace HiviewDFX {
30 REGISTER(SysEventService);
31 DEFINE_LOG_TAG("HiView-SysEventService");
SysEventService()32 SysEventService::SysEventService() : hasLoaded_(false)
33 {
34     sysEventDbMgr_ = std::make_unique<SysEventDbMgr>();
35     sysEventStat_ = std::make_unique<SysEventStat>();
36 }
37 
~SysEventService()38 SysEventService::~SysEventService() {}
39 
OnLoad()40 void SysEventService::OnLoad()
41 {
42     HIVIEW_LOGI("sys event service load");
43     auto notifyFunc = [&] (std::shared_ptr<Event> event) -> void {
44         SysEventService& servicePlugin = *this;
45         servicePlugin.SendEvent(event);
46     };
47     SysEventServiceAdapter::StartService(this, notifyFunc);
48     sysEventDbMgr_->StartCheckStoreTask(this->workLoop_);
49     std::string yamlFile =
50         HiviewGlobal::GetInstance()->GetHiViewDirectory(HiviewContext::DirectoryType::CONFIG_DIRECTORY);
51     yamlFile = (yamlFile.back() != '/') ? (yamlFile + "/hisysevent.def") : (yamlFile + "hisysevent.def");
52     HIVIEW_LOGI("yamlFile path is %{public}s", yamlFile.c_str());
53     sysEventParser_ = std::make_unique<EventJsonParser>(yamlFile);
54     auto getTagFunc = std::bind(&EventJsonParser::GetTagByDomainAndName, *(sysEventParser_.get()),
55         std::placeholders::_1, std::placeholders::_2);
56     SysEventServiceAdapter::BindGetTagFunc(getTagFunc);
57     auto getTypeFunc = std::bind(&EventJsonParser::GetTypeByDomainAndName, *(sysEventParser_.get()),
58         std::placeholders::_1, std::placeholders::_2);
59     SysEventServiceAdapter::BindGetTypeFunc(getTypeFunc);
60     hasLoaded_ = true;
61 }
62 
SendEvent(std::shared_ptr<Event> & event)63 void SysEventService::SendEvent(std::shared_ptr<Event>& event)
64 {
65     GetHiviewContext()->PostUnorderedEvent(shared_from_this(), event);
66 }
67 
OnUnload()68 void SysEventService::OnUnload()
69 {
70     HIVIEW_LOGI("sys event service unload");
71 }
72 
Convert2SysEvent(std::shared_ptr<Event> & event)73 std::shared_ptr<SysEvent> SysEventService::Convert2SysEvent(std::shared_ptr<Event>& event)
74 {
75     if (event == nullptr) {
76         HIVIEW_LOGE("event is null");
77         return nullptr;
78     }
79     if (event->messageType_ != Event::MessageType::SYS_EVENT) {
80         HIVIEW_LOGE("receive out of sys event type");
81         return nullptr;
82     }
83     HIVIEW_LOGI("domain is %{public}s, eventName is %{public}s.", event->domain_.c_str(), event->eventName_.c_str());
84     std::shared_ptr<SysEvent> sysEvent = Event::DownCastTo<SysEvent>(event);
85     if (sysEvent == nullptr) {
86         HIVIEW_LOGE("sysevent is null");
87     }
88     return sysEvent;
89 }
90 
OnEvent(std::shared_ptr<Event> & event)91 bool SysEventService::OnEvent(std::shared_ptr<Event>& event)
92 {
93     if (!hasLoaded_) {
94         HIVIEW_LOGE("SysEventService not ready");
95         return false;
96     }
97     std::shared_ptr<SysEvent> sysEvent = Convert2SysEvent(event);
98     if (sysEvent == nullptr) {
99         sysEventStat_->AccumulateEvent(false);
100         return false;
101     }
102 
103     if (!sysEventParser_->HandleEventJson(sysEvent)) {
104         HIVIEW_LOGI("HandleEventJson fail");
105         sysEventStat_->AccumulateEvent(sysEvent->domain_, sysEvent->eventName_, false);
106         return false;
107     } else {
108         sysEventStat_->AccumulateEvent(sysEvent->domain_, sysEvent->eventName_);
109     }
110     SysEventServiceAdapter::OnSysEvent(sysEvent);
111     sysEventDbMgr_->SaveToStore(sysEvent);
112     return true;
113 }
114 
ShowUsage(int fd,const std::vector<std::string> & cmds)115 static void ShowUsage(int fd, const std::vector<std::string>& cmds)
116 {
117     dprintf(fd, "invalid cmd:");
118     for (auto it = cmds.begin(); it != cmds.end(); it++) {
119         dprintf(fd, "%s ", it->c_str());
120     }
121     dprintf(fd, "\n");
122     dprintf(fd, "usage: SysEventService [sum|detail|invalid|clear]\n");
123 }
124 
Dump(int fd,const std::vector<std::string> & cmds)125 void SysEventService::Dump(int fd, const std::vector<std::string>& cmds)
126 {
127     if (cmds.size() >= 2) { // args from the second item
128         std::string arg1 = cmds[1];
129         if (arg1 == "sum") {
130             sysEventStat_->StatSummary(fd);
131         } else if (arg1 == "detail") {
132             sysEventStat_->StatDetail(fd);
133         } else if (arg1 == "invalid") {
134             sysEventStat_->StatInvalidDetail(fd);
135         } else if (arg1 == "clear") {
136             sysEventStat_->Clear(fd);
137         } else {
138             ShowUsage(fd, cmds);
139         }
140     } else {
141         sysEventStat_->StatSummary(fd);
142     }
143 }
144 } // namespace HiviewDFX
145 } // namespace OHOS
146