diff options
author | Arne Juul <arnej@yahooinc.com> | 2023-01-31 13:42:29 +0000 |
---|---|---|
committer | Arne Juul <arnej@yahooinc.com> | 2023-01-31 14:30:39 +0000 |
commit | 1cf70cdb76ce8f66a8006963ab3dc1033fc342ec (patch) | |
tree | 3f23c12250277db86531ee09b88fea3fdddfd30c /client/go/script-utils/logfmt/runlogfmt.go | |
parent | 3e54969fc961ee51c93404a37d559ab7ea2f9fe6 (diff) |
move vespa-logfmt command
Diffstat (limited to 'client/go/script-utils/logfmt/runlogfmt.go')
-rw-r--r-- | client/go/script-utils/logfmt/runlogfmt.go | 85 |
1 files changed, 85 insertions, 0 deletions
diff --git a/client/go/script-utils/logfmt/runlogfmt.go b/client/go/script-utils/logfmt/runlogfmt.go new file mode 100644 index 00000000000..5b9a3ac0870 --- /dev/null +++ b/client/go/script-utils/logfmt/runlogfmt.go @@ -0,0 +1,85 @@ +// 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" + + "github.com/vespa-engine/vespa/client/go/vespa" +) + +func inputIsPipe() bool { + fi, err := os.Stdin.Stat() + if err != nil { + return false + } + if fi.Mode()&os.ModeNamedPipe == 0 { + return false + } else { + return true + } +} + +// main entry point for vespa-logfmt + +func RunLogfmt(opts *Options, args []string) { + if len(args) == 0 { + if !inputIsPipe() { + args = append(args, vespa.FindHome()+"/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()) + } +} |