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 "sysevent_source.h"
17
18 #include <memory>
19
20 #include "defines.h"
21 #include "logger.h"
22 #include "plugin_factory.h"
23 #include "sys_event.h"
24
25 namespace OHOS {
26 namespace HiviewDFX {
27 REGISTER(SysEventSource);
28 DEFINE_LOG_TAG("HiView-SysEventSource");
29
Parser(const std::string & rawMsg) const30 std::shared_ptr<PipelineEvent> SysEventParser::Parser(const std::string& rawMsg) const
31 {
32 HIVIEW_LOGD("parser raw message size=%{public}d, %{public}s", rawMsg.length(), rawMsg.c_str());
33 auto baseEvent = std::make_shared<SysEvent>("SysEventSource", pipeProducer, rawMsg);
34 if (baseEvent->PaserJson() != 0) {
35 HIVIEW_LOGI("parser sys event error");
36 return nullptr;
37 }
38 HIVIEW_LOGI("parser result domain_=%{public}s eventName_=%{public}s",
39 baseEvent->domain_.c_str(), baseEvent->eventName_.c_str());
40 return baseEvent;
41 }
42
HandlerEvent(const std::string & rawMsg)43 void SysEventReceiver::HandlerEvent(const std::string& rawMsg)
44 {
45 SysEventParser sysEventParser(static_cast<PipelineEventProducer *>(&eventSource));
46 std::shared_ptr<PipelineEvent> event = sysEventParser.Parser(rawMsg);
47 eventSource.PublishPipelineEvent(event);
48 }
49
OnLoad()50 void SysEventSource::OnLoad()
51 {
52 HIVIEW_LOGI("SysEventSource load ");
53 }
54
OnUnload()55 void SysEventSource::OnUnload()
56 {
57 eventServer.Stop();
58 HIVIEW_LOGI("SysEventSource unload");
59 }
60
StartEventSource()61 void SysEventSource::StartEventSource()
62 {
63 HIVIEW_LOGI("SysEventSource start");
64 std::shared_ptr<EventReceiver> sysEventReceiver = std::make_shared<SysEventReceiver>(*this);
65 eventServer.AddReceiver(sysEventReceiver);
66 eventServer.Start();
67 }
68
Recycle(PipelineEvent * event __UNUSED)69 void SysEventSource::Recycle(PipelineEvent *event __UNUSED)
70 {
71 HIVIEW_LOGI("recycle resource");
72 }
73
PauseDispatch(std::weak_ptr<Plugin> plugin)74 void SysEventSource::PauseDispatch(std::weak_ptr<Plugin> plugin)
75 {
76 auto requester = plugin.lock();
77 if (requester != nullptr) {
78 HIVIEW_LOGI("process pause dispatch event from plugin:%s.\n", requester->GetName().c_str());
79 }
80 }
81 } // namespace HiviewDFX
82 } // namespace OHOS