HiSysEvent Listening
Overview
Introduction
HiSysEvent supports listening for events across processes. You can register a listener to listen for concerned events on a real-time basis. For example, you can enable the battery module to listen for power consumption events for power usage analysis.
Constraints
Before subscribing to system events, you need to configure HiSysEvent logging. For details, see HiSysEvent Logging Configuration.
Development Guidelines
Available APIs
Table 1 Description of EventListener APIs
Name | Description |
---|---|
bool HiSysEventManager::AddEventListener(std::shared_ptr<HiSysEventSubscribeCallBack> listener, std::vector<ListenerRule>& rules) | Registers a listener for system events. You can listen for certain events by specifying rules. Input arguments:
|
bool HiSysEventManager::RemoveListener(std::shared_ptr<HiSysEventSubscribeCallBack> listener) | Removes the listener for system events. Input arguments:
None. |
Table 2 Description of ListenerRule
API | Description |
---|---|
ListenerRule(const std::string& tag, RuleType ruleType = RuleType::WHOLE_WORD) | Constructor used to create a ListenerRule object based on the event tag. Input arguments:
|
ListenerRule(const std::string& domain, const std::string& eventName, RuleType ruleType = RuleType::WHOLE_WORD) | Constructor used to create a ListenerRule object based on the event domain and event name. Input arguments:
|
ListenerRule(const std::string& domain, const std::string& eventName, const std::string& tag, RuleType ruleType = RuleType::WHOLE_WORD) | Constructor used to create a ListenerRule object based on the event domain, event name, and event tag. Input arguments:
|
Table 3 Description of RuleType
Value | Description |
---|---|
WHOLE_WORD | Whole word matching |
PREFIX | Prefix matching |
REGULAR | Regular expression matching |
Table 4 Description of HiSysEventSubscribeCallBack
API | Description |
---|---|
void HiSysEventSubscribeCallBack::OnHandle(const std::string& domain, const std::string& eventName, const int eventType, const std::string& eventDetail) | Provides the callback of system events. Input arguments:
None. |
Development Example
C++
-
Develop the source code.
Import the DemoListener.h header file, which contains the DemoListener class for implementing the custom event callback.
#ifndef DEMO_LISTENER_H #define DEMO_LISTENER_H #include "hisysevent_subscribe_callback.h" #include <string> class DemoListener : public OHOS::HiviewDFX::HiSysEventSubscribeCallBack { public: explicit DemoListener() : HiSysEventSubscribeCallBack() {} void OnHandle(const std::string& domain, const std::string& eventName, const int eventType, const std::string& eventDetail); virtual ~DemoListener() {} void OnServiceDied(); }; #endif // DEMO_LISTENER_H
Create the DemoListener.cpp file, and add the implementation logic of the custom event callback API in the DemoListener class.
#include "demo_listener.h" #include <iostream> void DemoListener::OnHandle(const std::string& domain, const std::string& eventName, const int eventType, const std::string& eventDetail) { std::cout << eventDetail << std::endl; } void DemoListener::OnServiceDied() { std::cout << std::string("service disconnect, exit") << std::endl; exit(0); }
Call the AddEventListener API of the HiSysEventManager class to add a listener for system events.
std::shared_ptr<DemoListener> demoListener = nullptr; try { demoListener = std::make_shared<DemoListener>(); } catch(...) { // Catch exception thrown by make_shared } if (demoListener != nullptr) { // Add a ListenerRule object based on the event tag, with RuleType left unspecified (in this case, ruleType is defaulted to WHOLE_WORD). ListenerRule tagRule("dfx"); // Add a ListenerRule object based on the event tag, with RuleType set as REGULAR. ListenerRule regRule("dfx.*", RuleType::REGULAR); // Add a ListenerRule object based on the event domain and event name, with RuleType set as PREFIX. ListenerRule domainNameRule("HIVIEWDFX", "APP_USAGE", RuleType::PREFIX); std::vector<ListenerRule> sysRules; sysRules.push_back(tagRule); sysRules.push_back(regRule); sysRules.push_back(domainNameRule); HiSysEventManager::AddEventListener(demoListener, sysRules); }
-
Configure the BUILD.gn file.
In the BUILD.gn file, add the libhisyseventmanager library that depends on the hisysevent_native component.
external_deps = [ "hisysevent_native:libhisyseventmanager", ]