-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathLogFile.js
More file actions
51 lines (44 loc) · 1.31 KB
/
LogFile.js
File metadata and controls
51 lines (44 loc) · 1.31 KB
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
import {LogProvider} from './LogProvider.js'
export class LogFile extends LogProvider {
constructor (content, limit) {
super(limit);
// newlines that aren't proceeded by a '}' are either invalid or cary no meaning
content = content.replace(/([^}])\s*([\n\r]+)/g, "$1");
this.content = content;
this.lines = this.content.split('\n');
}
async loadEntries (offset, count = 50) {
const start = this.lines.length - offset;
const end = Math.max(start - count - 2, 0);
const entries = this.lines.slice(end, start).reverse()
.map(this.tryParseJSON)
.map(entry => {
if (!entry.id) {
entry.id = Math.random() * 10000;
}
return entry;
});
return {data: entries};
}
tryParseJSON (json) {
try {
return JSON.parse(json);
} catch (e) {
// fix unescaped message json
const startPos = json.indexOf('"message":"') + ('"message":"').length;
const endPos = json.lastIndexOf('","level":');
const start = json.slice(0, startPos);
const end = json.slice(endPos);
const message = json.slice(startPos, endPos);
const escapedMessage = message.replace(/([^\\]|^)["]/g, '$1\\"');
json = start + escapedMessage + end;
try {
return JSON.parse(json);
} catch (e) {
console.log('Error while parsing log message:');
console.log(json);
console.error(e);
}
}
}
}