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

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