优化日志记录与显示

This commit is contained in:
lyswhut 2021-09-16 15:48:57 +08:00
parent 9ea332a073
commit ccbdfceb3f
8 changed files with 31 additions and 7 deletions

View File

@ -28,3 +28,6 @@ global.playInfo = {
currentPlayMusicInfo: null,
duration: 0,
}
global.isEnableSyncLog = false

View File

@ -126,6 +126,7 @@
"setting_other_log_btn_clean": "Clear",
"setting_other_log_btn_hide": "Close",
"setting_other_log_btn_show": "View log",
"setting_other_log_sync_error_log": "Record synchronization connection failure error log",
"setting_other_log_tip_clean_success": "Log cleaning completed",
"setting_other_log_tip_null": "The log is empty~",
"setting_play": "Play",

View File

@ -126,6 +126,7 @@
"setting_other_log_btn_clean": "清空",
"setting_other_log_btn_hide": "关闭",
"setting_other_log_btn_show": "查看日志",
"setting_other_log_sync_error_log": "记录同步连接失败错误日志",
"setting_other_log_tip_clean_success": "日志清理完成",
"setting_other_log_tip_null": "日志是空的哦~",
"setting_play": "播放设置",

View File

@ -33,7 +33,6 @@ const codeAuth = async(host, port, serverId, authCode) => {
try {
msg = aesDecrypt(text, key, iv)
} catch (err) {
log.warn(err.stack)
throw new Error(SYNC_CODE.authFailed)
}
if (!msg) return Promise.reject(new Error(SYNC_CODE.authFailed))
@ -50,7 +49,6 @@ const keyAuth = async(host, port, keyInfo) => {
try {
msg = aesDecrypt(text, keyInfo.key, keyInfo.iv)
} catch (err) {
log.warn(err.stack)
throw new Error(SYNC_CODE.authFailed)
}
if (msg != SYNC_CODE.helloMsg) return Promise.reject(new Error(SYNC_CODE.authFailed))

View File

@ -43,6 +43,7 @@ export const connect = (host, port, keyInfo) => {
})
socket.on('connect_error', (err) => {
console.log(err.message)
if (global.isEnableSyncLog) log.error(err.stack)
const store = getStore()
store.dispatch(commonAction.setSyncStatus({
status: false,

View File

@ -36,7 +36,15 @@ const connect = authCode => {
status: false,
message: err.message,
}))
log.warn(err.message)
switch (err.message) {
case SYNC_CODE.connectServiceFailed:
case SYNC_CODE.missingAuthCode:
break
default:
log.warn(err.message)
break
}
return Promise.reject(err)
})
}

View File

@ -9,6 +9,7 @@ import Button from '../components/Button'
import { useTranslation } from '@/plugins/i18n'
import { toast } from '@/utils/tools'
import ConfirmAlert from '@/components/common/ConfirmAlert'
import CheckBoxItem from '../components/CheckBoxItem'
export default memo(() => {
const { t } = useTranslation()
@ -16,13 +17,15 @@ export default memo(() => {
const [logText, setLogText] = useState('')
const theme = useGetter('common', 'theme')
const isUnmountedRef = useRef(true)
const [isEnableSyncErrorLog, setIsEnableSyncErrorLog] = useState(false)
const getErrorLog = () => {
getLogs().then(log => {
if (isUnmountedRef.current) return
const logArr = log.split('\n\n')
const logArr = log.split(/^----lx log----\n|\n----lx log----\n|\n----lx log----$/)
// console.log(logArr)
logArr.reverse()
setLogText(logArr.join('\n').replace(/\n+$/, ''))
setLogText(logArr.join('\n\n').replace(/^\n+|\n+$/, ''))
})
}
@ -42,9 +45,15 @@ export default memo(() => {
})
}
const handleSetEnableSyncErrorLog = enable => {
setIsEnableSyncErrorLog(enable)
global.isEnableSyncLog = enable
}
useEffect(() => {
isUnmountedRef.current = false
setIsEnableSyncErrorLog(global.isEnableSyncLog)
return () => {
isUnmountedRef.current = true
}
@ -54,6 +63,9 @@ export default memo(() => {
return (
<>
<SubTitle title={t('setting_other_log')}>
<View style={{ paddingTop: 10, paddingBottom: 15, marginLeft: -25 }}>
<CheckBoxItem check={isEnableSyncErrorLog} label={t('setting_other_log_sync_error_log')} onChange={handleSetEnableSyncErrorLog} />
</View>
<View style={styles.btn}>
<Button onPress={openLogModal}>{t('setting_other_log_btn_show')}</Button>
</View>

View File

@ -7,13 +7,13 @@ const logTools = {
tempLog: [],
writeLog(msg) {
console.log(msg)
appendFile(logPath, '\n' + msg)
appendFile(logPath, '\n----lx log----\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'))
if (this.tempLog.length) this.writeLog(this.tempLog.map(m => `${m.time} ${m.type} ${m.text}`).join('\n----lx log----\n'))
this.tempLog = null
} catch (err) {
console.error(err)