Skip to content

Commit

Permalink
optimize: 捕获应用启动过程中的异常,并记录日志。
Browse files Browse the repository at this point in the history
  • Loading branch information
wangliang181230 committed Jan 22, 2025
1 parent 2a75503 commit 6d57101
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 117 deletions.
2 changes: 1 addition & 1 deletion packages/core/src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ const configApi = {
log.info('读取远程配置文件内容成功:', path)
return file.toString()
} else {
log.warn('远程配置文件不存在:', path)
log.info('远程配置文件不存在:', path)
}
} catch (e) {
log.error('读取远程配置文件内容失败:', e)
Expand Down
242 changes: 129 additions & 113 deletions packages/gui/src/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand Down Expand Up @@ -362,129 +368,139 @@ function initApp () {
}

// -------------执行开始---------------
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') {
try {
app.disableHardwareAcceleration() // 禁用gpu

// 开启后是否默认隐藏window
let startHideWindow = !DevSidecar.api.config.get().app.startShowWindow
if (app.getLoginItemSettings().wasOpenedAsHidden) {
startHideWindow = true
} else if (hideWindowArg === 'false' || hideWindowArg === '0') {
startHideWindow = false
}
}
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()
} 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
}
})
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()
}
})
}
log.info('startHideWindow = ', startHideWindow, ', app.getLoginItemSettings() = ', jsonApi.stringify2(app.getLoginItemSettings()))

// 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()
}
})
// 禁止双开
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()
}
})

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()
}
})
// 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()
}
})

// 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)
}
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()
}
})

try {
// 最小化到托盘
tray = setTray()
} catch (err) {
log.info('error:', err)
}
// 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())
// }
}

_powerMonitor.on('shutdown', async (e) => {
if (e) {
e.preventDefault()
try {
createWindow(startHideWindow)
} catch (err) {
log.error('createWindow error:', err)
}
log.info('系统关机,恢复代理设置')
await quit()
})
})
}

initApp()
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)
}

// 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()
try {
// 最小化到托盘
tray = setTray()
} catch (err) {
log.error('setTray error:', err)
}

_powerMonitor.on('shutdown', async (e) => {
if (e) {
e.preventDefault()
}
log.info('系统关机,恢复代理设置')
await quit()
})
})
} else {
process.on('SIGINT', () => {
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()
})
2 changes: 1 addition & 1 deletion packages/mitmproxy/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const api = {
}
})
newServer.on('error', (e) => {
log.info('server error', e)
log.error('server error', e)
// newServer = null
fireError(e)
})
Expand Down
2 changes: 1 addition & 1 deletion packages/mitmproxy/src/lib/dns/ipaddress.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 6d57101

Please sign in to comment.