# HiSysEvent Listening ## Overview 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 event for power usage analysis. ## Available APIs **Table 1** HiSysEvent listener APIs

API

Description

int HiSysEventManager::AddEventListener(std::shared_ptr<HiSysEventSubscribeCallBackBase> listener, std::vector<struct ListenerRule>& rules)

Registers a listener for system events. You can listen for certain events by specifying rules.

Input arguments:

  • listener: callback object for system events.
  • rules: rules for event listening.

Return values

  • 0: Repeated registration is successful.
  • 1: Initial registration is successful.
  • Other values: Registration has failed.

void HiSysEventManager::RemoveListener(std::shared_ptr<HiSysEventSubscribeCallBackBase> listener)

Removes the listener for system events.

Input arguments:

  • listener: callback object for system events.

Return value: none

**Table 2** HiSysEvent listener rules

Attribute

Description

uint32_t ruleType

Rule type. The matching scope includes domain and eventName. The value can be any of the following:

  • 1: whole word matching.
  • 2: prefix matching.
  • 3: regular expression matching.
  • Other values: invalid matching mode.

std::string domain

  • domain: domain to which the event belongs. By default, an empty string indicates that the domain is successfully matched.

std::string eventName

  • eventName: event name. By default, an empty string indicates that the event name is successfully matched.
**Table 3** HiSysEvent callback object

API

Description

void HiSysEventSubscribeCallBackBase::OnHandle(const std::string& domain, const std::string& eventName, const int eventType, const std::string& eventDetail)

Callback object for system events.

Input arguments:

  • domain: domain to which the event belongs.
  • eventName: event name.
  • eventType: event type.
  • eventDetail: JSON string containing event information.

Return value: none

## How to Develop ### **C++** In this example, you'll be instructed to register a listener for all system events that belong to the **HIVIEWDFX** domain. 1. Develop the source code. - Import the corresponding header file: hisysevent\_manager.h - Implement the callback API. HiSysEventSubscribeCallBackBase::OnHandle\(const std::string& domain, const std::string& eventName, const int eventType, const std::string& eventDetail\) - Register a callback object. HiSysEventManager::AddEventListener\(std::shared\_ptr listener, std::vector& rules\) ``` // Register a listener for all system events that belong to the HIVIEWDFX domain. #include "hisysevent_manager.h" #include namespace OHOS { namespace HiviewDFX { // Implement the API for registering a listener for callback objects. void HiSysEventToolListener::OnHandle(const std::string& domain, const std::string& eventName, const int eventType, const std::string& eventDetail) { std::cout << eventDetail << std::endl; } void HiSysEventToolListener::OnServiceDied() { std::cout << std::string("service disconnect, exit") << std::endl; exit(0); } } // namespace HiviewDFX } // namespace OHOS // Register a listener for callback objects. auto toolListener = std::make_shared(); struct ListenerRule rule; rule.ruleType = 1; // 1: default type rule.domain = "HIVIEWDFX"; std::vector sysRules; sysRules.push_back(rule); HiSysEventManager::AddEventListener(toolListener, sysRules); ``` 2. Modify 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", ] ```