feat(log): 添加软件日志功能

- 新增 SoftwareLog 相关接口和数据结构
- 实现日志数据的收集和展示
- 添加日志相关的国际化支持
- 优化控制页面布局,支持日志切换显示
This commit is contained in:
himeditator
2025-08-19 22:23:54 +08:00
parent 01936d5f12
commit 771f7ad002
21 changed files with 446 additions and 166 deletions

View File

@@ -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', () => {

View File

@@ -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 {

View File

@@ -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
}
}

View File

@@ -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)

View File

@@ -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)
}
}