harmony 鸿蒙@ohos.faultLogger (FaultLogger)

2022-08-09 浏览 (981)

@ohos.faultLogger (FaultLogger)

NOTE

The initial APIs of this module are supported since API version 8. Newly added APIs will be marked with a superscript to indicate their earliest API version.

Modules to Import

import faultLogger from '@ohos.faultLogger'

FaultType

Enumerates the fault types.

System capability: SystemCapability.HiviewDFX.Hiview.FaultLogger

NameValueDescription
NO_SPECIFIC0No specific fault type.
CPP_CRASH2C++ program crash.
JS_CRASH3JS program crash.
APP_FREEZE4Application freezing.

FaultLogInfo

Defines the data structure of the fault log information.

System capability: SystemCapability.HiviewDFX.Hiview.FaultLogger

NameTypeMandatoryDescription
pidnumberYesProcess ID of the faulty process.
uidnumberYesUser ID of the faulty process.
typeFaultTypeYesFault type.
timestampnumberYesSecond-level timestamp when the log was generated.
reasonstringYesReason for the fault.
modulestringYesModule on which the fault occurred.
summarystringYesSummary of the fault.
fullLogstringYesFull log text.

faultLogger.query9+

query(faultType: FaultType, callback: AsyncCallback<Array<FaultLogInfo>>) : void

Obtains the fault information about the current process. This API uses an asynchronous callback to return the fault information array obtained, which contains a maximum of 10 pieces of fault information.

System capability: SystemCapability.HiviewDFX.Hiview.FaultLogger

Parameters

NameTypeMandatoryDescription
faultTypeFaultTypeYesFault type.
callbackAsyncCallback<Array<FaultLogInfo>>YesCallback used to return the fault information array.
value is the fault information array obtained. If value is undefined, an exception occurs during the information retrieval. In this case, an error string will be returned.

Error codes

For details about the error codes, see FaultLogger Error Codes.

IDError Message
10600001The service is not started or is faulty

Example

import faultLogger from '@ohos.faultLogger'
import { BusinessError } from '@ohos.base'

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}`);
}

faultLogger.query9+

query(faultType: FaultType) : Promise<Array<FaultLogInfo>>

Obtains the fault information about the current process. This API uses a promise to return the fault information array obtained, which contains a maximum of 10 pieces of fault information.

System capability: SystemCapability.HiviewDFX.Hiview.FaultLogger

Parameters

NameTypeMandatoryDescription
faultTypeFaultTypeYesFault type.

Return value

TypeDescription
Promise<Array<FaultLogInfo>>Promise used to return the fault information array. You can obtain the fault information instance in its then() method or use await.
value is the fault information array obtained. If value is undefined, an exception occurs during the information retrieval.

Error codes

For details about the error codes, see FaultLogger Error Codes.

IDError Message
10600001The service is not started or is faulty

Example

import faultLogger from '@ohos.faultLogger'
import { BusinessError } from '@ohos.base'

async function getLog() {
    try {
        let value: Array<faultLogger.FaultLogInfo> = await faultLogger.query(faultLogger.FaultType.JS_CRASH);
        if (value) {
            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);
            }
        }
    } catch (err) {
        console.error(`code: ${(err as BusinessError).code}, message: ${(err as BusinessError).message}`);
    }
}

faultLogger.querySelfFaultLog(deprecated)

querySelfFaultLog(faultType: FaultType, callback: AsyncCallback<Array<FaultLogInfo>>) : void

NOTE

This API is deprecated since API version 9. You are advised to use faultLogger.query instead.

Obtains the fault information about the current process. This API uses an asynchronous callback to return the fault information array obtained, which contains a maximum of 10 pieces of fault information.

System capability: SystemCapability.HiviewDFX.Hiview.FaultLogger

Parameters

NameTypeMandatoryDescription
faultTypeFaultTypeYesFault type.
callbackAsyncCallback<Array<FaultLogInfo>>YesCallback used to return the fault information array.
value is the fault information array obtained. If value is undefined, an exception occurs during the information retrieval. In this case, an error string will be returned.

Example

import faultLogger from '@ohos.faultLogger'
import { BusinessError } from '@ohos.base'

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);
        }
    }
}
faultLogger.querySelfFaultLog(faultLogger.FaultType.JS_CRASH, queryFaultLogCallback);

faultLogger.querySelfFaultLog(deprecated)

querySelfFaultLog(faultType: FaultType) : Promise<Array<FaultLogInfo>>

NOTE

This API is deprecated since API version 9. You are advised to use faultLogger.query.

Obtains the fault information about the current process. This API uses a promise to return the fault information array obtained, which contains a maximum of 10 pieces of fault information.

System capability: SystemCapability.HiviewDFX.Hiview.FaultLogger

Parameters

NameTypeMandatoryDescription
faultTypeFaultTypeYesFault type.

Return value

TypeDescription
Promise<Array<FaultLogInfo>>Promise used to return the fault information array. You can obtain the fault information instance in its then() method or use await.
value is the fault information array obtained. If value is undefined, an exception occurs during the information retrieval.

Example

import faultLogger from '@ohos.faultLogger'

async function getLog() {
    let value: Array<faultLogger.FaultLogInfo> = await faultLogger.querySelfFaultLog(faultLogger.FaultType.JS_CRASH);
    if (value) {
        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);
        }
    }
}

你可能感兴趣的鸿蒙文章

harmony 鸿蒙APIs

harmony 鸿蒙System Common Events (To Be Deprecated Soon)

harmony 鸿蒙System Common Events

harmony 鸿蒙API Reference Document Description

harmony 鸿蒙Enterprise Device Management Overview (for System Applications Only)

harmony 鸿蒙BundleStatusCallback

harmony 鸿蒙@ohos.bundle.innerBundleManager (innerBundleManager)

harmony 鸿蒙@ohos.distributedBundle (Distributed Bundle Management)

harmony 鸿蒙@ohos.bundle (Bundle)

harmony 鸿蒙@ohos.enterprise.EnterpriseAdminExtensionAbility (EnterpriseAdminExtensionAbility)

  • 所属分类: 后端技术
  • 本文标签: 鸿蒙 软件
  • 版权声明: 本文链接 https://seaxiang.com/blog/a48840974f634d158657519107e0d8f1