feat: 添加日志导出功能

This commit is contained in:
dmy
2026-03-03 14:26:58 +08:00
parent 02bfcc18e4
commit 3b590f9a1f
3 changed files with 97 additions and 1 deletions

View File

@@ -67,11 +67,17 @@ const clearLog = () => {
logs.value = []
}
// 获取日志文本
const getLogsText = (): string => {
return logs.value.map(log => `[${log.time}] [${log.level.toUpperCase()}] ${log.message}`).join('\n')
}
// 暴露方法给父组件
defineExpose({
addLog,
clearLog,
lastTripRates
lastTripRates,
getLogsText
})
</script>

View File

@@ -287,6 +287,14 @@
@click="exportConfig"
class="px-8"
/>
<q-btn
color="grey"
size="lg"
label="导出日志"
icon="description"
@click="exportLog"
class="px-8"
/>
</div>
<!-- 隐藏的文件输入 -->
@@ -585,6 +593,40 @@ const parseToml = (tomlStr: string): any => {
return result
}
// 导出日志
const exportLog = async () => {
try {
const logText = logRef.value?.getLogsText() || ''
if (!logText) {
logRef.value?.addLog('warning', '暂无日志可导出')
return
}
if (window.pywebview) {
const response = await window.pywebview.api.export_log(logText)
if (response.success) {
logRef.value?.addLog('info', response.message)
} else {
logRef.value?.addLog('warning', response.message)
}
} else {
// 开发模式下的模拟
logRef.value?.addLog('info', '导出日志(开发模式,直接下载)')
const blob = new Blob([logText], { type: 'text/plain' })
const url = URL.createObjectURL(blob)
const a = document.createElement('a')
a.href = url
const timestamp = new Date().toISOString().slice(0, 19).replace(/[:-]/g, '')
a.download = `egm_log_${timestamp}.txt`
a.click()
URL.revokeObjectURL(url)
}
} catch (e: any) {
error.value = e.message || '导出日志失败'
logRef.value?.addLog('error', e.message || '导出日志失败')
}
}
// 导出配置
const exportConfig = async () => {
try {
@@ -659,6 +701,7 @@ declare global {
api: {
calculate: (params: AllParameters) => Promise<any>
export_config: (params: AllParameters) => Promise<any>
export_log: (logText: string) => Promise<any>
}
}
addLogFromBackend?: (log: { level: string; time: string; message: string }) => void