add logging
This commit is contained in:
54
src/stores/logs.ts
Normal file
54
src/stores/logs.ts
Normal file
@@ -0,0 +1,54 @@
|
||||
// filepath: src/stores/logs.ts
|
||||
import { defineStore } from 'pinia'
|
||||
import { ref } from 'vue'
|
||||
import { listen } from '@tauri-apps/api/event'
|
||||
|
||||
export interface LogEntry {
|
||||
id: string
|
||||
taskId: string
|
||||
message: string
|
||||
level: 'info' | 'error'
|
||||
timestamp: number
|
||||
}
|
||||
|
||||
interface LogEvent {
|
||||
id: string
|
||||
message: string
|
||||
level: string
|
||||
}
|
||||
|
||||
export const useLogsStore = defineStore('logs', () => {
|
||||
const logs = ref<LogEntry[]>([])
|
||||
const isListening = ref(false)
|
||||
|
||||
function addLog(taskId: string, message: string, level: 'info' | 'error') {
|
||||
logs.value.push({
|
||||
id: crypto.randomUUID(),
|
||||
taskId,
|
||||
message,
|
||||
level,
|
||||
timestamp: Date.now()
|
||||
})
|
||||
|
||||
// Optional: Limit log size to avoid memory issues
|
||||
if (logs.value.length > 5000) {
|
||||
logs.value = logs.value.slice(-5000)
|
||||
}
|
||||
}
|
||||
|
||||
async function initListener() {
|
||||
if (isListening.value) return
|
||||
isListening.value = true
|
||||
|
||||
await listen<LogEvent>('download-log', (event) => {
|
||||
const { id, message, level } = event.payload
|
||||
addLog(id, message, level as 'info' | 'error')
|
||||
})
|
||||
}
|
||||
|
||||
function clearLogs() {
|
||||
logs.value = []
|
||||
}
|
||||
|
||||
return { logs, addLog, initListener, clearLogs }
|
||||
})
|
||||
Reference in New Issue
Block a user