1# Subscribing to Crash Events (C/C++) 2 3## Available APIs 4 5For details about how to use the APIs (such as parameter usage restrictions and value ranges), see [HiAppEvent](../reference/apis-performance-analysis-kit/_hi_app_event.md#hiappevent). 6 7**Subscription APIs** 8 9| API | Description | 10| ------------------------------------------------------------ | -------------------------------------------- | 11| int OH_HiAppEvent_AddWatcher(HiAppEvent_Watcher \*watcher) | Adds a watcher to listen for application events.| 12| int OH_HiAppEvent_RemoveWatcher (HiAppEvent_Watcher \*watcher) | Removes a watcher to unsubscribe from application events.| 13 14## How to Develop 15 16The following describes how to subscribe to the crash event triggered by a button click. 17 181. Create a native C++ project and import the **jsoncpp** file to the project. The directory structure is as follows: 19 20 ```yml 21 entry: 22 src: 23 main: 24 cpp: 25 - json: 26 - json.h 27 - json-forwards.h 28 - types: 29 libentry: 30 - index.d.ts 31 - CMakeLists.txt 32 - napi_init.cpp 33 - jsoncpp.cpp 34 ets: 35 - entryability: 36 - EntryAbility.ets 37 - pages: 38 - Index.ets 39 ``` 40 412. In the **CMakeLists.txt** file, add the source file and dynamic libraries. 42 43 ```cmake 44 # Add the jsoncpp.cpp file, which is used to parse the JSON strings in the subscription events. 45 add_library(entry SHARED napi_init.cpp jsoncpp.cpp) 46 # Add libhiappevent_ndk.z.so and libhilog_ndk.z.so (log output). 47 target_link_libraries(entry PUBLIC libace_napi.z.so libhilog_ndk.z.so libhiappevent_ndk.z.so) 48 ``` 49 503. Import the dependency files to the **napi_init.cpp** file, and define **LOG_TAG**. 51 52 ```c++ 53 # include "json/json.h" 54 # include "hilog/log.h" 55 # include "hiappevent/hiappevent.h" 56 57 # undef LOG_TAG 58 # define LOG_TAG "testTag" 59 ``` 60 614. Subscribe to application events. 62 63 - Watcher of the onReceive type: 64 65 In the **napi_init.cpp** file, define the methods related to the watcher of the onReceive type. 66 67 ```c++ 68 // Define a variable to cache the pointer to the created watcher. 69 static HiAppEvent_Watcher *systemEventWatcher; 70 71 static void OnReceive(const char *domain, const struct HiAppEvent_AppEventGroup *appEventGroups, uint32_t groupLen) { 72 for (int i = 0; i < groupLen; ++i) { 73 for (int j = 0; j < appEventGroups[i].infoLen; ++j) { 74 OH_LOG_INFO(LogType::LOG_APP, "HiAppEvent eventInfo.domain=%{public}s", appEventGroups[i].appEventInfos[j].domain); 75 OH_LOG_INFO(LogType::LOG_APP, "HiAppEvent eventInfo.name=%{public}s", appEventGroups[i].appEventInfos[j].name); 76 OH_LOG_INFO(LogType::LOG_APP, "HiAppEvent eventInfo.eventType=%{public}d", appEventGroups[i].appEventInfos[j].type); 77 if (strcmp(appEventGroups[i].appEventInfos[j].domain, DOMAIN_OS) == 0 && 78 strcmp(appEventGroups[i].appEventInfos[j].name, EVENT_APP_CRASH) == 0) { 79 Json::Value params; 80 Json::Reader reader(Json::Features::strictMode()); 81 Json::FastWriter writer; 82 if (reader.parse(appEventGroups[i].appEventInfos[j].params, params)) { 83 auto time = params["time"].asInt64(); 84 auto crashType = params["crash_type"].asString(); 85 auto foreground = params["foreground"].asBool(); 86 auto bundleVersion = params["bundle_version"].asString(); 87 auto bundleName = params["bundle_name"].asString(); 88 auto pid = params["pid"].asInt(); 89 auto uid = params["uid"].asInt(); 90 auto uuid = params["uuid"].asString(); 91 auto exception = writer.write(params["exception"]); 92 auto hilogSize = params["hilog"].size(); 93 auto externalLog = writer.write(params["external_log"]); 94 auto logOverLimit = params["log_over_limit"].asBool(); 95 OH_LOG_INFO(LogType::LOG_APP, "HiAppEvent eventInfo.params.time=%{public}lld", time); 96 OH_LOG_INFO(LogType::LOG_APP, "HiAppEvent eventInfo.params.crash_type=%{public}s", crashType.c_str()); 97 OH_LOG_INFO(LogType::LOG_APP, "HiAppEvent eventInfo.params.foreground=%{public}d", foreground); 98 OH_LOG_INFO(LogType::LOG_APP, "HiAppEvent eventInfo.params.bundle_version=%{public}s", bundleVersion.c_str()); 99 OH_LOG_INFO(LogType::LOG_APP, "HiAppEvent eventInfo.params.bundle_name=%{public}s", bundleName.c_str()); 100 OH_LOG_INFO(LogType::LOG_APP, "HiAppEvent eventInfo.params.pid=%{public}d", pid); 101 OH_LOG_INFO(LogType::LOG_APP, "HiAppEvent eventInfo.params.uid=%{public}d", uid); 102 OH_LOG_INFO(LogType::LOG_APP, "HiAppEvent eventInfo.params.uuid=%{public}s", uuid.c_str()); 103 OH_LOG_INFO(LogType::LOG_APP, "HiAppEvent eventInfo.params.exception=%{public}s", exception.c_str()); 104 OH_LOG_INFO(LogType::LOG_APP, "HiAppEvent eventInfo.params.hilog.size=%{public}d", hilogSize); 105 OH_LOG_INFO(LogType::LOG_APP, "HiAppEvent eventInfo.params.external_log=%{public}s", externalLog.c_str()); 106 OH_LOG_INFO(LogType::LOG_APP, "HiAppEvent eventInfo.params.log_over_limit=%{public}d", logOverLimit); 107 } 108 } 109 } 110 } 111 } 112 113 static napi_value RegisterWatcher(napi_env env, napi_callback_info info) { 114 // Set the watcher name. The system identifies different watchers based on their names. 115 systemEventWatcher = OH_HiAppEvent_CreateWatcher("onReceiverWatcher"); 116 // Set the event type to EVENT_APP_CRASH. 117 const char *names[] = {EVENT_APP_CRASH}; 118 // Add the system events to watch, for example, system events. 119 OH_HiAppEvent_SetAppEventFilter(systemEventWatcher, DOMAIN_OS, 0, names, 1); 120 // Set the implemented callback. After receiving the event, the watcher immediately triggers the OnReceive callback. 121 OH_HiAppEvent_SetWatcherOnReceive(systemEventWatcher, OnReceive); 122 // Add a watcher to listen for the specified event. 123 OH_HiAppEvent_AddWatcher(systemEventWatcher); 124 return {}; 125 } 126 ``` 127 128 - Watcher of the onTrigger type: 129 130 In the **napi_init.cpp** file, define the methods related to the watcher of the OnTrigger type. 131 132 ```c++ 133 // Define a variable to cache the pointer to the created watcher. 134 static HiAppEvent_Watcher *systemEventWatcher; 135 136 // Implement the callback function used to return the listened events. The content pointed to by the events pointer is valid only in this function. 137 static void OnTake(const char *const *events, uint32_t eventLen) { 138 Json::Reader reader(Json::Features::strictMode()); 139 Json::FastWriter writer; 140 for (int i = 0; i < eventLen; ++i) { 141 Json::Value eventInfo; 142 if (reader.parse(events[i], eventInfo)) { 143 auto domain = eventInfo["domain_"].asString(); 144 auto name = eventInfo["name_"].asString(); 145 auto type = eventInfo["type_"].asInt(); 146 OH_LOG_INFO(LogType::LOG_APP, "HiAppEvent eventInfo.domain=%{public}s", domain.c_str()); 147 OH_LOG_INFO(LogType::LOG_APP, "HiAppEvent eventInfo.name=%{public}s", name.c_str()); 148 OH_LOG_INFO(LogType::LOG_APP, "HiAppEvent eventInfo.eventType=%{public}d", type); 149 if (domain == DOMAIN_OS && name == EVENT_APP_CRASH) { 150 auto time = eventInfo["time"].asInt64(); 151 auto crashType = eventInfo["crash_type"].asString(); 152 auto foreground = eventInfo["foreground"].asBool(); 153 auto bundleVersion = eventInfo["bundle_version"].asString(); 154 auto bundleName = eventInfo["bundle_name"].asString(); 155 auto pid = eventInfo["pid"].asInt(); 156 auto uid = eventInfo["uid"].asInt(); 157 auto uuid = eventInfo["uuid"].asString(); 158 auto exception = writer.write(eventInfo["exception"]); 159 auto hilogSize = eventInfo["hilog"].size(); 160 auto externalLog = writer.write(eventInfo["external_log"]); 161 auto logOverLimit = eventInfo["log_over_limit"].asBool(); 162 OH_LOG_INFO(LogType::LOG_APP, "HiAppEvent eventInfo.params.time=%{public}lld", time); 163 OH_LOG_INFO(LogType::LOG_APP, "HiAppEvent eventInfo.params.crash_type=%{public}s", crashType.c_str()); 164 OH_LOG_INFO(LogType::LOG_APP, "HiAppEvent eventInfo.params.foreground=%{public}d", foreground); 165 OH_LOG_INFO(LogType::LOG_APP, "HiAppEvent eventInfo.params.bundle_version=%{public}s", bundleVersion.c_str()); 166 OH_LOG_INFO(LogType::LOG_APP, "HiAppEvent eventInfo.params.bundle_name=%{public}s", bundleName.c_str()); 167 OH_LOG_INFO(LogType::LOG_APP, "HiAppEvent eventInfo.params.pid=%{public}d", pid); 168 OH_LOG_INFO(LogType::LOG_APP, "HiAppEvent eventInfo.params.uid=%{public}d", uid); 169 OH_LOG_INFO(LogType::LOG_APP, "HiAppEvent eventInfo.params.uuid=%{public}s", uuid.c_str()); 170 OH_LOG_INFO(LogType::LOG_APP, "HiAppEvent eventInfo.params.exception=%{public}s", exception.c_str()); 171 OH_LOG_INFO(LogType::LOG_APP, "HiAppEvent eventInfo.params.hilog.size=%{public}d", hilogSize); 172 OH_LOG_INFO(LogType::LOG_APP, "HiAppEvent eventInfo.params.external_log=%{public}s", externalLog.c_str()); 173 OH_LOG_INFO(LogType::LOG_APP, "HiAppEvent eventInfo.params.log_over_limit=%{public}d", logOverLimit); 174 } 175 } 176 } 177 } 178 179 // Implement the subscription callback function to apply custom processing to the obtained event logging data. 180 static void OnTrigger(int row, int size) { 181 // After the callback is received, obtain the specified number of received events. 182 OH_HiAppEvent_TakeWatcherData(systemEventWatcher, row, OnTake); 183 } 184 185 static napi_value RegisterWatcher(napi_env env, napi_callback_info info) { 186 // Set the watcher name. The system identifies different watchers based on their names. 187 systemEventWatcher = OH_HiAppEvent_CreateWatcher("onTriggerWatcher"); 188 // Set the event type to EVENT_APP_CRASH. 189 const char *names[] = {EVENT_APP_CRASH}; 190 // Add the system events to watch, for example, system events. 191 OH_HiAppEvent_SetAppEventFilter(systemEventWatcher, DOMAIN_OS, 0, names, 1); 192 // Set the implemented callback function. The callback function will be triggered when the conditions set by OH_HiAppEvent_SetTriggerCondition are met. 193 OH_HiAppEvent_SetWatcherOnTrigger(systemEventWatcher, OnTrigger); 194 // Set the conditions for triggering the subscription callback. For example, trigger this onTrigger callback when the number of new event logs is 1. 195 OH_HiAppEvent_SetTriggerCondition(systemEventWatcher, 1, 0, 0); 196 // Add a watcher to listen for the specified event. 197 OH_HiAppEvent_AddWatcher(systemEventWatcher); 198 return {}; 199 } 200 ``` 201 2025. Register **RegisterWatcher** as an ArkTS API. 203 204 In the **napi_init.cpp** file, register **RegisterWatcher** as an ArkTS API. 205 206 ```c++ 207 static napi_value Init(napi_env env, napi_value exports) 208 { 209 napi_property_descriptor desc[] = { 210 { "registerWatcher", nullptr, RegisterWatcher, nullptr, nullptr, nullptr, napi_default, nullptr } 211 }; 212 napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); 213 return exports; 214 } 215 ``` 216 217 In the **index.d.ts** file, define ArkTS APIs. 218 219 ```typescript 220 export const registerWatcher: () => void; 221 ``` 222 2236. In the **EntryAbility.ets** file, add the following interface to **onCreate()**. 224 225 ```typescript 226 // Import the dependent module. 227 import testNapi from 'libentry.so' 228 229 // Add the interface to onCreate(). 230 // Register the system event watcher at startup. 231 testNapi.registerWatcher(); 232 ``` 233 2347. In the **Index.ets** file, add a button to trigger the crash event. 235 236 ```typescript 237 Button("appCrash").onClick(() => { 238 JSON.parse(""); 239 }) 240 ``` 241 2428. In DevEco Studio, click the **Run** button to run the project. Then, click the **appCrash** button to trigger a crash event. 243 2449. The application crashes. After restarting the application, you can view the following event information in the **Log** window. 245 246 ```text 247 HiAppEvent eventInfo.domain=OS 248 HiAppEvent eventInfo.name=APP_CRASH 249 HiAppEvent eventInfo.eventType=1 250 HiAppEvent eventInfo.params.time=1502032265088 251 HiAppEvent eventInfo.params.crash_type=JsError 252 HiAppEvent eventInfo.params.foreground=1 253 HiAppEvent eventInfo.params.bundle_version=1.0.0 254 HiAppEvent eventInfo.params.bundle_name=com.example.myapplication 255 HiAppEvent eventInfo.params.pid=19237 256 HiAppEvent eventInfo.params.uid=20010043 257 HiAppEvent eventInfo.params.uuid=cc0f062e1b28c1fd2c817fafab5e8ca3207925b4bdd87c43ed23c60029659e01 258 HiAppEvent eventInfo.params.exception={"message":"Unexpected Text in JSON","name":"SyntaxError","stack":"at anonymous (entry/src/main/ets/pages/Index.ets:16:11)"} 259 HiAppEvent eventInfo.params.hilog.size=110 260 HiAppEvent eventInfo.params.external_log=["/data/storage/el2/log/hiappevent/APP_CRASH_1502032265211_19237.log"] 261 HiAppEvent eventInfo.params.log_over_limit=0 262 ``` 263 26410. Remove the application event watcher. 265 266 ```c++ 267 static napi_value RemoveWatcher(napi_env env, napi_callback_info info) { 268 // Remove the watcher. 269 OH_HiAppEvent_RemoveWatcher(systemEventWatcher); 270 return {}; 271 } 272 ``` 273 27411. Destroy the application event watcher. 275 276 ```c++ 277 static napi_value DestroyWatcher(napi_env env, napi_callback_info info) { 278 // Destroy the created watcher and set systemEventWatcher to nullptr. 279 OH_HiAppEvent_DestroyWatcher(systemEventWatcher); 280 systemEventWatcher = nullptr; 281 return {}; 282 } 283 ``` 284 285<!--no_check-->