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
|
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
// vespa logfmt command
// Author: arnej
package logfmt
import (
"bufio"
"fmt"
"os"
)
func inputIsPipe() bool {
fi, err := os.Stdin.Stat()
if err != nil {
return false
}
if fi.Mode()&os.ModeNamedPipe == 0 {
return false
} else {
return true
}
}
func vespaHome() string {
ev := os.Getenv("VESPA_HOME")
if ev == "" {
return "/opt/vespa"
}
return ev
}
// main entry point for vespa-logfmt
func RunLogfmt(opts *Options, args []string) {
if len(args) == 0 {
if !inputIsPipe() {
args = append(args, vespaHome()+"/logs/vespa/vespa.log")
} else {
formatFile(opts, os.Stdin)
}
}
if opts.FollowTail {
if len(args) != 1 {
fmt.Fprintf(os.Stderr, "Must have exact 1 file for 'follow' option, got %d\n", len(args))
return
}
if err := tailFile(opts, args[0]); err != nil {
fmt.Fprintln(os.Stderr, err)
return
}
return
}
for _, arg := range args {
file, err := os.Open(arg)
if err != nil {
fmt.Fprintf(os.Stderr, "Cannot open '%s': %v\n", arg, err)
} else {
formatFile(opts, file)
file.Close()
}
}
}
func formatLine(opts *Options, line string) {
output, err := handleLine(opts, line)
if err != nil {
fmt.Fprintln(os.Stderr, "bad log line:", err)
} else {
os.Stdout.WriteString(output)
}
}
func tailFile(opts *Options, fn string) error {
tailed, err := FollowFile(fn)
if err != nil {
return err
}
for line := range tailed.Lines() {
formatLine(opts, line.Text)
}
return nil
}
func formatFile(opts *Options, arg *os.File) {
input := bufio.NewScanner(arg)
input.Buffer(make([]byte, 64*1024), 4*1024*1024)
for input.Scan() {
formatLine(opts, input.Text())
}
}
|