optimize: 捕获应用启动过程中的异常,并记录日志。

pull/445/head
王良 2025-01-22 16:29:19 +08:00
parent 2a7550368b
commit 6d57101b2f
5 changed files with 137 additions and 121 deletions

View File

@ -173,7 +173,7 @@ const configApi = {
log.info('读取远程配置文件内容成功:', path)
return file.toString()
} else {
log.warn('远程配置文件不存在:', path)
log.info('远程配置文件不存在:', path)
}
} catch (e) {
log.error('读取远程配置文件内容失败:', e)

View File

@ -30,7 +30,13 @@ let winIsHidden = false
let tray // 防止被内存清理
let forceClose = false
DevSidecar.api.config.reload()
try {
DevSidecar.api.config.reload()
} catch (e) {
log.error('配置加载失败:', e)
}
let hideDockWhenWinClose = DevSidecar.api.config.get().app.dock.hideWhenWinClose || false
// Scheme must be registered before the app is ready
protocol.registerSchemesAsPrivileged([
@ -362,129 +368,139 @@ function initApp () {
}
// -------------执行开始---------------
app.disableHardwareAcceleration() // 禁用gpu
try {
app.disableHardwareAcceleration() // 禁用gpu
// 开启后是否默认隐藏window
let startHideWindow = !DevSidecar.api.config.get().app.startShowWindow
if (app.getLoginItemSettings().wasOpenedAsHidden) {
startHideWindow = true
} else if (process.argv) {
const args = minimist(process.argv)
log.info('start args:', args)
// 通过启动参数,判断是否隐藏窗口
const hideWindowArg = `${args.hideWindow}`
if (hideWindowArg === 'true' || hideWindowArg === '1') {
// 开启后是否默认隐藏window
let startHideWindow = !DevSidecar.api.config.get().app.startShowWindow
if (app.getLoginItemSettings().wasOpenedAsHidden) {
startHideWindow = true
} else if (hideWindowArg === 'false' || hideWindowArg === '0') {
startHideWindow = false
} else if (process.argv) {
const args = minimist(process.argv)
log.info('start args:', args)
// 通过启动参数,判断是否隐藏窗口
const hideWindowArg = `${args.hideWindow}`
if (hideWindowArg === 'true' || hideWindowArg === '1') {
startHideWindow = true
} else if (hideWindowArg === 'false' || hideWindowArg === '0') {
startHideWindow = false
}
}
}
log.info('startHideWindow = ', startHideWindow, ', app.getLoginItemSettings() = ', jsonApi.stringify2(app.getLoginItemSettings()))
log.info('startHideWindow = ', startHideWindow, ', app.getLoginItemSettings() = ', jsonApi.stringify2(app.getLoginItemSettings()))
// 禁止双开
const isFirstInstance = app.requestSingleInstanceLock()
if (!isFirstInstance) {
log.info('is second instance')
setTimeout(() => {
app.quit()
}, 1000)
} else {
app.on('before-quit', async () => {
log.info('before-quit')
if (process.platform === 'darwin') {
quit()
}
})
app.on('will-quit', () => {
log.info('应用关闭,注销所有快捷键')
globalShortcut.unregisterAll()
})
app.on('second-instance', (event, commandLine) => {
log.info('new app started, command:', commandLine)
if (win) {
showWin()
win.focus()
}
})
// Quit when all windows are closed.
app.on('window-all-closed', () => {
log.info('window-all-closed')
// On macOS it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== 'darwin') {
quit()
}
})
app.on('activate', () => {
// On macOS it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (win == null) {
createWindow(false)
} else {
showWin()
}
})
// initApp()
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', async () => {
if (isDevelopment && !process.env.IS_TEST) {
// Install Vue Devtools
// try {
// await installExtension(VUEJS_DEVTOOLS)
// } catch (e) {
// log.error('Vue Devtools failed to install:', e.toString())
// }
}
try {
createWindow(startHideWindow)
const context = { win, app, beforeQuit, quit, ipcMain, dialog, log, api: DevSidecar.api, changeAppConfig }
backend.install(context) // 模块安装
} catch (err) {
log.info('error:', err)
}
try {
// 最小化到托盘
tray = setTray()
} catch (err) {
log.info('error:', err)
}
_powerMonitor.on('shutdown', async (e) => {
if (e) {
e.preventDefault()
}
log.info('系统关机,恢复代理设置')
await quit()
})
})
}
initApp()
// Exit cleanly on request from parent process in development mode.
if (isDevelopment) {
if (process.platform === 'win32') {
process.on('message', (data) => {
if (data === 'graceful-exit') {
// 禁止双开
const isFirstInstance = app.requestSingleInstanceLock()
if (!isFirstInstance) {
log.info('is second instance')
setTimeout(() => {
app.quit()
}, 1000)
} else {
app.on('before-quit', async () => {
log.info('before-quit')
if (process.platform === 'darwin') {
quit()
}
})
} else {
process.on('SIGINT', () => {
quit()
app.on('will-quit', () => {
log.info('应用关闭,注销所有快捷键')
globalShortcut.unregisterAll()
})
app.on('second-instance', (event, commandLine) => {
log.info('new app started, command:', commandLine)
if (win) {
showWin()
win.focus()
}
})
// Quit when all windows are closed.
app.on('window-all-closed', () => {
log.info('window-all-closed')
// On macOS it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== 'darwin') {
quit()
}
})
app.on('activate', () => {
// On macOS it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (win == null) {
createWindow(false)
} else {
showWin()
}
})
// initApp()
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', async () => {
if (isDevelopment && !process.env.IS_TEST) {
// Install Vue Devtools
// try {
// await installExtension(VUEJS_DEVTOOLS)
// } catch (e) {
// log.error('Vue Devtools failed to install:', e.toString())
// }
}
try {
createWindow(startHideWindow)
} catch (err) {
log.error('createWindow error:', err)
}
try {
const context = { win, app, beforeQuit, quit, ipcMain, dialog, log, api: DevSidecar.api, changeAppConfig }
backend.install(context) // 模块安装
} catch (err) {
log.error('install modules error:', err)
}
try {
// 最小化到托盘
tray = setTray()
} catch (err) {
log.error('setTray error:', err)
}
_powerMonitor.on('shutdown', async (e) => {
if (e) {
e.preventDefault()
}
log.info('系统关机,恢复代理设置')
await quit()
})
})
}
initApp()
// Exit cleanly on request from parent process in development mode.
if (isDevelopment) {
if (process.platform === 'win32') {
process.on('message', (data) => {
if (data === 'graceful-exit') {
quit()
}
})
} else {
process.on('SIGINT', () => {
quit()
})
}
}
// 系统关机和重启时的操作
process.on('exit', () => {
log.info('进程结束退出app')
quit()
})
} catch (e) {
log.error('应用启动过程中,出现未知异常:', e)
}
// 系统关机和重启时的操作
process.on('exit', () => {
log.info('进程结束退出app')
quit()
})

View File

@ -38,7 +38,7 @@ const api = {
}
})
newServer.on('error', (e) => {
log.info('server error', e)
log.error('server error', e)
// newServer = null
fireError(e)
})

View File

@ -23,7 +23,7 @@ module.exports = class DNSOverIpAddress extends BaseDNS {
log.info(`[dns] get ${hostname} ipaddress:${ip}`)
return [ip]
}
log.info(`[dns] get ${hostname} ipaddress: error`)
log.warn(`[dns] get ${hostname} ipaddress: error`)
return null
// const { answers } = await dnstls.query(hostname)

View File

@ -175,7 +175,7 @@ module.exports = {
next() // 异步执行完继续next
}
}).catch((err) => {
log.info('baiduOcr error:', err)
log.error('baiduOcr error:', err)
res.writeHead(200, headers)
res.write(`{"error_code": 999500, "error_msg": "${err}"}`) // 格式如:{"words_result":[{"words":"6525"}],"words_result_num":1,"log_id":1818877093747960000}
res.end()