From 82e53b4789621909d20e09ec2ae908a14fbd1d8c Mon Sep 17 00:00:00 2001 From: fzk <458813868@qq.com> Date: Fri, 22 Dec 2023 17:19:00 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=E6=97=A5=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/desktop/CHANGELOG.md | 4 + packages/desktop/electron/main/contract.ts | 6 +- packages/desktop/electron/main/index.ts | 3 + packages/desktop/electron/main/ipcMain.ts | 8 ++ packages/desktop/electron/main/logger.ts | 79 +++++++++++++++++++ .../desktop/electron/main/serverProcess.ts | 3 + .../desktop/electron/preload/electronAPI.ts | 3 +- packages/desktop/electron/win/viewImageWin.ts | 8 +- packages/desktop/package.json | 3 +- .../editGif/VideoToGifConverter.tsx | 1 - 10 files changed, 108 insertions(+), 10 deletions(-) create mode 100644 packages/desktop/electron/main/logger.ts diff --git a/packages/desktop/CHANGELOG.md b/packages/desktop/CHANGELOG.md index bede3b23..e869981a 100644 --- a/packages/desktop/CHANGELOG.md +++ b/packages/desktop/CHANGELOG.md @@ -1,5 +1,9 @@ # @pear-rec/desktop +## 1.3.7 + +feat: 增加日志 + ## 1.3.6 feat: 修改GIF diff --git a/packages/desktop/electron/main/contract.ts b/packages/desktop/electron/main/contract.ts index e15030c6..2b796711 100644 --- a/packages/desktop/electron/main/contract.ts +++ b/packages/desktop/electron/main/contract.ts @@ -1,4 +1,4 @@ -import { app } from 'electron'; +import { homedir } from 'node:os'; import path from 'node:path'; process.env.DIST_ELECTRON = path.join(__dirname, '../'); @@ -21,7 +21,7 @@ export const ICON = url : path.join(DIST, './imgs/logo/favicon.ico'); export const PEAR_FILES = path.join(DIST_ELECTRON, `../Pear Files`); -const DOCS_PATH = app.getPath('documents'); +export const DOCS_PATH = path.join(homedir(), 'Documents'); export const filePath = path.join(DOCS_PATH, `Pear Files`); @@ -32,3 +32,5 @@ export const CONFIG_FILE_PATH = path.join(PEAR_FILES_PATH, `config.json`); export const DEFAULT_CONFIG_FILE_PATH = path.join(PEAR_FILES_PATH, `default-config.json`); export const DB_PATH = path.join(PEAR_FILES_PATH, 'db/pear-rec.db'); + +export const LOG_PATH = path.join(PEAR_FILES_PATH, 'log'); diff --git a/packages/desktop/electron/main/index.ts b/packages/desktop/electron/main/index.ts index cadf5c1b..08c002ff 100644 --- a/packages/desktop/electron/main/index.ts +++ b/packages/desktop/electron/main/index.ts @@ -8,6 +8,7 @@ import { registerGlobalShortcut, unregisterAllGlobalShortcut } from './globalSho import { initConfig, getConfig } from '@pear-rec/server/src/config'; import { initServerProcess, quitServerProcess } from './serverProcess'; import './ipcMain'; +import './logger'; initServerProcess(); initConfig(); @@ -31,6 +32,8 @@ if (release().startsWith('6.1')) app.disableHardwareAcceleration(); // Set application name for Windows 10+ notifications if (process.platform === 'win32') app.setAppUserModelId(app.getName()); +app.commandLine.appendSwitch('in-process-gpu'); + if (!app.requestSingleInstanceLock()) { app.quit(); process.exit(0); diff --git a/packages/desktop/electron/main/ipcMain.ts b/packages/desktop/electron/main/ipcMain.ts index 6d9c437e..cc18d5c5 100644 --- a/packages/desktop/electron/main/ipcMain.ts +++ b/packages/desktop/electron/main/ipcMain.ts @@ -16,6 +16,7 @@ import * as recorderFullScreenWin from '../win/recorderFullScreenWin'; import * as editGifWin from '../win/editGifWin'; import * as utils from './utils'; import { editConfig } from '@pear-rec/server/src/config'; +import logger from './logger'; const selfWindws = async () => await Promise.all( @@ -39,6 +40,12 @@ const selfWindws = async () => ); function initIpcMain() { + // 日志 + ipcMain.on('lg:send-msg', (e, msg) => { + logger.info(msg); + }); + + // 主页 ipcMain.on('ma:open-win', () => { mainWin.openMainWin(); }); @@ -165,6 +172,7 @@ function initIpcMain() { }); // 图片展示 ipcMain.on('vi:open-win', (e, search) => { + viewImageWin.closeViewImageWin(); viewImageWin.openViewImageWin(search); }); ipcMain.on('vi:close-win', () => { diff --git a/packages/desktop/electron/main/logger.ts b/packages/desktop/electron/main/logger.ts new file mode 100644 index 00000000..ea976840 --- /dev/null +++ b/packages/desktop/electron/main/logger.ts @@ -0,0 +1,79 @@ +// logger.ts +import { app } from 'electron'; +import log from 'electron-log'; +import path from 'node:path'; +import { LOG_PATH } from './contract'; + +// 关闭控制台打印 +log.transports.console.level = false; +log.transports.file.level = 'debug'; +log.transports.file.maxSize = 10024300; // 文件最大不超过 10M +// 输出格式 +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 = () => path.join(LOG_PATH, dateStr + '.log'); + +// 有六个日志级别error, warn, info, verbose, debug, silly。默认是silly +export default { + info(param: any) { + log.info(param); + }, + warn(param: any) { + log.warn(param); + }, + error(param: any) { + log.error(param); + }, + debug(param: any) { + log.debug(param); + }, + verbose(param: any) { + log.verbose(param); + }, + silly(param: any) { + log.silly(param); + }, +}; + +app.on('ready', async () => { + // 渲染进程崩溃 + app.on('renderer-process-crashed', (event, webContents, killed) => { + log.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) => { + log.error( + `APP-ERROR:gpu-process-crashed; event: ${JSON.stringify(event)}; killed: ${JSON.stringify( + killed, + )}`, + ); + }); + + // 渲染进程结束 + app.on('render-process-gone', async (event, webContents, details) => { + log.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) => { + log.error( + `APP-ERROR:child-process-gone; event: ${JSON.stringify(event)}; details:${JSON.stringify( + details, + )}`, + ); + }); +}); diff --git a/packages/desktop/electron/main/serverProcess.ts b/packages/desktop/electron/main/serverProcess.ts index e770f5d7..a1330323 100644 --- a/packages/desktop/electron/main/serverProcess.ts +++ b/packages/desktop/electron/main/serverProcess.ts @@ -1,5 +1,6 @@ import { type UtilityProcess, utilityProcess } from 'electron'; import { url, serverPath } from './contract'; +import logger from './logger'; let serverProcess: null | UtilityProcess = null; @@ -13,9 +14,11 @@ export function initServerProcess() { serverProcess.on?.('spawn', () => { serverProcess.stdout?.on('data', (data) => { console.log(`serverProcess output: ${data}`); + logger.info(`serverProcess output: ${data}`); }); serverProcess.stderr?.on('data', (data) => { console.error(`serverProcess err: ${data}`); + logger.error(`serverProcess output: ${data}`); }); }); } diff --git a/packages/desktop/electron/preload/electronAPI.ts b/packages/desktop/electron/preload/electronAPI.ts index 7be4cef8..d9f85c4a 100644 --- a/packages/desktop/electron/preload/electronAPI.ts +++ b/packages/desktop/electron/preload/electronAPI.ts @@ -1,7 +1,8 @@ import { contextBridge, ipcRenderer } from 'electron'; contextBridge.exposeInMainWorld('electronAPI', { - setTitle: (title: any) => ipcRenderer.send('set-title', title), + // logger + sendLogger: (msg: any) => ipcRenderer.send('lg:send-msg', msg), // mainWin sendMaOpenWin: () => ipcRenderer.send('ma:open-win'), diff --git a/packages/desktop/electron/win/viewImageWin.ts b/packages/desktop/electron/win/viewImageWin.ts index 858c922a..14790b6f 100644 --- a/packages/desktop/electron/win/viewImageWin.ts +++ b/packages/desktop/electron/win/viewImageWin.ts @@ -28,10 +28,6 @@ function createViewImageWin(search?: any): BrowserWindow { }); } - viewImageWin.once('ready-to-show', async () => { - viewImageWin?.show(); - }); - return viewImageWin; } @@ -43,7 +39,9 @@ function openViewImageWin(search?: any) { } function closeViewImageWin() { - viewImageWin?.close(); + if (!(viewImageWin && viewImageWin.isDestroyed())) { + viewImageWin?.close(); + } viewImageWin = null; } diff --git a/packages/desktop/package.json b/packages/desktop/package.json index 02484481..568f4144 100644 --- a/packages/desktop/package.json +++ b/packages/desktop/package.json @@ -33,6 +33,7 @@ "autoprefixer": "^10.4.16", "electron": "^26.0.0", "electron-builder": "^24.9.1", + "electron-log": "^5.0.1", "jsonfile": "^6.1.0", "typescript": "^5.2.2", "uuid": "^9.0.1", @@ -42,4 +43,4 @@ "engines": { "node": "^14.18.0 || >=16.0.0" } -} \ No newline at end of file +} diff --git a/packages/web/src/components/editGif/VideoToGifConverter.tsx b/packages/web/src/components/editGif/VideoToGifConverter.tsx index 067c48d3..bb062fe4 100644 --- a/packages/web/src/components/editGif/VideoToGifConverter.tsx +++ b/packages/web/src/components/editGif/VideoToGifConverter.tsx @@ -130,7 +130,6 @@ export default function VideoToGifConverter({ videoSrc, user }) { - GIF );