HiSysEvent Query

Overview

HiSysEvent provides an API for you to query system events. You can query concerned events by specifying search criteria. For example, for a power consumption module, you can query required system events for analysis.

Development Guidelines

Available APIs

Table 1 Description of the HiSysEvent query API

Name Description
bool HiSysEventManager::QueryHiSysEvent(struct QueryArg& queryArg, std::vector<QueryRule>& queryRules, std::shared_ptr<HiSysEventQueryCallBack> queryCallBack) Queries system events by specifying search criteria such as the time segment, event domain, and event name.

Input arguments:
  • queryArg: event query parameter.
  • queryRules: event filtering rules.
  • queryRules: callback object for query results.
Return value:
  • true: The query is successful.
  • false: The query has failed.

Table 2 Description of QueryArg

Attribute Description
beginTime Start time, in the long long int format.
endTime End time, in the long long int format.
maxEvents Maximum number of returned events, in the int format.

Table 3 Description of QueryRule

API Description
QueryRule(const std::string& domain, const std::vector<std::string>& eventList) Constructor used to create a QueryRule object.

Input arguments:
  • **domain: domain to which the event of the QueryRule object belongs, in the string format. By default, an empty string indicates that the domain is successfully matched.
  • eventList: event name list, in the std::vector<std::string> format. By default, an empty string indicates that the event names on the list are successfully matched.

Table 4 Description of HiSysEventQueryCallBack

API Description
void HiSysEventQueryCallBack::OnQuery(const ::std::vector<std::string>& sysEvent, const ::std::vector<int64_t>& seq) Callback object for event query.

Input arguments:
  • sysEvent: event set.
  • seq: event sequence set.
Return value:
None.
void HiSysEventQueryCallBack::OnComplete(int32_t reason, int32_t total) Callback object for completion of event query.

Input arguments:
  • reason: reason for completion of event query. The default value is 0.
  • total: total number of events returned in this query.
Return value:
None.

Development Example

C++

  1. Develop the source code.

    • Import the corresponding header file:

      hisysevent_manager.h

    • Implement the callback API.

      void HiSysEventQueryCallBack::OnQuery(const ::std::vector<std::string>& sysEvent, const ::std::vector<int64_t>& seq)

      void HiSysEventQueryCallBack::OnComplete(int32_t reason, int32_t total)

    • Invoke the query API in the corresponding service logic.

      HiSysEventManager::QueryHiSysEvent(struct QueryArg& queryArg, std::vector<QueryRule>& queryRules, std::shared_ptr<HiSysEventQueryCallBack> queryCallBack)

    // In this example, you'll query all system events.
    #include "hisysevent_manager.h"
    #include <iostream>
    
    namespace OHOS {
    namespace HiviewDFX {
    // Implement the query callback API.
    void HiSysEventToolQuery::OnQuery(const ::std::vector<std::string>& sysEvent,
        const ::std::vector<int64_t>& seq)
    {
        for_each(sysEvent.cbegin(), sysEvent.cend(), [](const std::string &tmp) {
            std::cout << tmp << std::endl;
        });
    }
    
    void HiSysEventToolQuery::OnComplete(int32_t reason, int32_t total)
    {
        return;
    }
    } // namespace HiviewDFX
    } // namespace OHOS
    
    // Invoke the query callback API to obtain system events.
    std::shared_ptr<HiSysEventToolQuery> queryCallBack = nullptr;
    try {
        queryCallBack = std::make_shared<HiSysEventToolQuery>();
    } catch(...) {
        // Catch exception thrown by make_shared
    }
    if (queryCallBack != nullptr) {
        struct QueryArg args(clientCmdArg.beginTime, clientCmdArg.endTime, clientCmdArg.maxEvents);
        std::vector<QueryRule> rules;
        HiSysEventManager::QueryHiSysEvent(args, rules, queryCallBack);
    }
    
  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",  ]