141 lines
5.3 KiB
JavaScript
141 lines
5.3 KiB
JavaScript
// Run and action log view helpers shared by WebUI classic scripts.
|
||
function classifyLogLine(line) {
|
||
const s = String(line || "");
|
||
if (!s) return "";
|
||
// 错误优先
|
||
if (/\b(ERROR|FATAL|CRITICAL|TRACEBACK|EXCEPTION|FAILED)\b/i.test(s)) return "logError";
|
||
if (/(^|[\s::\[])(FAIL|FAILURE)(?=$|[\s\]))])/.test(s.toUpperCase())) return "logError";
|
||
if (/(失败|错误|异常|未知错误|超时|崩溃)/.test(s)) return "logError";
|
||
if (/❌/.test(s)) return "logError";
|
||
// 警告
|
||
if (/\b(WARN(ING)?)\b/i.test(s)) return "logWarn";
|
||
if (/(警告|注意)/.test(s)) return "logWarn";
|
||
if (/⚠️/.test(s)) return "logWarn";
|
||
// 成功
|
||
if (/\b(SUCCESS|OK|DONE|COMPLETED?)\b/i.test(s)) return "logSuccess";
|
||
if (/(成功|已保存|完成|启动成功)/.test(s)) return "logSuccess";
|
||
if (/(✅|✓)/.test(s)) return "logSuccess";
|
||
// Debug
|
||
if (/\b(DEBUG|TRACE)\b/i.test(s)) return "logDebug";
|
||
// INFO 默认
|
||
if (/\b(INFO)\b/i.test(s)) return "logInfo";
|
||
return "";
|
||
}
|
||
|
||
function renderLogLineNode(line) {
|
||
const cls = classifyLogLine(line);
|
||
if (!cls) {
|
||
// 普通行:直接文本节点(性能最佳)
|
||
return document.createTextNode(String(line ?? "") + "\n");
|
||
}
|
||
const span = document.createElement("span");
|
||
span.className = "logLine " + cls;
|
||
span.textContent = String(line ?? "") + "\n";
|
||
return span;
|
||
}
|
||
|
||
function appendLog(lines) {
|
||
const log = $("runLog");
|
||
if (!lines || lines.length === 0) return;
|
||
|
||
// 记录更新前与底部的距离,用于在非底部时保持视图位置
|
||
const distanceFromBottom = log.scrollHeight - log.scrollTop - log.clientHeight;
|
||
// 是否严格位于底部(仅在几像素内视为底部),避免每次刷新都跳到底部
|
||
const wasAtBottom = distanceFromBottom < UI_LIMITS.scrollBottomThresholdPx;
|
||
|
||
// 直接追加到 DOM,同时维护 runLogBuffer 以便支持搜索
|
||
for (const ln of lines) runLogBuffer.push(ln);
|
||
// 限制缓冲
|
||
if (runLogBuffer.length > runLogMaxLines) runLogBuffer = runLogBuffer.slice(-runLogMaxLines);
|
||
|
||
// 如果当前处于 run search 结果显示状态,则不改变显示(保持搜索结果不被实时追加打断)
|
||
if (runSearchActive && runLogMode === 'search') {
|
||
// 仍保持缓冲更新,但不修改 DOM
|
||
} else if (runLogFilter) {
|
||
// 有过滤器时重渲染整个视图
|
||
renderRunLog();
|
||
} else {
|
||
// 批量追加:用 fragment + 行级 span 着色
|
||
const frag = document.createDocumentFragment();
|
||
for (const ln of lines) frag.appendChild(renderLogLineNode(ln));
|
||
log.appendChild(frag);
|
||
}
|
||
|
||
// 性能优化:限制日志长度,避免内存溢出(保留最近较多字符以便桌面端查看)
|
||
const maxLength = UI_LIMITS.runLogMaxChars;
|
||
if (log.textContent.length > maxLength) {
|
||
// 直接基于缓冲重渲染(截断到最新行)
|
||
runLogBuffer = runLogBuffer.slice(-Math.ceil(runLogMaxLines * 0.6));
|
||
renderRunLog();
|
||
if (!wasAtBottom) {
|
||
const newScrollTop = log.scrollHeight - log.clientHeight - distanceFromBottom;
|
||
log.scrollTop = Math.max(0, newScrollTop);
|
||
}
|
||
}
|
||
|
||
// 只有在确实位于底部时才自动滚动到最新内容,避免打断用户查看历史日志
|
||
if (wasAtBottom) {
|
||
log.scrollTop = log.scrollHeight;
|
||
}
|
||
}
|
||
|
||
function renderRunLog() {
|
||
const el = $("runLog");
|
||
if (!el) return;
|
||
const renderInto = (lines) => {
|
||
el.textContent = "";
|
||
if (!lines.length) return;
|
||
const frag = document.createDocumentFragment();
|
||
for (const ln of lines) frag.appendChild(renderLogLineNode(ln));
|
||
el.appendChild(frag);
|
||
};
|
||
|
||
// 如果处于搜索结果显示状态,优先显示 search 返回的结果
|
||
if (runSearchActive && runLogMode === 'search') {
|
||
const lines = runSearchResultsBuffer || [];
|
||
const prevScrollBottom = el.scrollHeight - el.scrollTop - el.clientHeight;
|
||
renderInto(lines);
|
||
if (prevScrollBottom < UI_LIMITS.scrollBottomThresholdPx) el.scrollTop = el.scrollHeight;
|
||
else el.scrollTop = Math.max(0, el.scrollHeight - el.clientHeight - prevScrollBottom);
|
||
return;
|
||
}
|
||
|
||
const filter = (runLogFilter || "").toLowerCase();
|
||
let lines = runLogBuffer;
|
||
if (filter) {
|
||
lines = runLogBuffer.filter(l => String(l || "").toLowerCase().includes(filter));
|
||
}
|
||
const prevScrollBottom = el.scrollHeight - el.scrollTop - el.clientHeight;
|
||
renderInto(lines);
|
||
if (prevScrollBottom < UI_LIMITS.scrollBottomThresholdPx) el.scrollTop = el.scrollHeight;
|
||
else el.scrollTop = Math.max(0, el.scrollHeight - el.clientHeight - prevScrollBottom);
|
||
}
|
||
|
||
function appendRunLogLines(lines) {
|
||
if (!Array.isArray(lines) || lines.length === 0) return;
|
||
// 使用 appendLog 让现有的 UI 消息也能写入缓冲并利用已有行为
|
||
appendLog(lines);
|
||
}
|
||
|
||
async function fetchActionsLog() {
|
||
try {
|
||
const data = await apiGet('/api/actions?lines=500');
|
||
const lines = Array.isArray(data.lines) ? data.lines : [];
|
||
actionsLogBuffer = lines.slice(-actionsLogMaxLines);
|
||
renderActionsLog();
|
||
} catch (e) {
|
||
console.error('fetchActionsLog error', e);
|
||
}
|
||
}
|
||
|
||
function renderActionsLog() {
|
||
const el = $("actionsLog");
|
||
if (!el) return;
|
||
const filter = (actionsLogFilter || "").toLowerCase();
|
||
let lines = actionsLogBuffer;
|
||
if (filter) lines = actionsLogBuffer.filter(l => String(l || "").toLowerCase().includes(filter));
|
||
el.textContent = lines.join("\n") + (lines.length ? "\n" : "");
|
||
// 自动滚到底部
|
||
el.scrollTop = el.scrollHeight;
|
||
}
|