1e41f4b71Sopenharmony_ci# HiAppEvent Data Processor Library
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ci
4e41f4b71Sopenharmony_ci## Overview
5e41f4b71Sopenharmony_ci
6e41f4b71Sopenharmony_ciHiAppEvent implements the logging function for OpenHarmony applications. The data processor library provides the event processing capability for HiAppEvent. It needs to be provided by the data processor during device development.
7e41f4b71Sopenharmony_ci
8e41f4b71Sopenharmony_ciThe following figure shows the service flowchart of the HiAppEvent data processor library.
9e41f4b71Sopenharmony_ci
10e41f4b71Sopenharmony_ci  **Figure 1** Service flowchart of the HiAppEvent data processor library
11e41f4b71Sopenharmony_ci
12e41f4b71Sopenharmony_ci  ![Service flowchart](../subsystems/figures/hiappevent_extend_so_stream.png)
13e41f4b71Sopenharmony_ci
14e41f4b71Sopenharmony_ci## Usage Scenarios
15e41f4b71Sopenharmony_ci
16e41f4b71Sopenharmony_ciAfter a data processor is added by using HiAppEvent APIs, an application event, once being submitted, will be distributed to the event handler. The event handler then performs proper processing, for example, reporting the event.
17e41f4b71Sopenharmony_ci
18e41f4b71Sopenharmony_ci## Function Description
19e41f4b71Sopenharmony_ci
20e41f4b71Sopenharmony_ciThe HiAppEvent data processor library provides the event processing capability for the HiAppEvent event handler. Its functions are as follows:
21e41f4b71Sopenharmony_ci
22e41f4b71Sopenharmony_ci- Initial registration: provides the service logic for initializing the `.so` file when the data processor library is loaded and registers the data processor with HiAppEvent. The following figure shows the initial registration flowchart.
23e41f4b71Sopenharmony_ci
24e41f4b71Sopenharmony_ci  **Figure 2** Initial registration flowchart
25e41f4b71Sopenharmony_ci
26e41f4b71Sopenharmony_ci  ![Initial registration](../subsystems/figures/hiappevent_extend_so_inited.png)
27e41f4b71Sopenharmony_ci
28e41f4b71Sopenharmony_ci
29e41f4b71Sopenharmony_ci- Event reporting: provides APIs related to logging and reporting of application events. The following figure shows the event reporting flowchart.
30e41f4b71Sopenharmony_ci
31e41f4b71Sopenharmony_ci  **Figure 3** Event reporting flowchart
32e41f4b71Sopenharmony_ci
33e41f4b71Sopenharmony_ci  ![Event reporting](../subsystems/figures/hiappevent_extend_so_report.png)
34e41f4b71Sopenharmony_ci
35e41f4b71Sopenharmony_ci- User ID verification: provides APIs for verifying user IDs. See Figure 4 for the user ID verification flowchart.
36e41f4b71Sopenharmony_ci
37e41f4b71Sopenharmony_ci- User property verification: provides APIs for verifying user properties. See Figure 4 for the user property verification flowchart.
38e41f4b71Sopenharmony_ci
39e41f4b71Sopenharmony_ci- Event verification: provides APIs for verifying application events. See Figure 4 for the event verification flowchart.
40e41f4b71Sopenharmony_ci
41e41f4b71Sopenharmony_ci  **Figure 4** Data verification flowchart
42e41f4b71Sopenharmony_ci
43e41f4b71Sopenharmony_ci  ![Data verification](../subsystems/figures/hiappevent_extend_so_verify.png)
44e41f4b71Sopenharmony_ci
45e41f4b71Sopenharmony_ci## How to Develop
46e41f4b71Sopenharmony_ci
47e41f4b71Sopenharmony_ci#### Available APIs
48e41f4b71Sopenharmony_ci
49e41f4b71Sopenharmony_ci**Table 1** API called by the data processor library
50e41f4b71Sopenharmony_ci
51e41f4b71Sopenharmony_ci| API                                                                                        | Description                        |
52e41f4b71Sopenharmony_ci| ---------------------------------------------------------------------------------------------- | ---------------------------- |
53e41f4b71Sopenharmony_ci| int RegisterProcessor(const std::string& name, std::shared_ptr\<AppEventProcessor\> processor) | Registers a data processor with HiAppEvent.|
54e41f4b71Sopenharmony_ci
55e41f4b71Sopenharmony_ci**Table 1** APIs implemented by the data processor library
56e41f4b71Sopenharmony_ci
57e41f4b71Sopenharmony_ci| API                                                                                                                                                                | Description              |
58e41f4b71Sopenharmony_ci| ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------ |
59e41f4b71Sopenharmony_ci| int OnReport(int64_t processorSeq, const std::vector\<UserId\>& userIds, const std::vector\<UserProperty\>& userProperties, const std::vector\<AppEventInfo\>& events) | Reports events.        |
60e41f4b71Sopenharmony_ci| int ValidateUserId(const UserId& userId)                                                                                                                               | Verifies user IDs.      |
61e41f4b71Sopenharmony_ci| int ValidateUserProperty(const UserProperty& userProperty)                                                                                                             | Verifies user properties.|
62e41f4b71Sopenharmony_ci| int ValidateEvent(const AppEventInfo& event)                                                                                                                           | Verifies events.       |
63e41f4b71Sopenharmony_ci
64e41f4b71Sopenharmony_ci#### Development Procedure
65e41f4b71Sopenharmony_ci
66e41f4b71Sopenharmony_ci1. Add the `.so` file initialization method to the `processor_init.cpp` file, and perform initial registration.
67e41f4b71Sopenharmony_ci
68e41f4b71Sopenharmony_ci   ```c++
69e41f4b71Sopenharmony_ci      #include "app_event_processor_mgr.h"
70e41f4b71Sopenharmony_ci
71e41f4b71Sopenharmony_ci      using namespace OHOS::HiviewDFX::HiAppEvent;
72e41f4b71Sopenharmony_ci
73e41f4b71Sopenharmony_ci      void __attribute__((constructor)) x_init(void)
74e41f4b71Sopenharmony_ci      {
75e41f4b71Sopenharmony_ci        ...
76e41f4b71Sopenharmony_ci        int result = AppEventProcessorMgr::RegisterProcessor("processor_example", new ProcessorExample());
77e41f4b71Sopenharmony_ci        printf("ProcessorExample OnReport\n");
78e41f4b71Sopenharmony_ci      }
79e41f4b71Sopenharmony_ci   ```
80e41f4b71Sopenharmony_ci
81e41f4b71Sopenharmony_ci2. Define the `ProcessorExample` class, which is inherited from the base class `AppEventProcessor`, in the `processor_example.h` file.
82e41f4b71Sopenharmony_ci
83e41f4b71Sopenharmony_ci   ```c++
84e41f4b71Sopenharmony_ci      #include <vector>
85e41f4b71Sopenharmony_ci      #include "app_event_processor.h"
86e41f4b71Sopenharmony_ci
87e41f4b71Sopenharmony_ci      using namespace OHOS::HiviewDFX::HiAppEvent;
88e41f4b71Sopenharmony_ci
89e41f4b71Sopenharmony_ci      class ProcessorExample : public AppEventProcessor {
90e41f4b71Sopenharmony_ci      public:
91e41f4b71Sopenharmony_ci          int OnReport(int64_t processorSeq, const std::vector<UserId>& userIds, const std::vector<UserProperty>& userProperties, const std::vector<AppEventInfo>& events) override;
92e41f4b71Sopenharmony_ci          int ValidateUserId(const UserId& userId) override;
93e41f4b71Sopenharmony_ci          int ValidateUserProperty(const UserProperty& userProperty) override;
94e41f4b71Sopenharmony_ci          int ValidateEvent(const AppEventInfo& event) override;
95e41f4b71Sopenharmony_ci      ...
96e41f4b71Sopenharmony_ci      };
97e41f4b71Sopenharmony_ci   ```
98e41f4b71Sopenharmony_ci
99e41f4b71Sopenharmony_ci3. Implement the `ProcessorExample` class in the `processor_example.cpp` file, and override related functions based on the service logic.
100e41f4b71Sopenharmony_ci
101e41f4b71Sopenharmony_ci   ```c++
102e41f4b71Sopenharmony_ci    #include "processor_example.h"
103e41f4b71Sopenharmony_ci
104e41f4b71Sopenharmony_ci    int ProcessorExample::OnReport(int64_t processorSeq, const std::vector<UserId>& userIds, const std::vector<UserProperty>& userProperties, const std::vector<AppEventInfo>& events)
105e41f4b71Sopenharmony_ci    {
106e41f4b71Sopenharmony_ci        ... // Enable specific event processing in the OnReport function.
107e41f4b71Sopenharmony_ci        printf("ProcessorExample OnReport\n");
108e41f4b71Sopenharmony_ci        return 0;
109e41f4b71Sopenharmony_ci    }
110e41f4b71Sopenharmony_ci
111e41f4b71Sopenharmony_ci    int ProcessorExample::ValidateUserId(const UserId& userId)
112e41f4b71Sopenharmony_ci    {
113e41f4b71Sopenharmony_ci        ... // Enable user ID verification in the ValidateUserId function.
114e41f4b71Sopenharmony_ci        printf("ProcessorExample ValidateUserId\n");
115e41f4b71Sopenharmony_ci        return 0;
116e41f4b71Sopenharmony_ci    }
117e41f4b71Sopenharmony_ci
118e41f4b71Sopenharmony_ci    int ProcessorExample::ValidateUserProperty(const UserProperty& userProperty)
119e41f4b71Sopenharmony_ci    {
120e41f4b71Sopenharmony_ci        ... // Enable user property verification in the ValidateUserProperty function.
121e41f4b71Sopenharmony_ci        printf("ProcessorExample ValidateUserProperty\n");
122e41f4b71Sopenharmony_ci        return 0;
123e41f4b71Sopenharmony_ci    }
124e41f4b71Sopenharmony_ci
125e41f4b71Sopenharmony_ci    int ProcessorExample::ValidateEvent(const AppEventInfo& event)
126e41f4b71Sopenharmony_ci    {
127e41f4b71Sopenharmony_ci        ... // Enable event verification in the ValidateEvent function.
128e41f4b71Sopenharmony_ci        printf("ProcessorExample ValidateEvent\n");
129e41f4b71Sopenharmony_ci        return 0;
130e41f4b71Sopenharmony_ci    }
131e41f4b71Sopenharmony_ci   ```
132e41f4b71Sopenharmony_ci
133e41f4b71Sopenharmony_ci4. Configure the `ProcessorExample` class in the `build.gn` class file, and compile it with the data processor library.
134e41f4b71Sopenharmony_ci
135e41f4b71Sopenharmony_ci   ```conf
136e41f4b71Sopenharmony_ci    sources = [
137e41f4b71Sopenharmony_ci      ... // Include the required source file.
138e41f4b71Sopenharmony_ci      "./src/processor_init.cpp",
139e41f4b71Sopenharmony_ci      "./src/processor_example.cpp",
140e41f4b71Sopenharmony_ci    ]
141e41f4b71Sopenharmony_ci    external_deps = [
142e41f4b71Sopenharmony_ci      ... // Include the required external dependency library.
143e41f4b71Sopenharmony_ci      "hiappevent:hiappevent_innerapi",
144e41f4b71Sopenharmony_ci    ]
145e41f4b71Sopenharmony_ci   ```
146e41f4b71Sopenharmony_ci
147e41f4b71Sopenharmony_ci## References
148e41f4b71Sopenharmony_ci
149e41f4b71Sopenharmony_ciFor more information about the source code and usage of HiAppEvent, access the [HiAppEvent code repository](https://gitee.com/openharmony/hiviewdfx_hiappevent).
150