html
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>日志可视化工具</title>
<!-- 引入外部库:jsMind -->
<link type="text/css" rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jsmind/0.4.6/jsmind.min.css" />
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jsmind/0.4.6/jsmind.js"></script>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
display: flex;
flex-direction: column;
height: 100vh;
}
header {
background-color: #2c3e50;
color: white;
padding: 10px 20px;
text-align: center;
font-size: 24px;
}
#input-section {
padding: 10px 20px;
background-color: #ecf0f1;
}
#log-input {
width: 100%;
height: 150px;
padding: 10px;
box-sizing: border-box;
font-family: monospace;
font-size: 14px;
}
#parse-button {
margin-top: 10px;
padding: 10px 20px;
background-color: #3498db;
border: none;
color: white;
font-size: 16px;
cursor: pointer;
border-radius: 4px;
}
#parse-button:hover {
background-color: #2980b9;
}
#main-container {
display: flex;
flex: 1;
overflow: hidden;
}
.panel {
flex: 1;
overflow: auto;
padding: 10px;
box-sizing: border-box;
}
#thread-container {
border-right: 1px solid #bdc3c7;
}
.jsmind_container {
width: 100%;
height: 100%;
}
/* 右键菜单样式 */
.context-menu {
position: absolute;
z-index: 1000;
width: 150px;
background-color: white;
border: 1px solid #ccc;
box-shadow: 2px 2px 5px rgba(0,0,0,0.2);
display: none;
}
.context-menu ul {
list-style: none;
padding: 5px 0;
margin: 0;
}
.context-menu ul li {
padding: 8px 12px;
cursor: pointer;
}
.context-menu ul li:hover {
background-color: #f2f2f2;
}
</style>
</head>
<body>
<header>日志可视化工具</header>
<div id="input-section">
<textarea id="log-input" placeholder="在此粘贴日志内容..."></textarea>
<button id="parse-button">解析并展示日志</button>
</div>
<div id="main-container">
<div class="panel" id="thread-container">
<h3>线程关系</h3>
<div id="thread-map" class="jsmind_container"></div>
</div>
<div class="panel" id="log-trace-container">
<h3>日志跟踪</h3>
<div id="log-trace-map" class="jsmind_container"></div>
</div>
</div>
<!-- 右键菜单 -->
<div class="context-menu" id="context-menu">
<ul>
<li id="toggle-important">标记/取消标记为不重要</li>
</ul>
</div>
<script>
// 初始化全局变量
let threadData = {};
let logData = {};
let parentThreadMap = {};
let currentSelectedThread = null;
let importantNodes = new Set();
// 初始化jsMind配置
const mind_options = {
container: 'thread-map',
editable: false,
theme: 'primary',
layout: 'independent',
view: {
line_width: 2,
line_color: '#ffa500',
support_html: true
}
};
const log_mind_options = {
container: 'log-trace-map',
editable: false,
theme: 'orange',
layout: 'independent',
view: {
line_width: 2,
line_color: '#1e90ff',
support_html: true
}
};
let thread_mind = jsMind.show(mind_options);
let log_mind = jsMind.show(log_mind_options);
// 事件监听:解析按钮
document.getElementById('parse-button').addEventListener('click', () => {
const logText = document.getElementById('log-input').value;
if (!logText.trim()) {
alert('请输入日志内容!');
return;
}
parseLogs(logText);
renderThreadMap();
// Clear log trace map
log_mind.init({ meta: { name: "log", children: [] } });
});
// 解析日志
function parseLogs(logText) {
threadData = {};
logData = {};
parentThreadMap = {};
const lines = logText.split('\n').filter(line => line.trim() !== '');
const threadStack = {};
lines.forEach(line => {
const regex = /^(I|E)\d+:(\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d{3})\((\d+)\|(\d+)\)([^:]+): (.+)$/;
const match = line.match(regex);
if (match) {
const level = match[1];
const timestamp = match[2];
const processId = match[3];
const threadId = match[4];
const module = match[5];
const content = match[6];
// 分类线程
if (!threadData[threadId]) {
threadData[threadId] = [];
}
threadData[threadId].push({
level,
timestamp,
processId,
threadId,
module,
content,
raw: line
});
// 处理线程嵌套关系
if (content.endsWith('Entered...')) {
if (!threadStack[threadId]) {
threadStack[threadId] = [];
}
const functionName = content.replace(' Entered...', '');
threadStack[threadId].push(functionName);
// Below assumes that if a new function is entered, it might spawn a new thread
// This logic can be adjusted based on actual thread creation patterns
} else if (content.endsWith('Exited...')) {
if (threadStack[threadId] && threadStack[threadId].length > 0) {
threadStack[threadId].pop();
}
}
}
});
// For simplicity, assuming all threads are independent. Adjust parentThreadMap if necessary.
Object.keys(threadData).forEach(tid => {
parentThreadMap[tid] = null; // No parent by default
});
}
// 渲染线程关系图
function renderThreadMap() {
const mind = { "meta": { "name": "thread", "author": "user", "version": "1.0" }, "format": "node_array", "data": [] };
const nodes = [];
const root = {
id: "root",
isroot: true,
topic: "Threads"
};
nodes.push(root);
Object.keys(threadData).forEach((tid, index) => {
const parent = parentThreadMap[tid] || "root";
nodes.push({
id: tid,
parentid: parent,
topic: `线程 ${tid}`
});
});
mind.data = nodes;
thread_mind.init(mind);
// 事件监听:点击节点
thread_mind.on('select_node', function (node) {
if (node.id !== 'root') {
currentSelectedThread = node.id;
renderLogTrace(node.id);
} else {
currentSelectedThread = null;
log_mind.init({ meta: { name: "log", children: [] } });
}
});
}
// 渲染日志跟踪
function renderLogTrace(threadId) {
if (!threadData[threadId]) return;
// 构建层级结构
const root = {
id: "log_root",
topic: `线程 ${threadId} 日志`,
children: []
};
const stack = [root];
threadData[threadId].forEach(entry => {
const current = stack[stack.length - 1];
if (entry.content.endsWith('Entered...')) {
const node = {
id: `${threadId}_${Math.random()}`,
topic: `[${entry.timestamp}] [${entry.level}] ${entry.content}`,
children: []
};
current.children.push(node);
stack.push(node);
} else if (entry.content.endsWith('Exited...')) {
stack.pop();
} else {
const node = {
id: `${threadId}_${Math.random()}`,
topic: `[${entry.timestamp}] [${entry.level}] ${entry.content}`
};
current.children.push(node);
}
});
log_mind.init({ meta: { name: "log_trace", children: [root] } });
// 添加右键菜单
const container = document.getElementById('log-trace-map');
container.addEventListener('contextmenu', function (e) {
e.preventDefault();
const node = log_mind.get_node_at(e.offsetX, e.offsetY);
if (node && node.id !== 'log_root') {
showContextMenu(e.pageX, e.pageY, node.id);
}
});
}
// 右键菜单相关
const contextMenu = document.getElementById('context-menu');
let selectedNodeId = null;
function showContextMenu(x, y, nodeId) {
selectedNodeId = nodeId;
contextMenu.style.left = `${x}px`;
contextMenu.style.top = `${y}px`;
contextMenu.style.display = 'block';
}
document.getElementById('toggle-important').addEventListener('click', () => {
if (!selectedNodeId) return;
const node = log_mind.get_node(selectedNodeId);
if (importantNodes.has(selectedNodeId)) {
importantNodes.delete(selectedNodeId);
node.style = { opacity: 1 };
} else {
importantNodes.add(selectedNodeId);
node.style = { opacity: 0.5 };
}
log_mind.refresh();
contextMenu.style.display = 'none';
});
// 点击其他地方隐藏右键菜单
document.addEventListener('click', () => {
contextMenu.style.display = 'none';
});
// 辅助函数:获取节点在相对位置
jsMind.util.extend(jsMind.layout, {
get_node_at: function (x, y) {
// Simple implementation: iterate through nodes and find one that contains the (x,y)
// jsMind does not provide a direct method, so this needs to be implemented if needed
// For simplicity, this function is left as a placeholder
return null;
}
});
</script>
</body>
</html>