-
Notifications
You must be signed in to change notification settings - Fork 16
/
tailer.go
108 lines (89 loc) · 2.2 KB
/
tailer.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
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
package ninetail
import (
"fmt"
"io"
"os"
"path/filepath"
"strings"
"github.com/hpcloud/tail"
"github.com/mattn/go-runewidth"
)
var (
// red, green, yellow, magenta, cyan
ansiColorCodes = [...]int{31, 32, 33, 35, 36}
seekInfoOnStart = &tail.SeekInfo{Offset: 0, Whence: os.SEEK_END}
)
//Tailer contains watches tailed files and contains per-file output parameters
type Tailer struct {
*tail.Tail
colorCode int
padding string
}
//NewTailers creates slice of Tailers from file names.
//Colors of file names are cycled through the list.
//maxWidth is a maximum widht of passed file names, for nice alignment
func NewTailers(filenames []string) ([]*Tailer, error) {
maxLength := maximumNameLength(filenames)
ts := make([]*Tailer, len(filenames))
for i, filename := range filenames {
t, err := newTailer(filename, getColorCode(i), maxLength)
if err != nil {
return nil, err
}
ts[i] = t
}
return ts, nil
}
func newTailer(filename string, colorCode int, maxWidth int) (*Tailer, error) {
t, err := tail.TailFile(filename, tail.Config{
Follow: true,
Location: seekInfoOnStart,
Logger: tail.DiscardingLogger,
})
if err != nil {
return nil, err
}
dispNameLength := displayFilenameLength(filename)
return &Tailer{
Tail: t,
colorCode: colorCode,
padding: strings.Repeat(" ", maxWidth-dispNameLength),
}, nil
}
//Do formats, colors and writes to stdout appended lines when they happen, exiting on write error
func (t Tailer) Do(output io.Writer) {
for line := range t.Lines {
_, err := fmt.Fprintf(
output,
"\x1b[%dm%s%s\x1b[0m: %s\n",
t.colorCode,
t.padding,
t.name(),
line.Text,
)
if err != nil {
return
}
}
}
func (t Tailer) name() string {
return filepath.Base(t.Filename)
}
func getColorCode(index int) int {
return ansiColorCodes[index%len(ansiColorCodes)]
}
func maximumNameLength(filenames []string) int {
max := 0
for _, name := range filenames {
if current := displayFilenameLength(name); current > max {
max = current
}
}
return max
}
func displayFilename(filename string) string {
return filepath.Base(filename)
}
func displayFilenameLength(filename string) int {
return runewidth.StringWidth(displayFilename(filename))
}