Files
H100PowerManger/src/pPowerManger/httpserver/logpage.h
T
2025-05-19 01:02:33 +08:00

574 lines
16 KiB
C++

#ifndef HTTP_SERVER_LOGPAGE_H
#define HTTP_SERVER_LOGPAGE_H
#include <string>
const std::string LOG_PAGE_HTML = R"(
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>系统日志 - Power Manager</title>
<style>
:root {
--primary-color: #2563eb;
--primary-hover: #1d4ed8;
--bg-color: #f8fafc;
--sidebar-width: 60px;
--sidebar-expanded: 220px;
--header-height: 36px;
}
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
background-color: var(--bg-color);
color: #1a1a1a;
}
.container {
display: grid;
grid-template-areas: "sidebar main-content";
grid-template-columns: var(--sidebar-width) 1fr;
grid-template-rows: 1fr;
min-height: 100vh;
gap: 0;
}
.header {
grid-area: unset;
background: white;
padding: 0 0.5rem;
display: flex;
align-items: center;
justify-content: space-between;
box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
position: fixed;
top: 0;
right: 0;
left: var(--sidebar-width);
height: var(--header-height);
z-index: 10;
}
.header h1 {
font-size: 1.25rem;
font-weight: 600;
color: #1a1a1a;
}
.sidebar {
grid-area: sidebar;
background: white;
width: var(--sidebar-width);
height: 100vh;
position: fixed;
left: 0;
top: 0;
padding: 0.5rem 0.25rem;
transition: width 0.3s ease;
box-shadow: 1px 0 3px rgba(0, 0, 0, 0.1);
z-index: 20;
}
.sidebar:hover {
width: var(--sidebar-expanded);
}
.sidebar-menu {
display: flex;
flex-direction: column;
gap: 0.5rem;
}
.sidebar-button {
display: flex;
align-items: center;
padding: 0.5rem 0.75rem;
border-radius: 0.375rem;
color: #4b5563;
text-decoration: none;
transition: all 0.2s ease;
white-space: nowrap;
overflow: hidden;
}
.sidebar-button:hover {
background: var(--bg-color);
color: var(--primary-color);
}
.sidebar-button.active {
background: var(--primary-color);
color: white;
}
.sidebar-icon {
min-width: 1.5rem;
margin-right: 1rem;
font-size: 1.25rem;
}
.main-content {
grid-area: main-content;
padding: 1px;
margin-top: var(--header-height);
}
.log-panel {
background: white;
border-radius: 4px;
padding: 6px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
height: calc(100vh - var(--header-height));
overflow: hidden;
display: flex;
flex-direction: column;
}
.log-controls {
display: flex;
gap: 8px;
padding: 4px;
border-bottom: 1px solid #e5e7eb;
flex-wrap: wrap;
}
.log-button {
padding: 4px 12px;
border: 1px solid #e5e7eb;
border-radius: 4px;
background: white;
color: #4b5563;
cursor: pointer;
font-size: 0.875rem;
transition: all 0.2s;
}
.log-button:hover {
background: var(--bg-color);
border-color: var(--primary-color);
color: var(--primary-color);
}
.log-filter {
display: flex;
gap: 8px;
align-items: center;
margin-left: auto;
}
.log-filter select, .log-filter input {
padding: 4px 8px;
border: 1px solid #e5e7eb;
border-radius: 4px;
font-size: 0.875rem;
color: #4b5563;
}
.log-filter input {
width: 150px;
}
.log-pagination {
display: flex;
gap: 4px;
align-items: center;
padding: 4px;
border-top: 1px solid #e5e7eb;
justify-content: space-between;
font-size: 0.875rem;
color: #4b5563;
}
.page-info {
display: flex;
align-items: center;
gap: 8px;
}
.page-size {
display: flex;
align-items: center;
gap: 4px;
}
.page-size select {
padding: 2px 4px;
border: 1px solid #e5e7eb;
border-radius: 4px;
}
.page-nav {
display: flex;
gap: 4px;
}
.page-nav button {
padding: 2px 8px;
border: 1px solid #e5e7eb;
border-radius: 4px;
background: white;
cursor: pointer;
}
.page-nav button:disabled {
opacity: 0.5;
cursor: not-allowed;
}
.log-content {
flex: 1;
overflow: auto;
padding: 8px;
font-family: monospace;
font-size: 0.875rem;
line-height: 1.5;
white-space: pre-wrap;
background: #1a1a1a;
color: #f8fafc;
margin: 4px;
border-radius: 4px;
}
.log-entry {
margin: 2px 0;
padding: 2px 4px;
border-radius: 2px;
}
.log-entry:hover {
background: rgba(255, 255, 255, 0.1);
}
.log-entry.error {
color: #f87171;
}
.log-entry.warning {
color: #fbbf24;
}
.log-entry.info {
color: #60a5fa;
}
.status-bar {
position: fixed;
top: 4px;
right: 8px;
background: white;
padding: 4px 8px;
border-radius: 4px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
display: flex;
gap: 8px;
z-index: 15;
}
.status-info {
display: flex;
align-items: center;
gap: 8px;
color: #4b5563;
font-size: 0.875rem;
}
</style>
</head>
<body>
<div class="container">
<div class="sidebar">
<div class="sidebar-menu">
<a href="/data" class="sidebar-button">
<span class="sidebar-icon">📊</span>
<span>系统数据</span>
</a>
<a href="/dashboard" class="sidebar-button">
<span class="sidebar-icon">⚡</span>
<span>配电操作</span>
</a>
<a href="/logs" class="sidebar-button active">
<span class="sidebar-icon">📝</span>
<span>系统日志</span>
</a>
</div>
</div>
<div class="header">
<h1>系统日志</h1>
<div class="status-bar">
<div class="status-info">
<span>🟢 系统状态: 运行中</span>
<span>🕒 更新时间: <time id="updateTime">2025-05-18 15:52</time></span>
</div>
</div>
</div>
<div class="main-content">
<div class="log-panel">
<div class="log-controls">
<button class="log-button" id="clearBtn">清空显示</button>
<button class="log-button" id="scrollLockBtn">自动滚动</button>
<button class="log-button" id="downloadBtn">下载日志</button>
<div class="log-filter">
<select id="logLevel">
<option value="ALL">所有级别</option>
<option value="ERROR">错误</option>
<option value="WARNING">警告</option>
<option value="INFO">信息</option>
</select>
<input type="text" id="searchInput" placeholder="搜索日志...">
</div>
</div>
<div class="log-content" id="logContent"></div>
<div class="log-pagination">
<div class="page-size">
每页显示:
<select id="pageSize">
<option value="100">100</option>
<option value="200">200</option>
<option value="500">500</option>
<option value="1000">1000</option>
</select>
</div>
<div class="page-info">
<span id="pageInfo">第 0/0 页</span>
<div class="page-nav">
<button id="firstPage">首页</button>
<button id="prevPage">上一页</button>
<button id="nextPage">下一页</button>
<button id="lastPage">末页</button>
</div>
</div>
</div>
</div>
</div>
</div>
<script>
let ws = null;
let autoScroll = true;
let logContent = document.getElementById('logContent');
let currentPage = 1;
let totalPages = 1;
let pageSize = 100;
let allLogs = [];
let filteredLogs = [];
let searchTimer = null;
function initWebSocket() {
ws = new WebSocket('ws://' + window.location.host + '/ws');
ws.onopen = () => {
console.log('WebSocket连接已建立');
updateStatus('运行中', true);
requestLogs();
};
ws.onmessage = (event) => {
const data = JSON.parse(event.data);
if (data.type === 'log') {
if (Array.isArray(data.content)) {
// 初始加载的日志数据
allLogs = data.content;
filterAndDisplayLogs();
} else {
// 新的日志条目
allLogs.push(data.content);
if (shouldShowLog(data.content)) {
appendLogEntry(data.content);
}
}
}
updateTime();
};
ws.onclose = () => {
console.log('WebSocket连接已关闭');
updateStatus('已断开', false);
setTimeout(initWebSocket, 5000);
};
ws.onerror = (error) => {
console.error('WebSocket错误:', error);
updateStatus('错误', false);
};
}
function requestLogs() {
ws.send(JSON.stringify({
type: 'get_logs',
page: currentPage,
pageSize: pageSize,
filter: {
level: document.getElementById('logLevel').value,
search: document.getElementById('searchInput').value
}
}));
}
function shouldShowLog(logEntry) {
const level = document.getElementById('logLevel').value;
const search = document.getElementById('searchInput').value.toLowerCase();
if (level !== 'ALL') {
if (!logEntry.includes(level)) return false;
}
if (search && !logEntry.toLowerCase().includes(search)) {
return false;
}
return true;
}
function filterAndDisplayLogs() {
filteredLogs = allLogs.filter(shouldShowLog);
totalPages = Math.ceil(filteredLogs.length / pageSize);
displayCurrentPage();
updatePagination();
}
function displayCurrentPage() {
logContent.innerHTML = '';
const start = (currentPage - 1) * pageSize;
const end = Math.min(start + pageSize, filteredLogs.length);
for (let i = start; i < end; i++) {
appendLogEntry(filteredLogs[i]);
}
}
function updatePagination() {
document.getElementById('pageInfo').textContent = `第 ${currentPage}/${totalPages} 页`;
document.getElementById('firstPage').disabled = currentPage === 1;
document.getElementById('prevPage').disabled = currentPage === 1;
document.getElementById('nextPage').disabled = currentPage === totalPages;
document.getElementById('lastPage').disabled = currentPage === totalPages;
}
// 事件监听器
document.getElementById('logLevel').addEventListener('change', () => {
currentPage = 1;
filterAndDisplayLogs();
});
document.getElementById('searchInput').addEventListener('input', (e) => {
clearTimeout(searchTimer);
searchTimer = setTimeout(() => {
currentPage = 1;
filterAndDisplayLogs();
}, 300);
});
document.getElementById('pageSize').addEventListener('change', (e) => {
pageSize = parseInt(e.target.value);
currentPage = 1;
filterAndDisplayLogs();
});
document.getElementById('firstPage').addEventListener('click', () => {
currentPage = 1;
displayCurrentPage();
updatePagination();
});
document.getElementById('prevPage').addEventListener('click', () => {
if (currentPage > 1) {
currentPage--;
displayCurrentPage();
updatePagination();
}
});
document.getElementById('nextPage').addEventListener('click', () => {
if (currentPage < totalPages) {
currentPage++;
displayCurrentPage();
updatePagination();
}
});
document.getElementById('lastPage').addEventListener('click', () => {
currentPage = totalPages;
displayCurrentPage();
updatePagination();
});
function appendLogEntry(content) {
const entry = document.createElement('div');
entry.className = 'log-entry';
// 根据日志内容设置不同的样式
if (content.includes('ERROR')) {
entry.classList.add('error');
} else if (content.includes('WARNING')) {
entry.classList.add('warning');
} else if (content.includes('INFO')) {
entry.classList.add('info');
}
entry.textContent = content;
logContent.appendChild(entry);
if (autoScroll) {
logContent.scrollTop = logContent.scrollHeight;
}
}
function updateStatus(status, isConnected) {
const statusElement = document.querySelector('.status-info span:first-child');
statusElement.innerHTML = `${isConnected ? '🟢' : '🔴'} 系统状态: ${status}`;
}
function updateTime() {
const now = new Date();
const timeStr = now.toLocaleString('zh-CN', {
year: 'numeric',
month: '2-digit',
day: '2-digit',
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
hour12: false
});
document.getElementById('updateTime').textContent = timeStr;
}
// 按钮事件处理
document.getElementById('clearBtn').onclick = () => {
logContent.innerHTML = '';
};
document.getElementById('scrollLockBtn').onclick = function() {
autoScroll = !autoScroll;
this.textContent = autoScroll ? '自动滚动' : '已锁定';
this.style.backgroundColor = autoScroll ? 'white' : '#e5e7eb';
};
document.getElementById('downloadBtn').onclick = () => {
const text = logContent.innerText;
const blob = new Blob([text], { type: 'text/plain' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = `system_log_${new Date().toISOString().slice(0,19).replace(/[:-]/g, '')}.txt`;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
};
window.addEventListener('load', initWebSocket);
</script>
</body>
</html>
)";
#endif // HTTP_SERVER_LOGPAGE_H