使用faultLogger.query(faultType: FaultType, callback: AsyncCallback<Array<FaultLogInfo>)接口获取故障日志。FaultType是枚举类型,可选值如下:
名称 | 值 | 说明 |
|---|---|---|
NO_SPECIFIC | 0 | 不区分故障类型 |
CPP_CRASH | 2 | C++程序故障类型 |
JS_CRASH | 3 | JS程序故障类型 |
APP_FREEZE | 4 | 应用程序卡死故障类型 |
第二个参数为callback回调函数,用于获取故障信息数组。
示例代码中,FaultType取值为JS_CRASH,queryFaultLogCallback用于回调并打印相关日志信息。
import { FaultLogger } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
function queryFaultLogCallback(error: BusinessError, value: Array<FaultLogger.FaultLogInfo>) {
if (error) {
console.info('error is ' + error);
} else {
console.info("value length is " + value.length);
let len: number = value.length;
for (let i = 0; i < len; i++) {
console.info("log: " + i);
console.info("Log pid: " + value[i].pid);
console.info("Log uid: " + value[i].uid);
console.info("Log type: " + value[i].type);
console.info("Log timestamp: " + value[i].timestamp);
console.info("Log reason: " + value[i].reason);
console.info("Log module: " + value[i].module);
console.info("Log summary: " + value[i].summary);
console.info("Log text: " + value[i].fullLog);
}
}
}
try {
FaultLogger.query(FaultLogger.FaultType.JS_CRASH, queryFaultLogCallback);
} catch (err) {
console.error(`code: ${(err as BusinessError).code}, message: ${(err as BusinessError).message}`);
} 参考链接