Skip to content

Commit

Permalink
修复一个导致崩溃日志写入文件前会导致APP崩溃的莫名其妙问题
Browse files Browse the repository at this point in the history
  • Loading branch information
lyswhut committed May 22, 2021
1 parent c078519 commit 066c991
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 24 deletions.
4 changes: 4 additions & 0 deletions publish/changeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,7 @@
### 优化

- 切换到播放详情歌词界面时将阻止屏幕息屏

### 修复

- 修复一个导致崩溃日志写入文件前会导致APP崩溃的莫名其妙问题
11 changes: 4 additions & 7 deletions src/utils/errorHandle.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { log } from '@/utils/log'

const errorHandler = (e, isFatal) => {
if (isFatal) {
log.error(e.message)
Alert.alert(
'💥Unexpected error occurred💥',
`
Expand All @@ -21,17 +20,15 @@ ${isFatal ? 'Fatal:' : ''} ${e.name} ${e.message}
},
}],
)
} else {
log.error(e.message)
console.log(e) // So that we can see it in the ADB logs in case of Android if needed
}
log.error(e.message)
}

if (process.env.NODE_ENV !== 'development') {
setJSExceptionHandler(errorHandler, true)
setJSExceptionHandler(errorHandler)

setNativeExceptionHandler((errorString) => {
log.error(errorString)
console.error('+++++', errorString, '+++++')
log.err(errorString)
// console.error('+++++', errorString, '+++++')
})
}
39 changes: 22 additions & 17 deletions src/utils/log.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ const logTools = {
}

export const init = () => {
return mkdir(logDir).then(() => {
return existsFile(logDir).then(isExists => {
if (isExists) return
return mkdir(logDir)
}).then(() => {
const tasks = []
for (const [type, path] of Object.entries(logPath)) {
tasks.push(logTools.initLogFile(type, path))
Expand Down Expand Up @@ -94,29 +97,31 @@ export const clearLogs = (type = LOG_TYPE.error) => {

export const log = {
info(...msgs) {
console.info(...msgs)
let msg = msgs.map(m => typeof m == 'object' ? JSON.stringify(m) : m).join(' ')
// console.info(...msgs)
const msg = msgs.map(m => typeof m == 'string' ? m : JSON.stringify(m)).join(' ')
if (msg.startsWith('%c')) return
let time = new Date().toLocaleString()
const time = new Date().toLocaleString()
if (logTools.tempLog.info) {
logTools.tempLog.info.push({ type: 'LOG', time, text: msg })
} else logTools.writeLog(LOG_TYPE.info, `${time} LOG ${msg}`)
},
warn(...msgs) {
console.warn(...msgs)
let msg = msgs.map(m => typeof m == 'object' ? JSON.stringify(m) : m).join(' ')
let time = new Date().toLocaleString()
// console.warn(...msgs)
const msg = msgs.map(m => typeof m == 'string' ? m : JSON.stringify(m)).join(' ')
const time = new Date().toLocaleString()
if (logTools.tempLog.warn) {
logTools.tempLog.warn.push({ type: 'WARN', time, text: msg })
} else logTools.writeLog(LOG_TYPE.warn, `${time} WARN ${msg}`)
},
error(...msgs) {
console.error(...msgs)
let msg = msgs.map(m => typeof m == 'object' ? JSON.stringify(m) : m).join(' ')
let time = new Date().toLocaleString()
// console.error...(msgs)
const msg = msgs.map(m => typeof m == 'string' ? m : JSON.stringify(m)).join(' ')
const time = new Date().toLocaleString()
if (logTools.tempLog.error) {
logTools.tempLog.error.push({ type: 'ERROR', time, text: msg })
} else logTools.writeLog(LOG_TYPE.error, `${time} ERROR ${msg}`)
} else {
logTools.writeLog(LOG_TYPE.error, `${time} ERROR ${msg}`)
}
},
}
/*
Expand All @@ -133,25 +138,25 @@ if (process.env.NODE_ENV !== 'development') {
window.console.log = (...msgs) => {
log(...msgs)
let msg = msgs.map(m => typeof m == 'object' ? JSON.stringify(m) : m).join(' ')
const msg = msgs.map(m => typeof m == 'string' ? m : JSON.stringify(m)).join(' ')
if (msg.startsWith('%c')) return
let time = new Date().toLocaleString()
const time = new Date().toLocaleString()
if (tempLog) {
tempLog.push({ type: 'LOG', time, text: msg })
} else writeLog(`${time} LOG ${msg}`)
}
window.console.error = (...msgs) => {
error(...msgs)
let msg = msgs.map(m => typeof m == 'object' ? JSON.stringify(m) : m).join(' ')
let time = new Date().toLocaleString()
const msg = msgs.map(m => typeof m == 'string' ? m : JSON.stringify(m)).join(' ')
const time = new Date().toLocaleString()
if (tempLog) {
tempLog.push({ type: 'ERROR', time, text: msg })
} else writeLog(`${time} ERROR ${msg}`)
}
window.console.warn = (...msgs) => {
warn(...msgs)
let msg = msgs.map(m => typeof m == 'object' ? JSON.stringify(m) : m).join(' ')
let time = new Date().toLocaleString()
const msg = msgs.map(m => typeof m == 'string' ? m : JSON.stringify(m)).join(' ')
const time = new Date().toLocaleString()
if (tempLog) {
tempLog.push({ type: 'WARN', time, text: msg })
} else writeLog(`${time} WARN ${msg}`)
Expand Down

0 comments on commit 066c991

Please sign in to comment.