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:
  • listener: callback object for system events.
  • rules: rules for event listening.
Return value:
  • 0: Repeated registration is successful.
  • 1: Initial registration is successful.
  • Other values: Registration has failed.
bool HiSysEventManager::RemoveListener(std::shared_ptr<HiSysEventSubscribeCallBack> listener) Removes the listener for system events.

Input arguments:
  • listener: callback object for system events.
Return value:
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:
  • tag: indicates the event tag for the ListenerRule object. The value is a string of 1 to 16 characters, including uppercase letters, lowercase letters, and digits.
  • ruleType: indicates the type of the ListenerRule object. The value is an enum defined by RuleType.
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:
  • domain: indicates the event domain for the ListenerRule object. The value is a string of 1 to 16 characters, including uppercase letters, digits, and underscores (_).
  • eventName: indicates the event name for the ListenerRule object. The value is a string of 1 to 32 characters, including uppercase letters, digits, and underscores (_).
  • ruleType: indicates the type of the ListenerRule object. The value is an enum defined by RuleType.
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:
  • tag: indicates the event tag for the ListenerRule object. The value is a string of 1 to 16 characters, including uppercase letters, lowercase letters, and digits.
  • domain: indicates the event domain for the ListenerRule object. The value is a string of 1 to 16 characters, including uppercase letters, digits, and underscores (_).
  • eventName: indicates the event name for the ListenerRule object. The value is a string of 1 to 32 characters, including uppercase letters, digits, and underscores (_).
  • ruleType: indicates the type of the ListenerRule object. The value is an enum defined by RuleType.

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:
  • domain: indicates the domain to which the event belongs.
  • eventName: indicates the event name.
  • eventType: indicates the event type.
  • eventDetail: indicates the event information, in JSON format.
Return value:
None.

Development Example

C++

  1. 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);
    }
    
  2. 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", ]