LogCat.js 5.5 KB
import { getTime } from './util'
import { serializeIfObject } from '../utils'
class LogCat {
  // main = plus.android.runtimeMainActivity()
  // File = plus.android.importClass('java.io.File')
  // Environment = plus.android.importClass('android.os.Environment')
  // BufferedWriter = plus.android.importClass('java.io.BufferedWriter')
  // FileOutputStream = plus.android.importClass('java.io.FileOutputStream')
  // OutputStreamWriter = plus.android.importClass('java.io.OutputStreamWriter')
  // // BufferedReader = plus.android.importClass('java.io.BufferedReader')
  // // FileInputStream = plus.android.importClass('java.io.FileInputStream')
  // // InputStreamReader = plus.android.importClass('java.io.InputStreamReader')
  // LogPath = ''
  constructor(folderName = '_zyzs_log') {
    // if (this.Environment.MEDIA_MOUNTED || !this.Environment.isExternalStorageRemovable()) {
    //   this.LogPath = this.main.getExternalFilesDir(null).getPath() + folderName
    // } else {
    //   this.LogPath = this.main.getFilesDir().getPath() + folderName
    // }
    // console.log('日志文件存放路径->', this.LogPath)
  }
  writeLog(tag, messages) {
    // let msg = serializeIfObject(messages)
    // let date = getTime('YYYY-MM-DD')
    // let datetime = getTime()
    // //文件名
    // let fileName = this.LogPath + '/' + date + '.txt'
    // //写入的内容
    // let content = `${datetime} [${tag}] : ${msg}\n`
    // console.log(content.replace(/\n/g, ''))
    // let path = new this.File(this.LogPath)
    // if (!path.exists()) {
    //   path.mkdirs() //创建父路径
    // }
    // let fos = null
    // let bw = null
    // try {
    //   fos = new this.FileOutputStream(fileName, true)
    //   bw = new this.BufferedWriter(new this.OutputStreamWriter(fos))
    //   //bw.write(log);
    //   bw.append(content)
    // } catch (e) {
    //   console.log('e->', e)
    // } finally {
    //   try {
    //     if (bw != null) {
    //       bw.close() //关闭缓冲流
    //       fos.close() //关闭文件输出流
    //     }
    //   } catch (closeEx) {
    //     console.log('closeEx->', closeEx)
    //   }
    // }
  }
  // 删除日志
  removeLogFile(fileName) {
    let file = new this.File(fileName)
    let fos = new this.FileOutputStream(fileName, true)
    fos.close()
    file.delete()
    console.log('删除------' + fileName)
  }
  // 获取所有日志文件列表
  getLogFileList() {
    let file = new this.File(this.LogPath)
    let tempList = file.listFiles()
    if (!tempList || tempList.length === 0) return []
    const list = tempList.map(file => {
      return {
        name: file.getName(),
        path: file.getPath()
      }
    })
    return list
  }
}
export default LogCat
// import { getTime } from './util'
// import { serializeIfObject } from '../utils'
// class LogCat {
//   // main = plus.android.runtimeMainActivity()
//   // File = plus.android.importClass('java.io.File')
//   // Environment = plus.android.importClass('android.os.Environment')
//   // BufferedWriter = plus.android.importClass('java.io.BufferedWriter')
//   // FileOutputStream = plus.android.importClass('java.io.FileOutputStream')
//   // OutputStreamWriter = plus.android.importClass('java.io.OutputStreamWriter')

//   // BufferedReader = plus.android.importClass('java.io.BufferedReader')
//   // FileInputStream = plus.android.importClass('java.io.FileInputStream')
//   // InputStreamReader = plus.android.importClass('java.io.InputStreamReader')

//   LogPath = ''
//   constructor(folderName = '_zyzs_log') {
//     // if (this.Environment.MEDIA_MOUNTED || !this.Environment.isExternalStorageRemovable()) {
//     //   this.LogPath = this.main.getExternalFilesDir(null).getPath() + folderName
//     // } else {
//     //   this.LogPath = this.main.getFilesDir().getPath() + folderName
//     // }
//     // console.log('日志文件存放路径->', this.LogPath)
//   }
//   writeLog(tag, messages) {
//     return
//     let msg = serializeIfObject(messages)
//     let date = getTime('YYYY-MM-DD')
//     let datetime = getTime()
//     //文件名
//     let fileName = this.LogPath + '/' + date + '.txt'
//     //写入的内容
//     let content = `${datetime} [${tag}] : ${msg}\n`
//     console.log(content.replace(/\n/g, ''))
//     let path = new this.File(this.LogPath)
//     if (!path.exists()) {
//       path.mkdirs() //创建父路径
//     }
//     let fos = null
//     let bw = null
//     try {
//       fos = new this.FileOutputStream(fileName, true)
//       bw = new this.BufferedWriter(new this.OutputStreamWriter(fos))
//       //bw.write(log);
//       bw.append(content)
//     } catch (e) {
//       console.log('e->', e)
//     } finally {
//       try {
//         if (bw != null) {
//           bw.close() //关闭缓冲流
//           fos.close() //关闭文件输出流
//         }
//       } catch (closeEx) {
//         console.log('closeEx->', closeEx)
//       }
//     }
//   }
//   // 删除日志
//   removeLogFile(fileName) {
//     let file = new this.File(fileName)
//     let fos = new this.FileOutputStream(fileName, true)
//     fos.close()
//     file.delete()
//     console.log('删除------' + fileName)
//   }
//   // 获取所有日志文件列表
//   getLogFileList() {
//     let file = new this.File(this.LogPath)
//     let tempList = file.listFiles()
//     if (!tempList || tempList.length === 0) return []
//     const list = tempList.map(file => {
//       return {
//         name: file.getName(),
//         path: file.getPath()
//       }
//     })
//     return list
//   }
// }
// export default LogCat