mirror of
https://github.com/ikun0014/lx-music-mobile.git
synced 2025-05-23 22:37:41 +08:00
122 lines
3.5 KiB
JavaScript
122 lines
3.5 KiB
JavaScript
// import { requestStoragePermission } from '@/utils/common'
|
|
import { temporaryDirectoryPath, existsFile, writeFile, appendFile, readFile, unlink } from '@/utils/fs'
|
|
|
|
const logPath = temporaryDirectoryPath + '/error.log'
|
|
|
|
const logTools = {
|
|
tempLog: [],
|
|
writeLog(msg) {
|
|
console.log(msg)
|
|
appendFile(logPath, '\n\n' + msg)
|
|
},
|
|
async initLogFile() {
|
|
try {
|
|
let isExists = await existsFile(logPath)
|
|
if (!isExists) await writeFile(logPath, '')
|
|
if (this.tempLog) this.writeLog(this.tempLog.map(m => `${m.time} ${m.type} ${m.text}`).join('\n'))
|
|
this.tempLog = null
|
|
} catch (err) {
|
|
console.error(err)
|
|
}
|
|
},
|
|
}
|
|
|
|
export const init = () => {
|
|
return logTools.initLogFile()
|
|
}
|
|
|
|
export const getLogs = () => {
|
|
return readFile(logPath)
|
|
}
|
|
|
|
export const clearLogs = () => {
|
|
return unlink(logPath).then(() => writeFile(logPath, ''))
|
|
}
|
|
|
|
export const log = {
|
|
info(...msgs) {
|
|
// console.info(...msgs)
|
|
const msg = msgs.map(m => typeof m == 'string' ? m : JSON.stringify(m)).join(' ')
|
|
if (msg.startsWith('%c')) return
|
|
const time = new Date().toLocaleString()
|
|
if (logTools.tempLog) {
|
|
logTools.tempLog.push({ type: 'LOG', time, text: msg })
|
|
} else logTools.writeLog(`${time} LOG ${msg}`)
|
|
},
|
|
warn(...msgs) {
|
|
// console.warn(...msgs)
|
|
const msg = msgs.map(m => typeof m == 'string' ? m : JSON.stringify(m)).join(' ')
|
|
const time = new Date().toLocaleString()
|
|
if (logTools.tempLog) {
|
|
logTools.tempLog.push({ type: 'WARN', time, text: msg })
|
|
} else logTools.writeLog(`${time} WARN ${msg}`)
|
|
},
|
|
error(...msgs) {
|
|
// console.error...(msgs)
|
|
const msg = msgs.map(m => typeof m == 'string' ? m : JSON.stringify(m)).join(' ')
|
|
const time = new Date().toLocaleString()
|
|
if (logTools.tempLog) {
|
|
logTools.tempLog.push({ type: 'ERROR', time, text: msg })
|
|
} else {
|
|
logTools.writeLog(`${time} ERROR ${msg}`)
|
|
}
|
|
},
|
|
}
|
|
/*
|
|
if (process.env.NODE_ENV !== 'development') {
|
|
const logPath = externalDirectoryPath + '/debug.log'
|
|
|
|
let tempLog = []
|
|
|
|
const log = window.console.log
|
|
const error = window.console.error
|
|
const warn = window.console.warn
|
|
|
|
const writeLog = msg => appendFile(logPath, '\n' + msg)
|
|
|
|
window.console.log = (...msgs) => {
|
|
log(...msgs)
|
|
const msg = msgs.map(m => typeof m == 'string' ? m : JSON.stringify(m)).join(' ')
|
|
if (msg.startsWith('%c')) return
|
|
const time = new Date().toLocaleString()
|
|
if (tempLog) {
|
|
tempLog({ type: 'LOG', time, text: msg })
|
|
} else writeLog(`${time} LOG ${msg}`)
|
|
}
|
|
window.console.error = (...msgs) => {
|
|
error(...msgs)
|
|
const msg = msgs.map(m => typeof m == 'string' ? m : JSON.stringify(m)).join(' ')
|
|
const time = new Date().toLocaleString()
|
|
if (tempLog) {
|
|
tempLog({ type: 'ERROR', time, text: msg })
|
|
} else writeLog(`${time} ERROR ${msg}`)
|
|
}
|
|
window.console.warn = (...msgs) => {
|
|
warn(...msgs)
|
|
const msg = msgs.map(m => typeof m == 'string' ? m : JSON.stringify(m)).join(' ')
|
|
const time = new Date().toLocaleString()
|
|
if (tempLog) {
|
|
tempLog({ type: 'WARN', time, text: msg })
|
|
} else writeLog(`${time} WARN ${msg}`)
|
|
}
|
|
|
|
const init = async() => {
|
|
try {
|
|
let result = await requestStoragePermission()
|
|
if (!result) return
|
|
let isExists = await existsFile(logPath)
|
|
console.log(logPath, isExists)
|
|
if (!isExists) await writeFile(logPath, '')
|
|
writeLog(tempLog(m => `${m.time} ${m.type} ${m.text}`).join('\n'))
|
|
tempLog = null
|
|
} catch (err) {
|
|
console.error(err)
|
|
}
|
|
}
|
|
|
|
|
|
init()
|
|
}
|
|
|
|
*/
|