From d33e91dd554f1c9860be2c2623552da776be71c3 Mon Sep 17 00:00:00 2001 From: xuyong Date: Mon, 28 Mar 2022 16:17:39 +0800 Subject: [PATCH] =?UTF-8?q?make=5Fshared=E4=BD=BF=E7=94=A8=E8=A7=84?= =?UTF-8?q?=E8=8C=83=E8=AF=B4=E6=98=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: xuyong --- .../subsys-dfx-hisysevent-listening.md | 31 ++++++++++++------- .../subsystems/subsys-dfx-hisysevent-query.md | 15 ++++++--- .../subsys-dfx-hisysevent-listening.md | 31 ++++++++++++------- .../subsystems/subsys-dfx-hisysevent-query.md | 15 ++++++--- 4 files changed, 60 insertions(+), 32 deletions(-) diff --git a/en/device-dev/subsystems/subsys-dfx-hisysevent-listening.md b/en/device-dev/subsystems/subsys-dfx-hisysevent-listening.md index 5df286d267..445c43acaf 100644 --- a/en/device-dev/subsystems/subsys-dfx-hisysevent-listening.md +++ b/en/device-dev/subsystems/subsys-dfx-hisysevent-listening.md @@ -101,18 +101,25 @@ C++ Call the **AddEventListener** API of the **HiSysEventManager** class to add a listener for system events. ``` - auto demoListener = std::make_shared(); - // 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 sysRules; - sysRules.push_back(tagRule); - sysRules.push_back(regRule); - sysRules.push_back(domainNameRule); - HiSysEventManager::AddEventListener(demoListener, sysRules); + std::shared_ptr demoListener = nullptr; + try { + demoListener = std::make_shared(); + } 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 sysRules; + sysRules.push_back(tagRule); + sysRules.push_back(regRule); + sysRules.push_back(domainNameRule); + HiSysEventManager::AddEventListener(demoListener, sysRules); + } ``` 2. Configure the **BUILD.gn** file. diff --git a/en/device-dev/subsystems/subsys-dfx-hisysevent-query.md b/en/device-dev/subsystems/subsys-dfx-hisysevent-query.md index 8401862dae..f6c0723081 100644 --- a/en/device-dev/subsystems/subsys-dfx-hisysevent-query.md +++ b/en/device-dev/subsystems/subsys-dfx-hisysevent-query.md @@ -86,10 +86,17 @@ C++ } // namespace OHOS // Invoke the query callback API to obtain system events. - auto queryCallBack = std::make_shared(); - struct QueryArg args(clientCmdArg.beginTime, clientCmdArg.endTime, clientCmdArg.maxEvents); - std::vector mRules; - HiSysEventManager::QueryHiSysEvent(args, mRules, queryCallBack); + std::shared_ptr queryCallBack = nullptr; + try { + queryCallBack = std::make_shared(); + } catch(...) { + // Catch exception thrown by make_shared + } + if (queryCallBack != nullptr) { + struct QueryArg args(clientCmdArg.beginTime, clientCmdArg.endTime, clientCmdArg.maxEvents); + std::vector rules; + HiSysEventManager::QueryHiSysEvent(args, rules, queryCallBack); + } ``` 2. Modify the **BUILD.gn** file. diff --git a/zh-cn/device-dev/subsystems/subsys-dfx-hisysevent-listening.md b/zh-cn/device-dev/subsystems/subsys-dfx-hisysevent-listening.md index 01bb60ea61..81785b1a3c 100644 --- a/zh-cn/device-dev/subsystems/subsys-dfx-hisysevent-listening.md +++ b/zh-cn/device-dev/subsystems/subsys-dfx-hisysevent-listening.md @@ -103,18 +103,25 @@ HiSysEvent提供了跨进程订阅机制,开发者可以通过注册订阅接 通过HiSysEventManager类提供的AddEventListener接口注册回调对象,完成对HiSysEvent的订阅: ``` - auto demoListener = std::make_shared(); - // 事件标签规则订阅,规则类型为默认的全词匹配类型 - ListenerRule tagRule("dfx"); - // 事件标签规则订阅,规则类型为正则匹配类型 - ListenerRule regRule("dfx.*", RuleType::REGULAR); - // 事件领域及事件名称规则订阅,规则类型为前缀匹配类型 - ListenerRule domainNameRule("HIVIEWDFX", "APP_USAGE", RuleType::PREFIX); - std::vector sysRules; - sysRules.push_back(tagRule); - sysRules.push_back(regRule); - sysRules.push_back(domainNameRule); - HiSysEventManager::AddEventListener(demoListener, sysRules); + std::shared_ptr demoListener = nullptr; + try { + demoListener = std::make_shared(); + } catch(...) { + // 智能指针获取失败异常处理 + } + if (demoListener != nullptr) { + // 事件标签规则订阅,规则类型为默认的全词匹配类型 + ListenerRule tagRule("dfx"); + // 事件标签规则订阅,规则类型为正则匹配类型 + ListenerRule regRule("dfx.*", RuleType::REGULAR); + // 事件领域及事件名称规则订阅,规则类型为前缀匹配类型 + ListenerRule domainNameRule("HIVIEWDFX", "APP_USAGE", RuleType::PREFIX); + std::vector sysRules; + sysRules.push_back(tagRule); + sysRules.push_back(regRule); + sysRules.push_back(domainNameRule); + HiSysEventManager::AddEventListener(demoListener, sysRules); + } ``` 2. 编译配置 diff --git a/zh-cn/device-dev/subsystems/subsys-dfx-hisysevent-query.md b/zh-cn/device-dev/subsystems/subsys-dfx-hisysevent-query.md index 60ffefa6e2..d467ec4e62 100644 --- a/zh-cn/device-dev/subsystems/subsys-dfx-hisysevent-query.md +++ b/zh-cn/device-dev/subsystems/subsys-dfx-hisysevent-query.md @@ -86,10 +86,17 @@ C++接口实例。 } // namespace OHOS // 调用查询接口获取HiSysEvent事件 - auto queryCallBack = std::make_shared(); - struct QueryArg args(clientCmdArg.beginTime, clientCmdArg.endTime, clientCmdArg.maxEvents); - std::vector mRules; - HiSysEventManager::QueryHiSysEvent(args, mRules, queryCallBack); + std::shared_ptr queryCallBack = nullptr; + try { + queryCallBack = std::make_shared(); + } catch (...) { + // 智能指针获取失败处理 + } + if (queryCallBack != nullptr) { + struct QueryArg args(clientCmdArg.beginTime, clientCmdArg.endTime, clientCmdArg.maxEvents); + std::vector rules; + HiSysEventManager::QueryHiSysEvent(args, rules, queryCallBack); + } ``` 2. 编译设置: -- GitLab