From 32ccb33d2bdc6461a1552e9ccf3a88b53f32a814 Mon Sep 17 00:00:00 2001 From: lyswhut Date: Tue, 25 May 2021 16:55:25 +0800 Subject: [PATCH] =?UTF-8?q?=E6=95=B4=E5=90=88=E6=97=A5=E5=BF=97=E8=AE=B0?= =?UTF-8?q?=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- index.js | 16 +++- publish/changeLog.md | 4 + src/lang/en_us.json | 2 +- src/lang/zh_cn.json | 2 +- src/screens/Home/Setting/Other/Log.js | 10 +-- src/utils/log.js | 122 +++++++------------------- 6 files changed, 54 insertions(+), 102 deletions(-) diff --git a/index.js b/index.js index e317f5e..7afa8b6 100644 --- a/index.js +++ b/index.js @@ -6,7 +6,7 @@ // import '@/utils/log' import './shim' import '@/utils/errorHandle' -import { init as initLog } from '@/utils/log' +import { init as initLog, log } from '@/utils/log' import '@/config/globalData' import SplashScreen from 'react-native-splash-screen' import { init as initNavigation, navigations, showPactModal } from '@/navigation' @@ -18,7 +18,7 @@ import { action as listAction } from '@/store/modules/list' import { init as initMusicTools } from '@/utils/music' import { init as initLyric } from '@/plugins/lyric' import { init as initI18n, supportedLngs } from '@/plugins/i18n' -import { deviceLanguage, getPlayInfo } from '@/utils/tools' +import { deviceLanguage, getPlayInfo, toast } from '@/utils/tools' import { LIST_ID_PLAY_TEMP } from '@/config/constant' console.log('starting app...') @@ -65,7 +65,17 @@ const init = () => { if (info.list) info.list = info.list.list } - if (!info.list || !info.list[info.index]) return + if (!info.list || !info.list[info.index]) { + const info2 = { ...info } + if (info2.list) { + info2.music = info2.list[info2.index]?.name + info2.list = info2.list.length + } + toast('恢复播放数据失败,请去错误日志查看', 'long') + log.warn('Restore Play Info failed: ', JSON.stringify(info2, null, 2)) + + return + } global.restorePlayInfo = info store.dispatch(playerAction.setList({ diff --git a/publish/changeLog.md b/publish/changeLog.md index 0b05cd2..4930fd5 100644 --- a/publish/changeLog.md +++ b/publish/changeLog.md @@ -3,3 +3,7 @@ - 尝试修复软件启动时恢复上一次播放的歌曲可能导致软件崩溃的问题 - 尝试修复播放详情页歌词导致UI冻结的问题 - 修复企鹅音乐搜索歌曲没有结果的问题 + +### 其他 + +- 整合日志记录 diff --git a/src/lang/en_us.json b/src/lang/en_us.json index db29013..b4df637 100644 --- a/src/lang/en_us.json +++ b/src/lang/en_us.json @@ -179,7 +179,7 @@ "setting_other_cache_size": "Currently used cache size: ", "setting_other_cache_clear_btn": "Clear Cache", "setting_other_cache_clear_success_tip": "Cache clearing completed", - "setting_other_log": "Error log (error log when the software crashes 💥)", + "setting_other_log": "Error log (log when abnormal operation occurs)", "setting_other_log_tip_clean_success": "Log cleaning completed", "setting_other_log_tip_null": "The log is empty~", "setting_other_log_btn_show": "View log", diff --git a/src/lang/zh_cn.json b/src/lang/zh_cn.json index 0d3df67..6c17b43 100644 --- a/src/lang/zh_cn.json +++ b/src/lang/zh_cn.json @@ -179,7 +179,7 @@ "setting_other_cache_size": "当前已用缓存大小:", "setting_other_cache_clear_btn": "清理缓存", "setting_other_cache_clear_success_tip": "缓存清理完成", - "setting_other_log": "错误日志(软件崩溃💥时的错误日志)", + "setting_other_log": "错误日志(运行发生异常时的日志)", "setting_other_log_tip_clean_success": "日志清理完成", "setting_other_log_tip_null": "日志是空的哦~", "setting_other_log_btn_show": "查看日志", diff --git a/src/screens/Home/Setting/Other/Log.js b/src/screens/Home/Setting/Other/Log.js index 29ad5fa..0bcbf87 100644 --- a/src/screens/Home/Setting/Other/Log.js +++ b/src/screens/Home/Setting/Other/Log.js @@ -1,6 +1,6 @@ import React, { memo, useRef, useState, useEffect } from 'react' import { StyleSheet, View, Text, InteractionManager } from 'react-native' -import { LOG_TYPE, getLogs, clearLogs } from '@/utils/log' +import { getLogs, clearLogs } from '@/utils/log' import { useGetter } from '@/store' // import { gzip, ungzip } from 'pako' @@ -18,11 +18,11 @@ export default memo(() => { const isUnmountedRef = useRef(true) const getErrorLog = () => { - getLogs(LOG_TYPE.error).then(log => { + getLogs().then(log => { if (isUnmountedRef.current) return - const logArr = log.split('\n') + const logArr = log.split('\n\n') logArr.reverse() - setLogText(logArr.join('\n\n').replace(/\n+$/, '')) + setLogText(logArr.join('\n').replace(/\n+$/, '')) }) } @@ -36,7 +36,7 @@ export default memo(() => { } const handleCleanLog = () => { - clearLogs(LOG_TYPE.error).then(() => { + clearLogs().then(() => { toast(t('setting_other_log_tip_clean_success')) getErrorLog() }) diff --git a/src/utils/log.js b/src/utils/log.js index fe924af..11fea7b 100644 --- a/src/utils/log.js +++ b/src/utils/log.js @@ -1,44 +1,20 @@ // import { requestStoragePermission } from '@/utils/common' -import { temporaryDirectoryPath, existsFile, writeFile, appendFile, mkdir, readFile, unlink } from '@/utils/fs' +import { temporaryDirectoryPath, existsFile, writeFile, appendFile, readFile, unlink } from '@/utils/fs' + +const logPath = temporaryDirectoryPath + '/error.log' -export const LOG_TYPE = { - info: 'INFO', - warn: 'WARN', - error: 'ERROR', -} -const logDir = temporaryDirectoryPath + '/lx_logs' -const logPath = { - info: logDir + '/info.log', - warn: logDir + '/warn.log', - error: logDir + '/error.log', -} const logTools = { - tempLog: { - info: [], - warn: [], - error: [], + tempLog: [], + writeLog(msg) { + console.log(msg) + appendFile(logPath, '\n\n' + msg) }, - writeLog(type, msg) { - switch (type) { - case LOG_TYPE.info: - appendFile(logPath.info, '\n' + msg) - break - case LOG_TYPE.warn: - appendFile(logPath.warn, '\n' + msg) - break - case LOG_TYPE.error: - appendFile(logPath.error, '\n' + msg) - break - default: - break - } - }, - async initLogFile(type, filePath) { + async initLogFile() { try { - let isExists = await existsFile(filePath) - if (!isExists) await writeFile(filePath, '') - if (this.tempLog[type].length) this.writeLog(LOG_TYPE[type], this.tempLog[type].map(m => `${m.time} ${m.type} ${m.text}`).join('\n')) - this.tempLog[type] = null + 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) } @@ -46,53 +22,15 @@ const logTools = { } export const init = () => { - 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)) - } - console.log('init log tools') - return Promise.all(tasks) - }) + return logTools.initLogFile() } -export const getLogs = (type = LOG_TYPE.error) => { - let path - switch (type) { - case LOG_TYPE.info: - path = logPath.info - break - case LOG_TYPE.warn: - path = logPath.warn - break - case LOG_TYPE.error: - path = logPath.error - break - default: - return Promise.reject(new Error('Unknow log type')) - } - return readFile(path) +export const getLogs = () => { + return readFile(logPath) } -export const clearLogs = (type = LOG_TYPE.error) => { - let path - switch (type) { - case LOG_TYPE.info: - path = logPath.info - break - case LOG_TYPE.warn: - path = logPath.warn - break - case LOG_TYPE.error: - path = logPath.error - break - default: - return Promise.reject(new Error('Unknow log type')) - } - return unlink(path).then(() => writeFile(path, '')) +export const clearLogs = () => { + return unlink(logPath).then(() => writeFile(logPath, '')) } export const log = { @@ -101,26 +39,26 @@ export const log = { 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.info) { - logTools.tempLog.info.push({ type: 'LOG', time, text: msg }) - } else logTools.writeLog(LOG_TYPE.info, `${time} LOG ${msg}`) + 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.warn) { - logTools.tempLog.warn.push({ type: 'WARN', time, text: msg }) - } else logTools.writeLog(LOG_TYPE.warn, `${time} WARN ${msg}`) + 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.error) { - logTools.tempLog.error.push({ type: 'ERROR', time, text: msg }) + if (logTools.tempLog) { + logTools.tempLog.push({ type: 'ERROR', time, text: msg }) } else { - logTools.writeLog(LOG_TYPE.error, `${time} ERROR ${msg}`) + logTools.writeLog(`${time} ERROR ${msg}`) } }, } @@ -142,7 +80,7 @@ if (process.env.NODE_ENV !== 'development') { if (msg.startsWith('%c')) return const time = new Date().toLocaleString() if (tempLog) { - tempLog.push({ type: 'LOG', time, text: msg }) + tempLog({ type: 'LOG', time, text: msg }) } else writeLog(`${time} LOG ${msg}`) } window.console.error = (...msgs) => { @@ -150,7 +88,7 @@ if (process.env.NODE_ENV !== 'development') { 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 }) + tempLog({ type: 'ERROR', time, text: msg }) } else writeLog(`${time} ERROR ${msg}`) } window.console.warn = (...msgs) => { @@ -158,7 +96,7 @@ if (process.env.NODE_ENV !== 'development') { 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 }) + tempLog({ type: 'WARN', time, text: msg }) } else writeLog(`${time} WARN ${msg}`) } @@ -169,7 +107,7 @@ if (process.env.NODE_ENV !== 'development') { let isExists = await existsFile(logPath) console.log(logPath, isExists) if (!isExists) await writeFile(logPath, '') - writeLog(tempLog.map(m => `${m.time} ${m.type} ${m.text}`).join('\n')) + writeLog(tempLog(m => `${m.time} ${m.type} ${m.text}`).join('\n')) tempLog = null } catch (err) { console.error(err)