tail/cmd/gotail/gotail.go

54 lines
1.0 KiB
Go
Raw Normal View History

2012-10-13 00:05:11 +08:00
package main
import (
"fmt"
"github.com/srid/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")
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
}