-
Notifications
You must be signed in to change notification settings - Fork 2
/
logs.ts
68 lines (55 loc) · 1.48 KB
/
logs.ts
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
/**
* This module should not be used by consumers of this library.
* @private
*/
import { LogEntry } from 'blockheads-api-interface'
/**
* Parses logs from the portal into a standard format. If you are consuming this library, you don't need to know anything about it.
* @private
*/
export class LogParser {
private entries: LogEntry[]
/**
* Creates a new instance of the LogParser class.
*/
constructor() {
this.entries = []
}
/**
* Parses the logs into a standard format.
*
* @param lines the raw log lines.
*/
parse = (lines: string[]): LogEntry[] => {
// Copy the lines array
lines = lines.slice(0)
// Assume first line is valid, if it isn't it will be dropped.
for (let i = lines.length - 1; i > 0; i--) {
const line = lines[i]
if (!this.isValidLine(line)) {
lines[i - 1] += '\n' + lines.splice(i, 1)
continue
}
this.addLine(line)
}
if (this.isValidLine(lines[0])) {
this.addLine(lines[0])
}
const entries = this.entries.reverse()
this.entries = []
return entries
}
private isValidLine = (line: string): boolean => {
return /^\d{4}-\d\d-\d\d \d\d:\d\d:\d\d\.\d{3} blockheads_server/.test(line)
}
private addLine = (line: string): void => {
const ts = line.substr(0, 24)
.replace(' ', 'T')
.replace(' ', 'Z')
this.entries.push({
raw: line,
timestamp: new Date(ts),
message: line.substr(line.indexOf(']') + 2)
})
}
}