github.com/nxadm/tail@v1.4.12-0.20231010141446-ba755e4d73b6/cmd/gotail/gotail.go (about)

     1  // Copyright (c) 2019 FOSS contributors of https://github.com/nxadm/tail
     2  // Copyright (c) 2015 HPE Software Inc. All rights reserved.
     3  // Copyright (c) 2013 ActiveState Software Inc. All rights reserved.
     4  
     5  package main
     6  
     7  import (
     8  	"flag"
     9  	"fmt"
    10      "io"
    11  	"os"
    12  	"github.com/nxadm/tail"
    13  )
    14  
    15  func args2config() (tail.Config, int64) {
    16  	config := tail.Config{Follow: true}
    17  	n := int64(0)
    18  	maxlinesize := int(0)
    19  	flag.Int64Var(&n, "n", 0, "tail from the last Nth location")
    20  	flag.IntVar(&maxlinesize, "max", 0, "max line size")
    21  	flag.BoolVar(&config.Follow, "f", false, "wait for additional data to be appended to the file")
    22  	flag.BoolVar(&config.ReOpen, "F", false, "follow, and track file rename/rotation")
    23  	flag.BoolVar(&config.Poll, "p", false, "use polling, instead of inotify")
    24  	flag.Parse()
    25  	if config.ReOpen {
    26  		config.Follow = true
    27  	}
    28  	config.MaxLineSize = maxlinesize
    29  	return config, n
    30  }
    31  
    32  func main() {
    33  	config, n := args2config()
    34  	if flag.NFlag() < 1 {
    35  		fmt.Println("need one or more files as arguments")
    36  		os.Exit(1)
    37  	}
    38  
    39  	if n != 0 {
    40  		config.Location = &tail.SeekInfo{Offset: -n, Whence: io.SeekEnd}
    41  	}
    42  
    43  	done := make(chan bool)
    44  	for _, filename := range flag.Args() {
    45  		go tailFile(filename, config, done)
    46  	}
    47  
    48  	for range flag.Args() {
    49  		<-done
    50  	}
    51  }
    52  
    53  func tailFile(filename string, config tail.Config, done chan bool) {
    54  	defer func() { done <- true }()
    55  	t, err := tail.TailFile(filename, config)
    56  	if err != nil {
    57  		fmt.Println(err)
    58  		return
    59  	}
    60  	for line := range t.Lines {
    61  		fmt.Println(line.Text)
    62  	}
    63  	err = t.Wait()
    64  	if err != nil {
    65  		fmt.Println(err)
    66  	}
    67  }