添加更多同步功能的日志记录

This commit is contained in:
lyswhut 2021-10-14 17:33:07 +08:00
parent 3104ca4213
commit 42f88969fb
15 changed files with 76 additions and 25 deletions

View File

@ -2,6 +2,10 @@
- 新增歌曲评论显示可在播放详情页进入。与PC端一样目前仅支持显示部分评论 - 新增歌曲评论显示可在播放详情页进入。与PC端一样目前仅支持显示部分评论
### 优化
- 添加更多同步功能的日志记录
### 修复 ### 修复
- 修复kg源的歌单链接无法打开的问题 - 修复kg源的歌单链接无法打开的问题

View File

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

View File

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

View File

@ -2,12 +2,13 @@ import { getSyncAuthKey, setSyncAuthKey } from '@/utils/tools'
import { request, aesEncrypt, aesDecrypt } from './utils' import { request, aesEncrypt, aesDecrypt } from './utils'
import { getDeviceName } from '@/utils/utils' import { getDeviceName } from '@/utils/utils'
import { SYNC_CODE } from './config' import { SYNC_CODE } from './config'
import { log } from '@/utils/log' import log from '../log'
const hello = (host, port) => request(`http://${host}:${port}/hello`) const hello = (host, port) => request(`http://${host}:${port}/hello`)
.then(text => text == SYNC_CODE.helloMsg) .then(text => text == SYNC_CODE.helloMsg)
.catch(err => { .catch(err => {
log.error('[auth] hello', err.message)
console.log(err) console.log(err)
return false return false
}) })
@ -18,6 +19,7 @@ const getServerId = (host, port) => request(`http://${host}:${port}/id`)
return text.replace(SYNC_CODE.idPrefix, '') return text.replace(SYNC_CODE.idPrefix, '')
}) })
.catch(err => { .catch(err => {
log.error('[auth] getServerId', err.message)
console.log(err) console.log(err)
return false return false
}) })
@ -33,6 +35,7 @@ const codeAuth = async(host, port, serverId, authCode) => {
try { try {
msg = aesDecrypt(text, key, iv) msg = aesDecrypt(text, key, iv)
} catch (err) { } catch (err) {
log.error('[auth] codeAuth decryptMsg error', err.message)
throw new Error(SYNC_CODE.authFailed) throw new Error(SYNC_CODE.authFailed)
} }
if (!msg) return Promise.reject(new Error(SYNC_CODE.authFailed)) if (!msg) return Promise.reject(new Error(SYNC_CODE.authFailed))
@ -49,6 +52,7 @@ const keyAuth = async(host, port, keyInfo) => {
try { try {
msg = aesDecrypt(text, keyInfo.key, keyInfo.iv) msg = aesDecrypt(text, keyInfo.key, keyInfo.iv)
} catch (err) { } catch (err) {
log.error('[auth] keyAuth decryptMsg error', err.message)
throw new Error(SYNC_CODE.authFailed) throw new Error(SYNC_CODE.authFailed)
} }
if (msg != SYNC_CODE.helloMsg) return Promise.reject(new Error(SYNC_CODE.authFailed)) if (msg != SYNC_CODE.helloMsg) return Promise.reject(new Error(SYNC_CODE.authFailed))

View File

@ -4,7 +4,7 @@ import * as modules from '../modules'
import { action as commonAction } from '@/store/modules/common' import { action as commonAction } from '@/store/modules/common'
import { getStore } from '@/store' import { getStore } from '@/store'
import syncList from './syncList' import syncList from './syncList'
import { log } from '@/utils/log' import log from '../log'
const handleConnection = (socket) => { const handleConnection = (socket) => {
for (const module of Object.values(modules)) { for (const module of Object.values(modules)) {
@ -26,16 +26,23 @@ export const connect = (host, port, keyInfo) => {
socket.on('connect', async() => { socket.on('connect', async() => {
console.log('connect') console.log('connect')
log.info('connect')
const store = getStore() const store = getStore()
global.syncKeyInfo = keyInfo global.syncKeyInfo = keyInfo
try { try {
await syncList(socket) await syncList(socket)
} catch (err) { } catch (err) {
console.log(err) console.log(err)
log.error(err.stack) log.r_error(err.stack)
store.dispatch(commonAction.setSyncStatus({
status: false,
message: err.message,
}))
return return
} }
log.info('sync list success')
handleConnection(socket) handleConnection(socket)
log.info('register list sync service success')
store.dispatch(commonAction.setSyncStatus({ store.dispatch(commonAction.setSyncStatus({
status: true, status: true,
message: '', message: '',
@ -43,7 +50,7 @@ export const connect = (host, port, keyInfo) => {
}) })
socket.on('connect_error', (err) => { socket.on('connect_error', (err) => {
console.log(err.message) console.log(err.message)
if (global.isEnableSyncLog) log.error(err.stack) log.error('connect error: ', err.stack)
const store = getStore() const store = getStore()
store.dispatch(commonAction.setSyncStatus({ store.dispatch(commonAction.setSyncStatus({
status: false, status: false,
@ -56,6 +63,7 @@ export const connect = (host, port, keyInfo) => {
}) })
socket.on('disconnect', (reason) => { socket.on('disconnect', (reason) => {
console.log('disconnect', reason) console.log('disconnect', reason)
log.warn('disconnect: ', reason)
const store = getStore() const store = getStore()
store.dispatch(commonAction.setSyncStatus({ store.dispatch(commonAction.setSyncStatus({
status: false, status: false,
@ -91,6 +99,7 @@ export const connect = (host, port, keyInfo) => {
export const disconnect = async() => { export const disconnect = async() => {
if (!socket) return if (!socket) return
log.info('disconnecting...')
await socket.close() await socket.close()
socket = null socket = null
} }

View File

@ -4,7 +4,7 @@ import { getSyncHost } from '@/utils/tools'
import { action as commonAction } from '@/store/modules/common' import { action as commonAction } from '@/store/modules/common'
import { getStore } from '@/store' import { getStore } from '@/store'
import { SYNC_CODE } from './config' import { SYNC_CODE } from './config'
import { log } from '@/utils/log' import log from '../log'
const handleConnect = async authCode => { const handleConnect = async authCode => {
const hostInfo = await getSyncHost() const hostInfo = await getSyncHost()
@ -41,7 +41,7 @@ const connect = authCode => {
case SYNC_CODE.missingAuthCode: case SYNC_CODE.missingAuthCode:
break break
default: default:
log.warn(err.message) log.r_warn(err.message)
break break
} }
@ -50,6 +50,7 @@ const connect = authCode => {
} }
const disconnect = (isResetStatus = true) => handleDisconnect().then(() => { const disconnect = (isResetStatus = true) => handleDisconnect().then(() => {
log.info('disconnect...')
if (isResetStatus) { if (isResetStatus) {
const store = getStore() const store = getStore()
store.dispatch(commonAction.setSyncStatus({ store.dispatch(commonAction.setSyncStatus({
@ -59,6 +60,7 @@ const disconnect = (isResetStatus = true) => handleDisconnect().then(() => {
} }
}).catch(err => { }).catch(err => {
const store = getStore() const store = getStore()
log.error('disconnect error: ' + err.message)
store.dispatch(commonAction.setSyncStatus({ store.dispatch(commonAction.setSyncStatus({
message: err.message, message: err.message,
})) }))

View File

@ -4,6 +4,8 @@ import { action as listAction } from '@/store/modules/list'
import { toast } from '@/utils/tools' import { toast } from '@/utils/tools'
import { decryptMsg, encryptMsg } from './utils' import { decryptMsg, encryptMsg } from './utils'
import log from '../log'
let socket let socket
let syncAction let syncAction
@ -31,7 +33,9 @@ const sendListData = type => {
socket.emit('list:sync', encryptMsg(JSON.stringify({ socket.emit('list:sync', encryptMsg(JSON.stringify({
action: 'getData', action: 'getData',
data: listData, data: listData,
}))) })), () => {
log.info('[syncList]send data success')
})
// console.log('sendListData', 'encryptMsg') // console.log('sendListData', 'encryptMsg')
} }
@ -46,13 +50,17 @@ const handleListSync = enMsg => {
// console.log('handleListSync', action) // console.log('handleListSync', action)
switch (action) { switch (action) {
case 'getData': case 'getData':
log.info('[syncList]get data')
sendListData(data) sendListData(data)
break break
case 'setData': case 'setData':
log.info('[syncList]set data')
saveList(data) saveList(data)
log.info('[syncList]set data success')
break break
case 'finished': case 'finished':
if (!syncAction) return if (!syncAction) return
log.info('[syncList]finished')
syncAction[0]() syncAction[0]()
syncAction = null syncAction = null
toast('Sync successfully') toast('Sync successfully')

22
src/plugins/sync/log.js Normal file
View File

@ -0,0 +1,22 @@
import { log as writeLog } from '@/utils/log'
export default {
r_info(...args) {
writeLog.info(...args)
},
r_warn(...args) {
writeLog.warn(...args)
},
r_error(...args) {
writeLog.error(...args)
},
info(...args) {
if (global.isEnableSyncLog) writeLog.info(...args)
},
warn(...args) {
if (global.isEnableSyncLog) writeLog.warn(...args)
},
error(...args) {
if (global.isEnableSyncLog) writeLog.error(...args)
},
}

View File

@ -1,5 +1,6 @@
import { getStore } from '@/store' import { getStore } from '@/store'
import { decryptMsg } from '../../client/utils' import { decryptMsg } from '../../client/utils'
import log from '../../log'
import { import {
setList, setList,
listAdd, listAdd,
@ -27,6 +28,7 @@ const handleListAction = enMsg => {
const { action, data } = JSON.parse(decryptMsg(enMsg)) const { action, data } = JSON.parse(decryptMsg(enMsg))
if (typeof data == 'object') data.isSync = true if (typeof data == 'object') data.isSync = true
console.log(action) console.log(action)
log.info(action)
switch (action) { switch (action) {
// case 'init_list': // case 'init_list':

View File

@ -64,7 +64,7 @@ export default memo(() => {
<> <>
<SubTitle title={t('setting_other_log')}> <SubTitle title={t('setting_other_log')}>
<View style={{ paddingTop: 10, paddingBottom: 15, marginLeft: -25 }}> <View style={{ paddingTop: 10, paddingBottom: 15, marginLeft: -25 }}>
<CheckBoxItem check={isEnableSyncErrorLog} label={t('setting_other_log_sync_error_log')} onChange={handleSetEnableSyncErrorLog} /> <CheckBoxItem check={isEnableSyncErrorLog} label={t('setting_other_log_sync_log')} onChange={handleSetEnableSyncErrorLog} />
</View> </View>
<View style={styles.btn}> <View style={styles.btn}>
<Button onPress={openLogModal}>{t('setting_other_log_btn_show')}</Button> <Button onPress={openLogModal}>{t('setting_other_log_btn_show')}</Button>

View File

@ -1,11 +1,11 @@
import React from 'react' import React from 'react'
import PlayerPortrait from './PlayerPortrait' import PlayerPortrait from './PlayerPortrait'
const playNextModes = [ // const playNextModes = [
'listLoop', // 'listLoop',
'random', // 'random',
'list', // 'list',
'singleLoop', // 'singleLoop',
] // ]
export default () => <PlayerPortrait playNextModes={playNextModes} /> export default () => <PlayerPortrait />

View File

@ -5,7 +5,7 @@ import { useGetter, useDispatch } from '@/store'
import { STATUS } from '@/store/modules/player' import { STATUS } from '@/store/modules/player'
export default ({ playNextModes }) => { export default () => {
const playStatus = useGetter('player', 'status') const playStatus = useGetter('player', 'status')
const playNext = useDispatch('player', 'playNext') const playNext = useDispatch('player', 'playNext')
const playPrev = useDispatch('player', 'playPrev') const playPrev = useDispatch('player', 'playPrev')

View File

@ -10,7 +10,7 @@ import PlayInfo from './components/PlayInfo'
import ControlBtn from './components/ControlBtn' import ControlBtn from './components/ControlBtn'
export default memo(({ playNextModes }) => { export default memo(() => {
// const { onLayout, ...layout } = useLayout() // const { onLayout, ...layout } = useLayout()
const { keyboardShown } = useKeyboard() const { keyboardShown } = useKeyboard()
const theme = useGetter('common', 'theme') const theme = useGetter('common', 'theme')
@ -28,10 +28,10 @@ export default memo(({ playNextModes }) => {
{(componentIds.playDetail || componentIds.songlistDetail) ? null : <PlayInfo />} {(componentIds.playDetail || componentIds.songlistDetail) ? null : <PlayInfo />}
</View> </View>
<View style={styles.right}> <View style={styles.right}>
<ControlBtn playNextModes={playNextModes} /> <ControlBtn />
</View> </View>
</View> </View>
), [theme, componentIds, playNextModes]) ), [theme, componentIds])
// console.log(layout) // console.log(layout)

View File

@ -5,7 +5,7 @@ import { useGetter, useDispatch } from '@/store'
import { STATUS } from '@/store/modules/player' import { STATUS } from '@/store/modules/player'
export default ({ playNextModes }) => { export default () => {
const playStatus = useGetter('player', 'status') const playStatus = useGetter('player', 'status')
const playNext = useDispatch('player', 'playNext') const playNext = useDispatch('player', 'playNext')
const playPrev = useDispatch('player', 'playPrev') const playPrev = useDispatch('player', 'playPrev')

View File

@ -10,7 +10,7 @@ import PlayInfo from './components/PlayInfo'
import ControlBtn from './components/ControlBtn' import ControlBtn from './components/ControlBtn'
export default memo(({ playNextModes }) => { export default memo(() => {
// const { onLayout, ...layout } = useLayout() // const { onLayout, ...layout } = useLayout()
const { keyboardShown } = useKeyboard() const { keyboardShown } = useKeyboard()
const theme = useGetter('common', 'theme') const theme = useGetter('common', 'theme')
@ -28,10 +28,10 @@ export default memo(({ playNextModes }) => {
{componentIds.playDetail ? null : <PlayInfo />} {componentIds.playDetail ? null : <PlayInfo />}
</View> </View>
<View style={styles.right}> <View style={styles.right}>
<ControlBtn playNextModes={playNextModes} /> <ControlBtn />
</View> </View>
</View> </View>
), [theme, componentIds, playNextModes]) ), [theme, componentIds])
// console.log(layout) // console.log(layout)