From 9510a4cb6784e85fa696b628b5d0c591f4cd9af3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E5=98=89=E4=BC=9F?= <8473136@qq.com> Date: Wed, 8 Jan 2025 10:45:54 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Refactor=20logging=20and=20applicat?= =?UTF-8?q?ion=20initialization:=20Introduced=20a=20new=20logging=20utilit?= =?UTF-8?q?y=20for=20structured=20logging=20across=20the=20application.=20?= =?UTF-8?q?Enhanced=20the=20main=20process=20to=20log=20application=20even?= =?UTF-8?q?ts=20and=20errors,=20improving=20traceability=20and=20debugging?= =?UTF-8?q?.=20Cleaned=20up=20the=20code=20by=20removing=20deprecated=20co?= =?UTF-8?q?mments=20and=20unused=20imports,=20and=20ensured=20proper=20err?= =?UTF-8?q?or=20handling=20during=20configuration=20retrieval=20and=20API?= =?UTF-8?q?=20initialization.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- electron/main/index.ts | 140 ++++++++++++++++++++++++++--------------- electron/utils/log.ts | 31 +++++++++ 2 files changed, 120 insertions(+), 51 deletions(-) create mode 100644 electron/utils/log.ts diff --git a/electron/main/index.ts b/electron/main/index.ts index 93743bb..b6b06ed 100644 --- a/electron/main/index.ts +++ b/electron/main/index.ts @@ -21,25 +21,23 @@ import { import { initLoggerApi } from "../api/logger"; import { initFileApi } from "../api/file"; import { getConfig } from "../storage/config"; -import log from "electron-log"; import { initCommonApi } from "../api/common"; import { initLocalApi } from "../api/local"; -// The built directory structure -// -// ├─┬ dist-electron -// │ ├─┬ main -// │ │ └── index.js > Electron-Main -// │ └─┬ preload -// │ └── index.js > Preload-Scripts -// ├─┬ dist -// │ └── index.html > Electron-Renderer -// +import { logError, logInfo, LogModule } from "../utils/log"; + process.env.DIST_ELECTRON = join(__dirname, ".."); process.env.DIST = join(process.env.DIST_ELECTRON, "../dist"); process.env.VITE_PUBLIC = process.env.VITE_DEV_SERVER_URL ? join(process.env.DIST_ELECTRON, "../public") : process.env.DIST; +let win: BrowserWindow | null = null; +let tray = null; +const preload = join(__dirname, "../preload/index.js"); +const url = process.env.VITE_DEV_SERVER_URL; +const indexHtml = join(process.env.DIST, "index.html"); +let isQuiting; + // Disable GPU Acceleration for Windows 7 if (release().startsWith("6.1")) app.disableHardwareAcceleration(); @@ -51,28 +49,11 @@ if (!app.requestSingleInstanceLock()) { process.exit(0); } -// Remove electron security warnings -// This warning only shows in development mode -// Read more on https://www.electronjs.org/docs/latest/tutorial/security -// process.env['ELECTRON_DISABLE_SECURITY_WARNINGS'] = 'true' - -let win: BrowserWindow | null = null; -let tray = null; -// Here, you can also use other preload -const preload = join(__dirname, "../preload/index.js"); -const url = process.env.VITE_DEV_SERVER_URL; -const indexHtml = join(process.env.DIST, "index.html"); -let isQuiting; -log.transports.file.level = "debug"; -log.transports.console.level = "debug"; - async function createWindow(config: FrpConfig) { - console.log("config", config); let show = true; if (config) { show = !config.systemSilentStartup; } - console.log("界面", show); win = new BrowserWindow({ title: "Frpc Desktop", icon: join(process.env.VITE_PUBLIC, "logo/only/16x16.png"), @@ -138,11 +119,6 @@ async function createWindow(config: FrpConfig) { } export const createTray = (config: FrpConfig) => { - log.info( - `当前环境 platform:${process.platform} arch:${ - process.arch - } appData:${app.getPath("userData")} version:${app.getVersion()}` - ); let menu: Array = [ { label: "显示主窗口", @@ -177,40 +153,87 @@ export const createTray = (config: FrpConfig) => { if (config) { if (config.systemStartupConnect) { - log.info(`已开启自动连接 正在自动连接服务器`); startFrpWorkerProcess(config); } } }; - app.whenReady().then(() => { + logInfo( + LogModule.APP, + `Application started. Current system architecture: ${ + process.arch + }, platform: ${process.platform}, version: ${app.getVersion()}.` + ); + getConfig((err, config) => { - createWindow(config).then(r => { - createTray(config); - // 初始化各个API - initGitHubApi(); - initConfigApi(win); - initProxyApi(); - initFrpcApi(); - initLoggerApi(); - initFileApi(); - initCommonApi(); - initLocalApi(); - // initUpdaterApi(win); - }); + if (err) { + logError(LogModule.APP, `Failed to get config: ${err.message}`); + return; + } + + const { serverAddr, serverPort, authToken, ...sensitiveData } = config; + logInfo(LogModule.APP, `Config retrieved: ${JSON.stringify({ ...sensitiveData, serverAddr: '**', serverPort: '**', authToken: '**' })}`); + + createWindow(config) + .then(r => { + logInfo(LogModule.APP, `Window created successfully.`); + createTray(config); + logInfo(LogModule.APP, `Tray created successfully.`); + + // Initialize APIs + try { + initGitHubApi(); + logInfo(LogModule.APP, `GitHub API initialized.`); + + initConfigApi(win); + logInfo(LogModule.APP, `Config API initialized.`); + + initProxyApi(); + logInfo(LogModule.APP, `Proxy API initialized.`); + + initFrpcApi(); + logInfo(LogModule.APP, `FRPC API initialized.`); + + initLoggerApi(); + logInfo(LogModule.APP, `Logger API initialized.`); + + initFileApi(); + logInfo(LogModule.APP, `File API initialized.`); + + initCommonApi(); + logInfo(LogModule.APP, `Common API initialized.`); + + initLocalApi(); + logInfo(LogModule.APP, `Local API initialized.`); + + // initUpdaterApi(win); + logInfo(LogModule.APP, `Updater API initialization skipped.`); + } catch (error) { + logError( + LogModule.APP, + `Error during API initialization: ${error.message}` + ); + } + }) + .catch(error => { + logError(LogModule.APP, `Error creating window: ${error.message}`); + }); }); }); app.on("window-all-closed", () => { + logInfo(LogModule.APP, `All windows closed.`); win = null; if (process.platform !== "darwin") { stopFrpcProcess(() => { + logInfo(LogModule.APP, `FRPC process stopped. Quitting application.`); app.quit(); }); } }); app.on("second-instance", () => { + logInfo(LogModule.APP, `Second instance detected.`); if (win) { // Focus on the main window if the user tried to open another if (win.isMinimized()) win.restore(); @@ -219,23 +242,33 @@ app.on("second-instance", () => { }); app.on("activate", () => { + logInfo(LogModule.APP, `Application activated.`); const allWindows = BrowserWindow.getAllWindows(); if (allWindows.length) { allWindows[0].focus(); } else { getConfig((err, config) => { - createWindow(config).then(r => {}); + if (err) { + logError( + LogModule.APP, + `Failed to get config on activate: ${err.message}` + ); + return; + } + createWindow(config).then(r => { + logInfo(LogModule.APP, `Window created on activate.`); + }); }); } }); app.on("before-quit", () => { - log.info("before-quit"); + logInfo(LogModule.APP, `Application is about to quit.`); isQuiting = true; }); -// New window example arg: new windows url ipcMain.handle("open-win", (_, arg) => { + logInfo(LogModule.APP, `Opening new window with argument: ${arg}`); const childWindow = new BrowserWindow({ webPreferences: { preload, @@ -246,7 +279,12 @@ ipcMain.handle("open-win", (_, arg) => { if (process.env.VITE_DEV_SERVER_URL) { childWindow.loadURL(`${url}#${arg}`); + logInfo(LogModule.APP, `Child window loaded URL: ${url}#${arg}`); } else { childWindow.loadFile(indexHtml, { hash: arg }); + logInfo( + LogModule.APP, + `Child window loaded file: ${indexHtml} with hash: ${arg}` + ); } }); diff --git a/electron/utils/log.ts b/electron/utils/log.ts new file mode 100644 index 0000000..9040e48 --- /dev/null +++ b/electron/utils/log.ts @@ -0,0 +1,31 @@ +import log from "electron-log"; +// 定义模块枚举 +export enum LogModule { + APP = "app", + FRP_CLIENT = "frpc client", + PROXY = "proxy", + DOWNLOAD = "download", + SETTING = "setting", + LOGGER = "logger" +} + +// 设置日志级别 +log.transports.file.level = "debug"; // 可以根据需要调整日志级别 +log.transports.console.level = "debug"; // 控制台输出日志级别 + +// 自定义日志输出函数,记录到指定业务模块 +export const logInfo = (module: LogModule, message: string) => { + log.info(`[${module}] ${message}`); +}; + +export const logError = (module: LogModule, message: string) => { + log.error(`[${module}] ${message}`); +}; + +export const logDebug = (module: LogModule, message: string) => { + log.debug(`[${module}] ${message}`); +}; + +export const logWarn = (module: LogModule, message: string) => { + log.warn(`[${module}] ${message}`); +};