ForcePilot/web/src/components/DebugComponent.vue

100 lines
2.4 KiB
Vue
Raw Normal View History

2024-08-25 20:29:24 +08:00
<template>
<div class="log-viewer">
2024-09-09 17:07:03 +08:00
<a-button @click="fetchLogs" :loading="state.fetching">刷新</a-button>
2024-08-25 20:29:24 +08:00
<div ref="logContainer" class="log-container">
<pre v-if="logs">{{ logs }}</pre>
</div>
<p v-if="error" class="error">{{ error }}</p>
</div>
</template>
<script setup>
2024-09-09 17:07:03 +08:00
import { ref, onMounted, onActivated, nextTick, reactive } from 'vue';
2024-08-25 20:29:24 +08:00
// 定义一个 ref 来存储日志数据和错误信息
const logs = ref('');
const error = ref('');
2024-09-09 17:07:03 +08:00
const state = reactive({
fetching: false,
})
2024-08-25 20:29:24 +08:00
// 定义一个 ref 来获取日志容器的 DOM 元素
const logContainer = ref(null);
// 定义一个方法来获取日志数据
const fetchLogs = async () => {
2024-09-09 17:07:03 +08:00
state.fetching = true;
2024-08-25 20:29:24 +08:00
try {
// 清空之前的错误信息
error.value = '';
// 发送请求获取日志数据
const response = await fetch('/api/log'); // 替换为你的 API 路径
if (!response.ok) {
throw new Error('Failed to fetch logs');
}
// 解析 JSON 数据
const data = await response.json();
// 将日志信息赋值给 logs 变量
logs.value = data.log;
// 等待 DOM 更新完成后自动滚动到最底部
await nextTick();
if (logContainer.value) {
logContainer.value.scrollTop = logContainer.value.scrollHeight;
}
} catch (err) {
// 处理请求错误
error.value = `Error: ${err.message}`;
2024-09-09 17:07:03 +08:00
} finally {
state.fetching = false;
2024-08-25 20:29:24 +08:00
}
};
// 组件第一次挂载时自动获取日志并滚动到底部
onMounted(() => {
fetchLogs();
2024-09-09 17:07:03 +08:00
// setInterval(fetchLogs, 5000); // 每 5 秒自动刷新一次日志
2024-08-25 20:29:24 +08:00
});
// 组件重新激活时(从 keep-alive 缓存恢复)自动获取日志
onActivated(() => {
2024-09-09 17:07:03 +08:00
setTimeout(fetchLogs, 1000);
2024-08-25 20:29:24 +08:00
});
</script>
<style scoped>
2024-09-09 17:07:03 +08:00
.log-viewer {
}
2024-08-25 20:29:24 +08:00
.error {
2024-09-25 13:46:51 +08:00
color: var(--error-color);
2024-08-25 20:29:24 +08:00
}
.log-container {
2024-09-09 17:07:03 +08:00
height: calc(100vh - 150px); /* 设置最大高度 */
2024-08-25 20:29:24 +08:00
overflow-y: auto; /* 启用垂直滚动 */
2024-09-09 17:07:03 +08:00
background-color: #f6f7f7;
2024-08-25 20:29:24 +08:00
padding-bottom: 0;
border-radius: 5px;
2024-09-09 17:07:03 +08:00
margin-top: 10px;
2024-08-25 20:29:24 +08:00
white-space: pre-wrap; /* 使日志内容自动换行 */
word-wrap: break-word;
2024-09-09 17:07:03 +08:00
font-size: small;
2024-08-25 20:29:24 +08:00
background: #0C0C0C;
color: #D1D1D1;
2024-09-09 17:07:03 +08:00
pre {
margin: 0;
padding: 10px;
height: 100%;
}
2024-08-25 20:29:24 +08:00
}
</style>