1# HiSysEvent订阅指导<a name="ZH-CN_TOPIC_0000001185655868"></a> 2 3 4## 概述<a name="section315316685112"></a> 5 6### 功能简介<a name="section123181433335224"></a> 7 8HiSysEvent提供了跨进程订阅机制,开发者可以通过注册订阅接口实时获取关注的事件,例如电池模块侦听功耗相关的事件,用于分析耗电情况。 9 10### 约束与限制<a name="section123181433375224"></a> 11 12**HiSysEvent事件订阅条件约束:** 13 14- 在订阅HiSysEvent事件之前,需要先完成HiSysEvent打点配置,具体配置方法请参考[《HiSysEvent打点配置指导》](subsys-dfx-hisysevent-logging-config.md)。 15 16## 开发指导<a name="section315316685113"></a> 17 18### 接口说明<a name="section0342191810519"></a> 19 20**表 1** HiSysEvent订阅接口 21 22| 接口名称 | 描述 | 23| -------- | --------- | 24|bool HiSysEventManager::AddEventListener(std::shared_ptr<HiSysEventSubscribeCallBack> listener, std::vector<ListenerRule>& rules)|接口功能:注册订阅HiSysEvent系统事件侦听对象,可设置规则订阅某些事件。<br><br>输入参数:<ul><li>listener:订阅回调对象。</li><li>rules:事件订阅规则。</li></ul>返回值:<ul><li>0:订阅成功,重复订阅。</li><li>1:订阅成功,初次订阅。</li><li>其他值:订阅失败。</li></ul>| 25|bool HiSysEventManager::RemoveListener(std::shared_ptr<HiSysEventSubscribeCallBack> listener)|接口功能:移除订阅hisysevent系统事件侦听对象。<br><br>输入参数:<ul><li>listener:订阅回调对象。</ul>返回值:<br>  无。| 26 27**表 2** ListenerRule订阅规则对象 28 29| 接口名称 | 描述 | 30| -------- | --------- | 31|ListenerRule(const std::string& tag, RuleType ruleType = RuleType::WHOLE_WORD)|接口功能:订阅规则构造函数,创建事件标签订阅规则对象。<br><br>输入参数:<ul><li>tag:订阅规则的HisysEvent事件标签,字符串类型,最大长度16个字符(含),有效字符包含大小写字母及数字。</li><li>ruleType:订阅规则的规则类型,RuleType枚举类型(参考表3)。</li></ul>| 32|ListenerRule(const std::string& domain, const std::string& eventName, RuleType ruleType = RuleType::WHOLE_WORD)|接口功能:订阅规则构造函数,创建事件领域与事件名称订阅规则对象。<br><br>输入参数:<ul><li>domain:订阅规则的HisysEvent事件领域,字符串类型,最大长度16个字符(含),有效字符包含大写字母、数字及下划线。</li><li>eventName:订阅规则的HisysEvent事件名称,字符串类型,最大长度32个字符(含),有效字符包含大写字母、数字及下划线。</li><li>ruleType:订阅规则的规则类型,RuleType枚举类型(参考表3)。</li></ul>| 33|ListenerRule(const std::string& domain, const std::string& eventName, const std::string& tag, RuleType ruleType = RuleType::WHOLE_WORD)|接口功能:订阅规则构造函数,创建事件领域、事件名称,事件标签订阅规则对象。<br><br>输入参数:<ul><li>tag:订阅规则的HisysEvent事件标签,字符串类型,最大长度16个字符(含),有效字符包含大小写字母及数字。</li><li>domain:订阅规则的HisysEvent事件领域,字符串类型,最大长度16个字符(含),有效字符包含大写字母、数字及下划线。</li><li>eventName:订阅规则的HisysEvent事件名称,字符串类型,最大长度32个字符(含),有效字符包含大写字母、数字及下划线。</li><li>ruleType:订阅规则的规则类型,RuleType枚举类型(参考表3)。</li></ul>| 34 35**表 3** RuleType类型 36 37| 枚举值 | 描述 | 38| ------------ | ------------- | 39| WHOLE_WORD | 全词匹配类型 | 40| PREFIX | 前缀匹配类型 | 41| REGULAR | 正则匹配类型 | 42 43**表 4** HiSysEventSubscribeCallBack订阅对象 44 45| 接口名称 | 描述 | 46| -------- | --------- | 47|void HiSysEventSubscribeCallBack::OnHandle(const std::string& domain, const std::string& eventName, const int eventType, const std::string& eventDetail)|接口功能:订阅事件的回调接口。<br><br>输入参数:<ul><li>domain:事件所属领域。</li><li>eventName:事件的名称。</li><li>eventType:事件类型。</li><li>eventDetail:包含事件相关信息的字符串,以json的形式体现。</li></ul>返回值:<br>  无。| 48 49### 开发实例<a name="section123181432175110"></a> 50 51订阅HiSysEvent事件C++接口实例 52 531. 源代码开发 54 55 自定义订阅回调实现类头文件DemoListener.h: 56 57 ``` 58 #ifndef DEMO_LISTENER_H 59 #define DEMO_LISTENER_H 60 61 #include "hisysevent_subscribe_callback.h" 62 63 #include <string> 64 65 class DemoListener : public OHOS::HiviewDFX::HiSysEventSubscribeCallBack { 66 public: 67 explicit DemoListener() : HiSysEventSubscribeCallBack() {} 68 void OnHandle(const std::string& domain, const std::string& eventName, const int eventType, 69 const std::string& eventDetail); 70 virtual ~DemoListener() {} 71 void OnServiceDied(); 72 }; 73 74 #endif // DEMO_LISTENER_H 75 ``` 76 77 增加DemoListener.cpp文件,在DemoListener类中根据实际需求自定义订阅回调接口的实现逻辑: 78 79 ``` 80 #include "demo_listener.h" 81 82 #include <iostream> 83 84 void DemoListener::OnHandle(const std::string& domain, const std::string& eventName, 85 const int eventType, const std::string& eventDetail) 86 { 87 std::cout << eventDetail << std::endl; 88 } 89 90 void DemoListener::OnServiceDied() 91 { 92 std::cout << std::string("service disconnect, exit") << std::endl; 93 exit(0); 94 } 95 ``` 96 97 通过HiSysEventManager类提供的AddEventListener接口注册回调对象,完成对HiSysEvent的订阅: 98 99 ``` 100 std::shared_ptr<DemoListener> demoListener = nullptr; 101 try { 102 demoListener = std::make_shared<DemoListener>(); 103 } catch(...) { 104 // 智能指针获取失败异常处理 105 } 106 if (demoListener != nullptr) { 107 // 事件标签规则订阅,规则类型为默认的全词匹配类型 108 ListenerRule tagRule("dfx"); 109 // 事件标签规则订阅,规则类型为正则匹配类型 110 ListenerRule regRule("dfx.*", RuleType::REGULAR); 111 // 事件领域及事件名称规则订阅,规则类型为前缀匹配类型 112 ListenerRule domainNameRule("HIVIEWDFX", "APP_USAGE", RuleType::PREFIX); 113 std::vector<ListenerRule> sysRules; 114 sysRules.push_back(tagRule); 115 sysRules.push_back(regRule); 116 sysRules.push_back(domainNameRule); 117 HiSysEventManager::AddEventListener(demoListener, sysRules); 118 } 119 ``` 120 1212. 编译配置 122 123 在BUILD.gn编译文件中,需要添加依赖hisysevent\_native组件的libhisyseventmanager库: 124 125 ``` 126 external_deps = [ "hisysevent_native:libhisyseventmanager", ] 127 ``` 128