如何保存faultLogger ,有人知道吗?

如何保存faultLogger


HarmonyOS
2024-06-13 00:20:58
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
milchcow

参考代码如下:

import fs from '@ohos.file.fs'; 
import { BusinessError } from '@ohos.base'; 
import hilog from '@ohos.hilog'; 
import common from '@ohos.app.ability.common'; 
import AbilityConstant from '@ohos.app.ability.AbilityConstant'; 
import FaultLogger from '@ohos.faultLogger'; 
import preferences from '@ohos.data.preferences'; 
 
const TAG: string = 'testTag' 
 
export class LogUtils { 
  // import faultLogger from '@ohos.faultLogger' 
  static async queryAndUploadFaultLog(context: common.UIAbilityContext, launchParam: AbilityConstant.LaunchParam) { 
    if (launchParam.lastExitReason == AbilityConstant.LastExitReason.NORMAL) { 
      hilog.info(0x00000, TAG, '上次应用退出时未发生异常'); 
      return; 
    } 
    let value: Array<FaultLogger.FaultLogInfo> = await FaultLogger.query(FaultLogger.FaultType.NO_SPECIFIC) 
    hilog.info(0x00000, TAG, '报错日志:' + value.toString()); 
    if (value) { 
      let len = value.length 
      hilog.info(0x00000, TAG, '报错日志数量:' + len); 
      if (len === 0) { 
        return; 
      } 
      //取得实例 
      let preference: preferences.Preferences = await preferences.getPreferences(context, "STLiveness"); 
      // 查询上一次的处理的时间戳 
      let lastFaultHandleTime = preference.getSync('faultHandleTime', 0) 
      hilog.info(0x0000, TAG, "lastFaultHandleTime:" + lastFaultHandleTime); 
      // let innerContext = context; 
      for (let i = 0; i < len; i++) { 
        let timestamp = value[i].timestamp 
        hilog.info(0x00000, TAG, '日志文件名称#' + timestamp); 
        if (lastFaultHandleTime >= timestamp) { 
          hilog.error(0x00000, TAG, "Maple No New Logs."); 
          return; 
        } 
        // 将日志保存到应用沙箱的目录 保存文件名为 时间戳.log 
        await LogUtils.save(value[i].fullLog, context.filesDir + "/crash", timestamp + ".log") 
        await preference.put("faultHandleTime", timestamp); 
        await preference.flush() 
      } 
    } 
  } 
  static async save(buffer: ArrayBuffer | string, destFilePath: string, name: string): Promise<string> { 
    await LogUtils.mkdir(destFilePath); 
    hilog.info(0x00000, TAG, '写入日志的内容为:' + buffer); 
    hilog.info(0x00000, TAG, '被写入的日志文件路径为:' + destFilePath); 
    if (buffer) { 
      try { 
        let file = fs.openSync(destFilePath + "/" + name, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); 
        fs.writeSync(file.fd, buffer); 
        fs.closeSync(file) 
      } catch (error) { 
        let e: BusinessError = error as BusinessError; 
        hilog.info(0x0000, TAG, "FileUtils:save:::" + e.message); 
      } 
    } 
    return destFilePath; 
  } 
 
  static async mkdir(destFilePath: string) { 
    hilog.info(0x00000, TAG, '开始创建目录:' + destFilePath); 
    if (fs.accessSync(destFilePath)) { 
      hilog.info(0x00000, TAG, '目录已经存在,无需创建'); 
      return 
    } 
    await fs.mkdir(destFilePath) 
    hilog.info(0x00000, TAG, '创建完成'); 
  } 
}
分享
微博
QQ
微信
回复
2024-06-13 20:32:10
相关问题
图片压缩并保存方法,有人知道吗
308浏览 • 0回复 待解决
如何跳出ForEach,有人知道吗
590浏览 • 1回复 待解决
如何发送短信,有人知道吗?
551浏览 • 1回复 待解决
如何实现翻页功能,有人知道吗
571浏览 • 1回复 待解决
如何引用HSP库,有人知道吗?
547浏览 • 1回复 待解决
如何定义dialog动画,有人知道吗?
697浏览 • 1回复 待解决
如何使用快速修复,有人知道吗
216浏览 • 1回复 待解决
导航栏如何适配,有人知道吗?
554浏览 • 0回复 待解决
IP地址如何转化,有人知道吗
225浏览 • 1回复 待解决
如何获取组件高度,有人知道吗
538浏览 • 1回复 待解决
如何获取系统电量,有人知道吗
585浏览 • 1回复 待解决
如何实现http长连接,有人知道吗
448浏览 • 1回复 待解决
如何开启AOT编译模式,有人知道吗
636浏览 • 1回复 待解决
taskpool 使用问题,有人知道吗
389浏览 • 1回复 待解决
webview组件demo ,有人知道吗
418浏览 • 1回复 待解决
如何压缩字符串,有人知道吗
409浏览 • 1回复 待解决
如何拉起相机界面,有人知道吗
563浏览 • 1回复 待解决
热重载该如何实现?有人知道吗
306浏览 • 1回复 待解决
clientid相关问题,有人知道吗
447浏览 • 1回复 待解决
如何设置约束优先级,有人知道吗
368浏览 • 2回复 待解决
如何跳转到app设置页,有人知道吗
265浏览 • 1回复 待解决
List组件性能问题,有人知道吗
589浏览 • 1回复 待解决
如何实现防截屏功能,有人知道吗
725浏览 • 1回复 待解决
状态管理相关问题,有人知道吗?
351浏览 • 1回复 待解决