zdg #165

Merged
zhengdegang merged 2 commits from zdg into main 2024-09-02 13:53:23 +08:00
4 changed files with 113 additions and 8 deletions
Showing only changes of commit 3f4e9c35b0 - Show all commits

View File

@ -3,15 +3,17 @@ import { join } from 'path'
import { electronApp, optimizer, is } from '@electron-toolkit/utils' import { electronApp, optimizer, is } from '@electron-toolkit/utils'
import icon from '../../resources/icon.png?asset' import icon from '../../resources/icon.png?asset'
import File from './file' import File from './file'
import Logger from './logger' // 日志封装
import chat from './chat' // chat封装 import chat from './chat' // chat封装
import Store from './store' // Store封装 import Store from './store' // Store封装
import updateInit from './update' import updateInit from './update'
// 代理 electron/remote // 代理 electron/remote
// 第一步引入remote // 第一步引入remote
import remote from '@electron/remote/main' import remote from '@electron/remote/main'
// 第二步: 初始化remote // 第二步: 初始化remote
remote.initialize() remote.initialize()
// 日志配置-初始化(日志直接绑定到console上)
Logger.initialize()
// 持久化数据-初始化 // 持久化数据-初始化
Store.initialize() Store.initialize()
@ -153,6 +155,7 @@ async function createLinkWin(data) {
// 初始化完成 // 初始化完成
app.on('ready', () => { app.on('ready', () => {
appWatchError() // 监听app错误
process.env.LANG = 'en_US.UTF-8' process.env.LANG = 'en_US.UTF-8'
// 设置应用程序用户模型标识符 // 设置应用程序用户模型标识符
electronApp.setAppUserModelId('com.electron') electronApp.setAppUserModelId('com.electron')
@ -259,3 +262,34 @@ function handleAll() {
win.webContents.send('pinia-state-set', storeName, jsonStr) win.webContents.send('pinia-state-set', storeName, jsonStr)
}) })
} }
// app 崩溃监听器
function appWatchError() {
// 渲染进程崩溃
app.on('renderer-process-crashed', (event, webContents, killed) => {
console.error(
`APP-ERROR:renderer-process-crashed; event: ${JSON.stringify(event)}; webContents:${JSON.stringify(
webContents
)}; killed:${JSON.stringify(killed)}`
)
})
// GPU进程崩溃
app.on('gpu-process-crashed', (event, killed) => {
console.error(`APP-ERROR:gpu-process-crashed; event: ${JSON.stringify(event)}; killed: ${JSON.stringify(killed)}`)
})
// 渲染进程结束
app.on('render-process-gone', async (event, webContents, details) => {
console.error(
`APP-ERROR:render-process-gone; event: ${JSON.stringify(event)}; webContents:${JSON.stringify(
webContents
)}; details:${JSON.stringify(details)}`
)
})
// 子进程结束
app.on('child-process-gone', async (event, details) => {
console.error(`APP-ERROR:child-process-gone; event: ${JSON.stringify(event)}; details:${JSON.stringify(details)}`)
})
}

52
src/main/logger.js Normal file
View File

@ -0,0 +1,52 @@
/**
* @description 日志配置
* @author zdg
* @date 2021-07-05 14:07:01
*/
// import log from 'electron-log'
import log from 'electron-log/main'
import { app } from 'electron'
import path from 'path'
// 关闭控制台打印
// 日志控制台等级默认值false
log.transports.console.level = false
// log.transports.console.level = 'info'
// 日志文件等级默认值false
log.transports.file.level = 'info'
// 日志文件名默认main.log
// log.transports.file.fileName = 'main.log';
// 日志大小默认10485761M达到最大上限后备份文件并重命名为main.old.log有且仅有一个备份文件
log.transports.file.maxSize = 10 * 1024 * 1024; // 文件最大不超过 10M
// 自定义日志文件滚动策略
log.transports.file.rollSize = 10 * 1024 * 1024; // 10MB
// 日志格式,默认:[{y}-{m}-{d} {h}:{i}:{s}.{ms}] [{level}]{scope} {text}
log.transports.file.format = '[{y}-{m}-{d} {h}:{i}:{s}.{ms}] [{level}]{scope} {text}'
let date = new Date()
let dateStr = date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate()
// 文件位置及命名方式
// 默认位置为C:\Users\[user]\AppData\Roaming\[appname]\electron_log\
// 文件名为:年-月-日.log
// 自定义文件保存位置为安装目录下 \log\年-月-日.log
// log.transports.file.resolvePathFn = () => 'logs\\' + dateStr+ '.log';
log.transports.file.resolvePathFn = () => path.join(app.getPath('userData'), `logs/${dateStr}.log`)
// 有六个日志级别error, warn, info, verbose, debug, silly。默认是silly
export const logger = {
error: (...args) => log.error(...args),
warn: (...args) => log.warn(...args),
info: (...args) => log.info(...args),
verbose: (...args) => log.verbose(...args),
debug: (...args) => log.debug(...args),
silly: (...args) => log.silly(...args)
}
export function initialize(bool = true, type = 'all') {
log.initialize() // 为渲染器进行初始化
if (bool) { // 是否替换默认的console
if (type == 'all') Object.assign(console, log.functions)
else { // 替换指定类型
console[type] = log[type]
}
}
}
export default { initialize }

View File

@ -19,7 +19,6 @@ const defaultData = {
} }
}, },
local: { // 本地(永久localStorage) local: { // 本地(永久localStorage)
}, },
} }
@ -29,7 +28,15 @@ export function initialize(){
const sessionStore = new Store({ const sessionStore = new Store({
name: 'session-store', // 存储文件名 name: 'session-store', // 存储文件名
fileExtension: 'ini', // 文件后缀名 fileExtension: 'ini', // 文件后缀名
encryptionKey: 'BvPLmgCC4DSIG0KkTec5' // 数据加密-防止用户直接改配置 encryptionKey: 'BvPLmgCC4DSIG0KkTec5', // 数据加密-防止用户直接改配置
beforeEachMigration: (store, context) => { // 版本迁移回调
console.log(`[session-store] 迁移从 ${context.fromVersion}${context.toVersion}`);
},
migrations: { // 版本变化
'0.0.0': store => {
// store.set('debugPhase', true);
}
}
}) })
sessionStore.clear() // 先清除-所有缓存数据 sessionStore.clear() // 先清除-所有缓存数据
sessionStore.set(defaultData.session) // 初始化-默认数据 sessionStore.set(defaultData.session) // 初始化-默认数据
@ -38,7 +45,15 @@ export function initialize(){
const localStore = new Store({ const localStore = new Store({
name: 'local-store', // 存储文件名 name: 'local-store', // 存储文件名
fileExtension: 'ini', // 文件后缀名 fileExtension: 'ini', // 文件后缀名
encryptionKey: '6CyoHQmUaPmLzvVsh' // 数据加密-防止用户直接改配置 encryptionKey: '6CyoHQmUaPmLzvVsh', // 数据加密-防止用户直接改配置
beforeEachMigration: (store, context) => { // 版本迁移回调
console.log(`[local-store] 迁移从 ${context.fromVersion}${context.toVersion}`);
},
migrations: { // 版本变化
'0.0.0': store => {
// store.set('debugPhase', true);
}
}
}) })
localStore.set(defaultData.local) // 初始化-默认数据 localStore.set(defaultData.local) // 初始化-默认数据
return {sessionStore, localStore} return {sessionStore, localStore}

View File

@ -12,10 +12,14 @@ import 'virtual:windi.css'
import { store } from '@/store' import { store } from '@/store'
import App from './App.vue' import App from './App.vue'
import router from './router' import router from './router'
import log from 'electron-log/renderer' // 渲染进程日志-文件记录
if(process.env.NODE_ENV != 'development') { // 非开发环境,将日志打印到日志文件
Object.assign(console, log.functions) // 渲染进程日志-控制台替换
}
const app = createApp(App) const app = createApp(App)
app.use(router) app.use(router)
.use(store) .use(store)
.use(ElementPlus, { locale: zhLocale }).mount('#app') .use(ElementPlus, { locale: zhLocale }).mount('#app')