-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogviewer.html
206 lines (191 loc) · 6.09 KB
/
logviewer.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Log Viewer</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
display: flex;
flex-direction: column;
height: 100vh;
}
header {
background: #333;
color: #fff;
padding: 10px;
text-align: center;
}
#links {
flex: 1;
overflow-y: auto;
background: #f4f4f4;
padding: 10px;
border-right: 1px solid #ccc;
}
#viewer {
flex: 3;
overflow-y: auto;
padding: 10px;
border-left: 1px solid #ccc;
background: #fff;
white-space: pre-wrap;
overflow-wrap: break-word;
}
.container {
display: flex;
flex: 1;
}
a {
display: block;
margin: 5px 0;
color: #007bff;
text-decoration: none;
}
a:hover {
text-decoration: underline;
}
</style>
</head>
<body>
<header>
<h1>Log Viewer</h1>
</header>
<div class="container">
<div id="links"></div>
<div id="viewer" style="white-space: pre-wrap; font-family: monospace; overflow-y: auto; padding: 10px; border-left: 1px solid #ccc; background: #fff;">
Select a log to view its contents here.
</div>
</div>
<script>
// Get URL parameters
const urlParams = new URLSearchParams(window.location.search);
// Default URL or from parameter
const baseUrl = urlParams.get('baseUrl') || 'https://teleport1.blob.core.windows.net/agent-sanitized-logs';
// URL to fetch the XML file from
const xmlUrl = `${baseUrl}?restype=container&comp=list`;
// DOM Elements
const linksContainer = document.getElementById('links');
const viewer = document.getElementById('viewer');
// Fetch and process the XML file
fetch(xmlUrl)
.then(response => response.text())
.then(str => new window.DOMParser().parseFromString(str, "text/xml"))
.then(xml => {
const blobs = xml.getElementsByTagName('Blob');
for (let blob of blobs) {
const name = blob.getElementsByTagName('Name')[0].textContent;
const url = blob.getElementsByTagName('Url')[0].textContent;
// Create a link for each log file
const link = document.createElement('a');
link.href = '#';
link.textContent = name;
link.onclick = (e) => {
e.preventDefault();
loadLog(url);
};
linksContainer.appendChild(link);
}
})
.catch(err => console.error('Error fetching or processing XML:', err));
function parseAnsiToHtml(text) {
const ansiRegex = /\x1b\[(\d+(?:;\d+)*)m/g;
const newlineRegex = /\n/g;
let result = '';
let lastIndex = 0;
let openSpans = [];
let currentStyles = [];
const ansiStyles = {
0: '</span>', // Reset / Normal
1: 'font-weight:bold;', // Bold
30: 'color:black;',
31: 'color:red;',
32: 'color:green;',
33: 'color:orange;',
34: 'color:blue;',
35: 'color:magenta;',
36: 'color:cyan;',
37: 'color:brown;',
90: 'color:grey;', // Bright Black
// Add more styles as needed
};
// Combined regex to match ANSI codes and newlines
const combinedRegex = /(\x1b\[(\d+(?:;\d+)*)m)|(\n)/g;
let match;
while ((match = combinedRegex.exec(text)) !== null) {
// Append text before the matched sequence
result += escapeHtml(text.substring(lastIndex, match.index));
lastIndex = combinedRegex.lastIndex;
if (match[1]) {
// ANSI escape code detected
const codes = match[2].split(';').map(Number);
for (const code of codes) {
if (code === 0) {
// Reset all styles
while (openSpans.length > 0) {
result += '</span>';
openSpans.pop();
}
currentStyles = [];
} else {
const style = ansiStyles[code];
if (style) {
result += `<span style="${style}">`;
openSpans.push('</span>');
currentStyles.push(style);
}
}
}
} else if (match[3]) {
// Newline detected
// Close all open spans before newline
while (openSpans.length > 0) {
result += '</span>';
openSpans.pop();
}
result += '\n';
// Reopen spans with current styles after newline
for (const style of currentStyles) {
result += `<span style="${style}">`;
openSpans.push('</span>');
}
}
}
// Append any remaining text
result += escapeHtml(text.substring(lastIndex));
// Close any remaining open spans at the end
while (openSpans.length > 0) {
result += '</span>';
openSpans.pop();
}
return result;
}
// Helper function to escape HTML special characters
function escapeHtml(text) {
const map = {
'&': '&',
'<': '<',
'>': '>',
'"': '"',
"'": ''',
};
return text.replace(/[&<>"']/g, function(m) { return map[m]; });
}
function loadLog(url) {
viewer.innerHTML = 'Loading...'; // Use innerHTML to render styled text
fetch(url)
.then(response => response.text())
.then(text => {
const formattedText = parseAnsiToHtml(text);
viewer.innerHTML = formattedText;
})
.catch(err => {
viewer.textContent = 'Error loading log: ' + err;
});
}
</script>
</body>
</html>