9 Star 8 Fork 48

OpenHarmony / hiviewdfx_hiappevent

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
Apache-2.0

HiAppEvent组件

简介

HiAppEvent为OpenHarmony应用提供事件打点接口,用于帮助应用记录在运行过程中发生的故障信息、统计信息、安全信息、用户行为信息,以支撑开发者分析应用的运行情况。

图 1 HiAppEvent架构图

目录

/base/hiviewdfx/hiappevent   # hiappevent部件代码
├── frameworks               # 框架代码
│   └── native               # 打点接口的native实现代码
├── interfaces               # 对外接口存放目录
│   └── js                   # js接口
│       └── innerkits        # js接口内部实现代码
│           └── napi         # 基于napi实现的js接口代码
├── test                     # 测试用例代码

编译构建

依赖 Clang 编译器(Clang 8.0.0 )及以上,依赖C++11版本及以上。

说明

接口说明

JS接口说明

表 1 JS 打点接口介绍

模块

方法

描述

hiAppEvent

write(string eventName, EventType type, object keyValues, AsyncCallback<void> callback): void

接口功能:应用事件异步打点方法,使用callback方式作为异步回调。

输入参数:

  • eventName:事件名称。
  • type:事件类型。
  • keyValues:事件参数键值对,为Json对象类型。
  • callback:回调函数,可以在回调函数中处理接口返回值。返回值为0表示事件参数校验成功,事件正常异步写入事件文件;大于0表示事件存在异常参数,事件在忽略异常参数后再异步写入事件文件;小于0表示事件校验失败,不执行事件异步打点操作。

hiAppEvent

write(string eventName, EventType type, object keyValues): Promise<void>

接口功能:应用事件异步打点方法,使用promise方式作为异步回调。

输入参数:同上。

表 2 JS 事件类型枚举——EventType

类型 描述
FAULT 故障类型事件。
STATISTIC 统计类型事件。
SECURITY 安全类型事件。
BEHAVIOR 行为类型事件。

表 3 JS 打点配置接口介绍

模块 方法 描述
hiAppEvent configure(config: ConfigOption): boolean 接口功能:应用事件打点配置方法,可以对打点功能进行自定义配置。
输入参数:
  • config:应用事件打点配置项对象。
返回值:boolean,true表示配置成功,false表示配置失败。

表 4 JS 应用打点配置选项——ConfigOption

参数名 类型 必填 说明
disable boolean 应用打点功能开关。配置值为true表示关闭打点功能,false表示不关闭打点功能。
maxStorage string 打点数据本地存储文件所在目录的配额大小,默认限额为“10M”。所在目录大小超出限额后会对目录进行清理操作,会按从旧到新的顺序逐个删除打点数据文件,直到目录大小不超出限额时停止。

表 5 JS 预定义事件名称常量接口——Event

常量名 类型 描述
USER_LOGIN string 用户登录事件。
USER_LOGOUT string 用户登出事件。
DISTRIBUTED_SERVICE_START string 分布式服务启动事件。

表 6 JS 预定义参数名称常量接口——Param

常量名 类型 描述
USER_ID string 用户自定义ID。
DISTRIBUTED_SERVICE_NAME string 分布式服务名称。
DISTRIBUTED_SERVICE_INSTANCE_ID string 分布式服务实例ID。

Native接口说明

表 1 打点接口介绍

方法 返回值 描述
OH_HiAppEvent_Write(const char* domain, const char* name, enum EventType type, const ParamList list) int 接口功能:应用事件打点方法。
输入参数:
  • domain:事件领域。
  • name:事件名称。
  • type:事件类型。
  • list:事件参数列表,实质上是一个指向链表头节点的指针——ParamListNode*,列表的每个参数节点由参数名和参数值组成。
返回值:int,错误码。

表 2 事件参数列表构造接口介绍

方法 返回值 描述
OH_HiAppEvent_CreateParamList() ParamList 创建一个ParamList节点。返回指向创建节点的指针。
OH_HiAppEvent_DestroyParamList(ParamList list) void 从头节点开始逐个删除参数列表的各个节点,并释放内存。
OH_HiAppEvent_AddBoolParam(ParamList list, const char* name, bool boolean) ParamList 创建一个bool类型的参数节点,并将该节点添加到ParamList。
OH_HiAppEvent_AddBoolArrayParam(ParamList list, const char* name, const bool* booleans, int arrSize) ParamList 创建一个bool数组类型的参数节点,并将该节点添加到ParamList。
OH_HiAppEvent_AddInt8Param(ParamList list, const char* name, int8_t num) ParamList 创建一个int8_t类型的参数节点,并将该节点添加到ParamList。
OH_HiAppEvent_AddInt8ArrayParam(ParamList list, const char* name, const int8_t* nums, int arrSize) ParamList 创建一个int8_t数组类型的参数节点,并将该节点添加到ParamList。
OH_HiAppEvent_AddInt16Param(ParamList list, const char* name, int16_t num) ParamList 创建一个int16_t类型的参数节点,并将该节点添加到ParamList。
OH_HiAppEvent_AddInt16ArrayParam(ParamList list, const char* name, const int16_t* nums, int arrSize) ParamList 创建一个int16_t数组类型的参数节点,并将该节点添加到ParamList。
OH_HiAppEvent_AddInt32Param(ParamList list, const char* name, int32_t num) ParamList 创建一个int32_t类型的参数节点,并将该节点添加到ParamList。
OH_HiAppEvent_AddInt32ArrayParam(ParamList list, const char* name, const int32_t* nums, int arrSize) ParamList 创建一个int32_t数组类型的参数节点,并将该节点添加到ParamList。
OH_HiAppEvent_AddInt64Param(ParamList list, const char* name, int64_t num) ParamList 创建一个int64_t类型的参数节点,并将该节点添加到ParamList。
OH_HiAppEvent_AddInt64ArrayParam(ParamList list, const char* name, const int64_t* nums, int arrSize) ParamList 创建一个int64_t数组类型的参数节点,并将该节点添加到ParamList。
OH_HiAppEvent_AddFloatParam(ParamList list, const char* name, float num) ParamList 创建一个float类型的参数节点,并将该节点添加到ParamList。
OH_HiAppEvent_AddFloatArrayParam(ParamList list, const char* name, const float* nums, int arrSize) ParamList 创建一个float数组类型的参数节点,并将该节点添加到ParamList。
OH_HiAppEvent_AddDoubleParam(ParamList list, const char* name, double num) ParamList 创建一个double类型的参数节点,并将该节点添加到ParamList。
OH_HiAppEvent_AddDoubleArrayParam(ParamList list, const char* name, const double* nums, int arrSize) ParamList 创建一个double数组类型的参数节点,并将该节点添加到ParamList。
OH_HiAppEvent_AddStringParam(ParamList list, const char* name, const char* str) ParamList 创建一个char*类型的参数节点,并将该节点添加到ParamList。
OH_HiAppEvent_AddStringArrayParam(ParamList list, const char* name, const char * const *strs, int arrSize) ParamList 创建一个char*数组类型的参数节点,并将该节点添加到ParamList。

表 3 打点配置接口介绍

方法 返回值 描述
OH_HiAppEvent_Configure(const char* name, const char* value) bool 接口功能:应用事件打点配置方法,可以对打点功能进行自定义配置。
输入参数:
  • name:配置项名称,可直接传入预定义好的配置项常量。
  • value:配置项值。
返回值:bool,返回true表示配置成功,返回false表示配置失败。

表 4 配置项常量介绍

常量名 类型 描述
DISABLE const char[] 打点功能开关的配置项名称,对应的默认配置值为“false”表示不关闭打点功能。
MAX_STORAGE const char[] 打点落盘文件存放目录配额大小的配置项名称,对应的默认配置值为“10M”。

表 5 预定义事件名称常量介绍

常量名 类型 描述
EVENT_USER_LOGIN const char[] 用户登录事件名称。
EVENT_USER_LOGOUT const char[] 用户登出事件名称。
EVENT_DISTRIBUTED_SERVICE_START const char[] 分布式服务启动事件名称。

表 6 预定义参数名称常量介绍

类型 描述
PARAM_USER_ID const char[] 用户自定义ID。
PARAM_DISTRIBUTED_SERVICE_NAME const char[] 分布式服务名称。
PARAM_DISTRIBUTED_SERVICE_INSTANCE_ID const char[] 分布式服务实例ID。

使用说明

JS接口开发实例

  1. 源代码开发

    引入模块:

    import hiAppEvent from '@ohos.hiAppEvent'
  2. 应用执行事件打点

    // callback方式
    hiAppEvent.write("test_event", hiAppEvent.EventType.FAULT, {"int_data":100, "str_data":"strValue"}, (err, value) => {
        if (err) {
            // 事件写入异常:事件存在异常参数或者事件校验失败不执行写入
            console.error(`failed to write event because ${err.code}`);
            return;
        }
    
        // 事件写入正常
        console.log(`success to write event: ${value}`);
    });
    
    // Promise方式
    hiAppEvent.write("test_event", hiAppEvent.EventType.FAULT, {"int_data":100, "str_data":"strValue"})
        .then((value) => {
            // 事件写入正常
            console.log(`success to write event: ${value}`);
        }).catch((err) => {
            // 事件写入异常:事件存在异常参数或者事件校验失败不执行写入
            console.error(`failed to write event because ${err.code}`);
        });
  3. 应用对打点功能进行自定义配置

    // 配置应用事件打点功能开关
    hiAppEvent.configure({
        disable: true
    })
    
    // 配置事件文件目录存储限额大小
    hiAppEvent.configure({
        maxStorage: '100M'
    })

Native接口开发实例

  1. 源代码开发

    引入模块:

    #include "hiappevent/hiappevent.h"
  2. 应用执行事件打点

    // 1. 创建空参数list
    ParamList list = OH_HiAppEvent_CreateParamList();
    
    // 2. 向list添加要传入的键值对参数
    // 2.1 传入int32_t参数值
    int32_t num = 1;
    OH_HiAppEvent_AddInt32Param(list, "int32_key", num);
    // 2.2 传入int32_t数组参数值
    int32_t nums3[] = {1, INT32_MAX, INT32_MIN};
    OH_HiAppEvent_AddInt32ArrayParam(list, "int32_arr_key", nums3, sizeof(nums3) / sizeof(nums3[0]));
    // 2.3 传入字符串数组参数值
    char str1[] = "hello";
    char str2[] = "world";
    char* strs[] = {str1, str2};
    OH_HiAppEvent_AddStringArrayParam(list, "string_arr_key", strs, sizeof(strs) / sizeof(strs[0]));
    
    // 3. 执行打点操作
    int result = OH_HiAppEvent_Write("domain", "name", BEHAVIOR, list);
    printf("HiAppEvent logging test, res=%d\n", result);
    
    // 4. 销毁ParamLIst对象,释放内存
    OH_HiAppEvent_DestroyParamList(list);
  3. 应用对打点功能进行自定义配置

    // 关闭应用打点功能
    OH_HiAppEvent_Configure(DISABLE, "true");
    
    // 配置打点数据文件目录存储限额为100M
    OH_HiAppEvent_Configure(MAX_STORAGE, "100M");

相关仓

DFX子系统

hiviewdfx_hiview

hiviewdfx_hilog

hiviewdfx_hiappevent

hiviewdfx_hisysevent

hiviewdfx_faultloggerd

hiviewdfx_hilog_lite

hiviewdfx_hievent_lite

hiviewdfx_hiview_lite

Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS

简介

Application event logging | 应用事件记录接口及框架 展开 收起
C++ 等 3 种语言
Apache-2.0
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
1
https://gitee.com/openharmony/hiviewdfx_hiappevent.git
git@gitee.com:openharmony/hiviewdfx_hiappevent.git
openharmony
hiviewdfx_hiappevent
hiviewdfx_hiappevent
master

搜索帮助

14c37bed 8189591 565d56ea 8189591