LogCat.js
5.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
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