diff --git a/docs/api-docs/electron-ipc.md b/docs/api-docs/electron-ipc.md index 07aa240..f6965dd 100644 --- a/docs/api-docs/electron-ipc.md +++ b/docs/api-docs/electron-ipc.md @@ -284,6 +284,16 @@ **数据类型:** `Controls` +### `control.softwareLog.add` + +**介绍:** 添加一条新的日志数据 + +**发起方:** 后端 + +**接收方:** 前端控制窗口 + +**数据类型:** `SoftwareLog` + ### `both.styles.set` **介绍:** 后端将最新字幕样式发送给前端,前端进行设置 diff --git a/engine/main.py b/engine/main.py index 063ed71..23f9a09 100644 --- a/engine/main.py +++ b/engine/main.py @@ -76,7 +76,7 @@ if __name__ == "__main__": # vosk only parser.add_argument('-m', '--model_path', default='', help='The path to the vosk model.') - args = parser.parse_args() + args = parser.parse_args() if int(args.port) == 0: thread_data.status = "running" else: diff --git a/src/main/ControlWindow.ts b/src/main/ControlWindow.ts index db3b236..524170a 100644 --- a/src/main/ControlWindow.ts +++ b/src/main/ControlWindow.ts @@ -7,8 +7,10 @@ import icon from '../../build/icon.png?asset' import { captionWindow } from './CaptionWindow' import { allConfig } from './utils/AllConfig' import { captionEngine } from './utils/CaptionEngine' +import { Log } from './utils/Log' class ControlWindow { + mounted: boolean = false; window: BrowserWindow | undefined; public createWindow(): void { @@ -34,6 +36,7 @@ class ControlWindow { }) this.window.on('closed', () => { + this.mounted = false this.window = undefined allConfig.writeConfig() }) @@ -63,7 +66,8 @@ class ControlWindow { }) ipcMain.handle('both.window.mounted', () => { - return allConfig.getFullConfig() + this.mounted = true + return allConfig.getFullConfig(Log.getAndClearLogQueue()) }) ipcMain.handle('control.nativeTheme.get', () => { diff --git a/src/main/types/index.ts b/src/main/types/index.ts index 241725d..dc93991 100644 --- a/src/main/types/index.ts +++ b/src/main/types/index.ts @@ -45,6 +45,13 @@ export interface CaptionItem { translation: string } +export interface SoftwareLogItem { + type: "INFO" | "WARN" | "ERROR", + index: number, + time: string, + text: string +} + export interface FullConfig { platform: string, uiLanguage: UILanguage, @@ -53,7 +60,8 @@ export interface FullConfig { leftBarWidth: number, styles: Styles, controls: Controls, - captionLog: CaptionItem[] + captionLog: CaptionItem[], + softwareLog: SoftwareLogItem[] } export interface EngineInfo { diff --git a/src/main/utils/AllConfig.ts b/src/main/utils/AllConfig.ts index ffb97bf..78ace77 100644 --- a/src/main/utils/AllConfig.ts +++ b/src/main/utils/AllConfig.ts @@ -1,6 +1,6 @@ import { UILanguage, UITheme, Styles, Controls, - CaptionItem, FullConfig + CaptionItem, FullConfig, SoftwareLogItem } from '../types' import { Log } from './Log' import { app, BrowserWindow } from 'electron' @@ -88,7 +88,7 @@ class AllConfig { Log.info('Write Config to:', configPath) } - public getFullConfig(): FullConfig { + public getFullConfig(softwareLog: SoftwareLogItem[]): FullConfig { return { platform: process.platform, uiLanguage: this.uiLanguage, @@ -97,7 +97,8 @@ class AllConfig { leftBarWidth: this.leftBarWidth, styles: this.styles, controls: this.controls, - captionLog: this.captionLog + captionLog: this.captionLog, + softwareLog: softwareLog } } diff --git a/src/main/utils/CaptionEngine.ts b/src/main/utils/CaptionEngine.ts index 2ea1a8a..f199833 100644 --- a/src/main/utils/CaptionEngine.ts +++ b/src/main/utils/CaptionEngine.ts @@ -85,12 +85,16 @@ export class CaptionEngine { } } Log.info('Engine Path:', this.appPath) - Log.info('Engine Command:', this.command) + if(this.command.length > 2 && this.command.at(-2) === '-k') { + const _command = [...this.command] + _command[_command.length -1] = _command[_command.length -1].replace(/./g, '*') + Log.info('Engine Command:', _command) + } + else Log.info('Engine Command:', this.command) return true } public connect() { - Log.info('Connecting to caption engine server...') if(this.client) { Log.warn('Client already exists, ignoring...') } this.client = net.createConnection({ port: this.port }, () => { Log.info('Connected to caption engine server'); @@ -177,7 +181,6 @@ export class CaptionEngine { this.client = undefined } this.status = 'stopping' - Log.info('Caption engine process stopping...') this.timerID = setTimeout(() => { if(this.status !== 'stopping') return Log.warn('Engine process still not stopped, trying to kill...') @@ -226,7 +229,7 @@ function handleEngineData(data: any) { Log.info('Engine Info:', data.content) } else if(data.command === 'usage') { - Log.info('Engine Usage: ', data.content) + Log.info('Engine Token Usage: ', data.content) } else { Log.warn('Unknown command:', data) diff --git a/src/main/utils/Log.ts b/src/main/utils/Log.ts index c226c08..7a36842 100644 --- a/src/main/utils/Log.ts +++ b/src/main/utils/Log.ts @@ -1,3 +1,9 @@ +import { controlWindow } from "../ControlWindow" +import { type SoftwareLogItem } from "../types" + +let logIndex = 0 +const logQueue: SoftwareLogItem[] = [] + function getTimeString() { const now = new Date() const HH = String(now.getHours()).padStart(2, '0') @@ -8,15 +14,45 @@ function getTimeString() { } export class Log { + static getAndClearLogQueue() { + const copiedQueue = structuredClone(logQueue) + logQueue.length = 0 + return copiedQueue + } + + static handleLog(logType: "INFO" | "WARN" | "ERROR", ...msg: any[]) { + const timeStr = getTimeString() + const logPre = `[${logType} ${timeStr}]` + let logStr = "" + for(let i = 0; i < msg.length; i++) { + logStr += i ? " " : "" + if(typeof msg[i] === "string") logStr += msg[i] + else logStr += JSON.stringify(msg[i], undefined, 2) + } + console.log(logPre, logStr) + const logItem: SoftwareLogItem = { + type: logType, + index: ++logIndex, + time: timeStr, + text: logStr + } + if(controlWindow.mounted && controlWindow.window) { + controlWindow.window.webContents.send('control.softwareLog.add', logItem) + } + else { + logQueue.push(logItem) + } + } + static info(...msg: any[]){ - console.log(`[INFO ${getTimeString()}]`, ...msg) + this.handleLog("INFO", ...msg) } static warn(...msg: any[]){ - console.warn(`[WARN ${getTimeString()}]`, ...msg) + this.handleLog("WARN", ...msg) } static error(...msg: any[]){ - console.error(`[ERROR ${getTimeString()}]`, ...msg) + this.handleLog("ERROR", ...msg) } } diff --git a/src/renderer/index.html b/src/renderer/index.html index 2c8b1d7..4d642df 100644 --- a/src/renderer/index.html +++ b/src/renderer/index.html @@ -2,7 +2,7 @@ - Auto Caption + Auto Caption v0.7.0 { useEngineControlStore().platform = data.platform useEngineControlStore().setControls(data.controls) useCaptionLogStore().captionData = data.captionLog + useSoftwareLogStore().softwareLogs = data.softwareLog }) }) diff --git a/src/renderer/src/components/CaptionLog.vue b/src/renderer/src/components/CaptionLog.vue index 5dace1d..e56a881 100644 --- a/src/renderer/src/components/CaptionLog.vue +++ b/src/renderer/src/components/CaptionLog.vue @@ -1,141 +1,139 @@