2012-10-13 00:05:11 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2012-10-18 02:56:54 +08:00
|
|
|
"tail"
|
2012-10-13 08:14:35 +08:00
|
|
|
"flag"
|
2012-10-13 08:28:04 +08:00
|
|
|
"os"
|
2012-10-13 00:05:11 +08:00
|
|
|
)
|
|
|
|
|
2012-10-13 08:14:35 +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() {
|
2012-10-13 08:28:04 +08:00
|
|
|
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 {
|
2012-10-13 08:14:35 +08:00
|
|
|
fmt.Println(err)
|
|
|
|
return
|
2012-10-13 00:05:11 +08:00
|
|
|
}
|
|
|
|
for line := range t.Lines {
|
|
|
|
fmt.Println(line.Text)
|
|
|
|
}
|
2012-10-13 08:14:35 +08:00
|
|
|
err = t.Wait()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
2012-10-13 00:05:11 +08:00
|
|
|
}
|