-
Notifications
You must be signed in to change notification settings - Fork 4
/
track.go
72 lines (60 loc) · 1.18 KB
/
track.go
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
package main
import (
"bufio"
"io"
"os"
"regexp"
"sort"
errors "github.com/insolar/assured-ledger/ledger-core/vanilla/throw"
)
var lineRegex = regexp.MustCompile(`^([^ :]+):\d+:([^ ]*) `)
var ErrNotMatch = errors.New("line does no match")
type line struct {
file string
time string
source string
}
func main() {
bufIN := bufio.NewReader(os.Stdin)
bufOUT := bufio.NewWriter(os.Stdout)
var lines []line
for {
text, err := bufIN.ReadString('\n')
if err == io.EOF {
break
}
if err != nil {
panic(err)
}
ln, err := parse(text)
if err != nil {
panic(err)
}
lines = append(lines, ln)
}
sort.Slice(lines, func(i, j int) bool {
return lines[i].time < lines[j].time
})
var currentFile string
for _, ln := range lines {
if ln.file != currentFile {
_, err := bufOUT.WriteString("\n" + ln.file + "\n")
if err != nil {
panic(err)
}
currentFile = ln.file
}
_, err := bufOUT.WriteString(ln.source)
if err != nil {
panic(err)
}
bufOUT.Flush()
}
}
func parse(ln string) (line, error) {
g := lineRegex.FindStringSubmatch(ln)
if len(g) < 3 {
return line{}, ErrNotMatch
}
return line{file: g[1], time: g[2], source: ln}, nil
}