tail/cmd/gotail/gotail.go

57 lines
1.2 KiB
Go
Raw Normal View History

2013-01-08 04:54:49 +08:00
// Copyright (c) 2013 ActiveState Software Inc. All rights reserved.
2012-10-13 00:05:11 +08:00
package main
import (
"fmt"
2012-12-14 21:00:55 +08:00
"github.com/ActiveState/tail"
"flag"
"os"
2012-10-13 00:05:11 +08:00
)
func args2config() tail.Config {
config := tail.Config{Follow: true}
flag.IntVar(&config.Location, "n", 0, "tail from the last Nth location")
flag.BoolVar(&config.Follow, "f", false, "wait for additional data to be appended to the file")
flag.BoolVar(&config.ReOpen, "F", false, "follow, and track file rename/rotation")
2013-05-30 06:07:53 +08:00
flag.BoolVar(&config.Poll, "p", false, "use polling, instead of inotify")
flag.Parse()
if config.ReOpen {
config.Follow = true
}
return config
}
2012-10-13 00:05:11 +08:00
func main() {
config := args2config()
if flag.NFlag() < 1 {
fmt.Println("need one or more files as arguments")
os.Exit(1)
}
done := make(chan bool)
for _, filename := range flag.Args() {
go tailFile(filename, config, done)
}
for _, _ = range flag.Args() {
<-done
}
}
func tailFile(filename string, config tail.Config, done chan bool) {
defer func() { done <- true }()
t, err := tail.TailFile(filename, config)
2012-10-13 00:05:11 +08:00
if err != nil {
fmt.Println(err)
return
2012-10-13 00:05:11 +08:00
}
for line := range t.Lines {
fmt.Println(line.Text)
}
err = t.Wait()
if err != nil {
fmt.Println(err)
}
2012-10-13 00:05:11 +08:00
}