Sfoglia il codice sorgente

Use github.com/nxadm/tail in place of github.com/hpcloud/tail

Ask Bjørn Hansen 5 anni fa
parent
commit
efb958fec3

+ 15 - 16
Gopkg.lock

@@ -63,21 +63,6 @@
   revision = "0cd6bf5da1e1c83f8b45653022c74f71af0538a4"
   version = "v1.1.1"
 
-[[projects]]
-  branch = "master"
-  digest = "1:1ebcd0aef6d4512ef3e56932606daec583ea39f2b756111f64d50179ef032130"
-  name = "github.com/hpcloud/tail"
-  packages = [
-    ".",
-    "ratelimiter",
-    "util",
-    "watch",
-    "winfile",
-  ]
-  pruneopts = "NUT"
-  revision = "7d02b9cfe313d6d68d4a184d56d490b5a8ba4163"
-  source = "https://github.com/abh/tail.git"
-
 [[projects]]
   digest = "1:5985ef4caf91ece5d54817c11ea25f182697534f8ae6521eadcd628c142ac4b6"
   name = "github.com/matttproud/golang_protobuf_extensions"
@@ -94,6 +79,20 @@
   pruneopts = "NUT"
   revision = "eda228adcff6f7a80ddaf1d265209a10a4f51ca9"
 
+[[projects]]
+  digest = "1:17995b544cdab2ad44419d8c9f551416c63c0fdee5a8b916373188623ed77ea5"
+  name = "github.com/nxadm/tail"
+  packages = [
+    ".",
+    "ratelimiter",
+    "util",
+    "watch",
+    "winfile",
+  ]
+  pruneopts = "NUT"
+  revision = "327c577245448d8192115e77a76ea3d6aee88202"
+  version = "v1.4.4"
+
 [[projects]]
   branch = "master"
   digest = "1:8458ad87b512968637510fc6caea25f8b734dfda7c35fd5ce00300ecd16ccf46"
@@ -269,8 +268,8 @@
     "github.com/abh/errorutil",
     "github.com/fsnotify/fsnotify",
     "github.com/golang/geo/s2",
-    "github.com/hpcloud/tail",
     "github.com/miekg/dns",
+    "github.com/nxadm/tail",
     "github.com/oschwald/geoip2-golang",
     "github.com/pborman/uuid",
     "github.com/prometheus/client_golang/prometheus",

+ 1 - 1
geodns-logs/process-stats.go

@@ -10,7 +10,7 @@ import (
 	"strings"
 	"sync"
 
-	"github.com/hpcloud/tail"
+	"github.com/nxadm/tail"
 	"github.com/miekg/dns"
 	"github.com/prometheus/client_golang/prometheus"
 	"github.com/prometheus/client_golang/prometheus/promhttp"

+ 0 - 0
vendor/github.com/hpcloud/tail/LICENSE.txt → vendor/github.com/nxadm/tail/LICENSE


+ 0 - 0
vendor/github.com/hpcloud/tail/ratelimiter/Licence → vendor/github.com/nxadm/tail/ratelimiter/Licence


+ 0 - 0
vendor/github.com/hpcloud/tail/ratelimiter/leakybucket.go → vendor/github.com/nxadm/tail/ratelimiter/leakybucket.go


+ 0 - 0
vendor/github.com/hpcloud/tail/ratelimiter/memory.go → vendor/github.com/nxadm/tail/ratelimiter/memory.go


+ 0 - 0
vendor/github.com/hpcloud/tail/ratelimiter/storage.go → vendor/github.com/nxadm/tail/ratelimiter/storage.go


+ 41 - 38
vendor/github.com/hpcloud/tail/tail.go → vendor/github.com/nxadm/tail/tail.go

@@ -15,9 +15,9 @@ import (
 	"sync"
 	"time"
 
-	"github.com/hpcloud/tail/ratelimiter"
-	"github.com/hpcloud/tail/util"
-	"github.com/hpcloud/tail/watch"
+	"github.com/nxadm/tail/ratelimiter"
+	"github.com/nxadm/tail/util"
+	"github.com/nxadm/tail/watch"
 	"gopkg.in/tomb.v1"
 )
 
@@ -26,20 +26,22 @@ var (
 )
 
 type Line struct {
-	Text string
-	Time time.Time
-	Err  error // Error from tail
+	Text     string
+	Num      int
+	SeekInfo SeekInfo
+	Time     time.Time
+	Err      error // Error from tail
 }
 
 // NewLine returns a Line with present time.
-func NewLine(text string) *Line {
-	return &Line{text, time.Now(), nil}
+func NewLine(text string, lineNum int) *Line {
+	return &Line{text, lineNum, SeekInfo{}, time.Now(), nil}
 }
 
-// SeekInfo represents arguments to `os.Seek`
+// SeekInfo represents arguments to `io.Seek`
 type SeekInfo struct {
 	Offset int64
-	Whence int // os.SEEK_*
+	Whence int // io.Seek*
 }
 
 type logger interface {
@@ -78,8 +80,9 @@ type Tail struct {
 	Lines    chan *Line
 	Config
 
-	file   *os.File
-	reader *bufio.Reader
+	file    *os.File
+	reader  *bufio.Reader
+	lineNum int
 
 	watcher watch.FileWatcher
 	changes *watch.FileChanges
@@ -113,7 +116,7 @@ func TailFile(filename string, config Config) (*Tail, error) {
 
 	// when Logger was not specified in config, use default logger
 	if t.Logger == nil {
-		t.Logger = log.New(os.Stderr, "", log.LstdFlags)
+		t.Logger = DefaultLogger
 	}
 
 	if t.Poll {
@@ -135,15 +138,15 @@ func TailFile(filename string, config Config) (*Tail, error) {
 	return t, nil
 }
 
-// Return the file's current position, like stdio's ftell().
+// Tell returns the file's current position, like stdio's ftell().
 // But this value is not very accurate.
-// it may readed one line in the chan(tail.Lines),
-// so it may lost one line.
+// One line from the chan(tail.Lines) may have been read,
+// so it may have lost one line.
 func (tail *Tail) Tell() (offset int64, err error) {
 	if tail.file == nil {
 		return
 	}
-	offset, err = tail.file.Seek(0, os.SEEK_CUR)
+	offset, err = tail.file.Seek(0, io.SeekCurrent)
 	if err != nil {
 		return
 	}
@@ -186,6 +189,7 @@ func (tail *Tail) closeFile() {
 
 func (tail *Tail) reopen() error {
 	tail.closeFile()
+	tail.lineNum = 0
 	for {
 		var err error
 		tail.file, err = OpenFile(tail.Filename)
@@ -241,7 +245,6 @@ func (tail *Tail) tailFileSync() {
 	// Seek to requested location on first open of the file.
 	if tail.Location != nil {
 		_, err := tail.file.Seek(tail.Location.Offset, tail.Location.Whence)
-		tail.Logger.Printf("Seeked %s - %+v\n", tail.Filename, tail.Location)
 		if err != nil {
 			tail.Killf("Seek error on %s: %s", tail.Filename, err)
 			return
@@ -250,16 +253,12 @@ func (tail *Tail) tailFileSync() {
 
 	tail.openReader()
 
-	var offset int64
-	var err error
-
 	// Read line by line.
 	for {
 		// do not seek in named pipes
 		if !tail.Pipe {
 			// grab the position in case we need to back up in the event of a half-line
-			offset, err = tail.Tell()
-			if err != nil {
+			if _, err := tail.Tell(); err != nil {
 				tail.Kill(err)
 				return
 			}
@@ -273,9 +272,9 @@ func (tail *Tail) tailFileSync() {
 			if cooloff {
 				// Wait a second before seeking till the end of
 				// file when rate limit is reached.
-				msg := ("Too much log activity; waiting a second " +
-					"before resuming tailing")
-				tail.Lines <- &Line{msg, time.Now(), errors.New(msg)}
+				msg := ("Too much log activity; waiting a second before resuming tailing")
+				offset, _ := tail.Tell()
+				tail.Lines <- &Line{msg, tail.lineNum, SeekInfo{Offset: offset}, time.Now(), errors.New(msg)}
 				select {
 				case <-time.After(time.Second):
 				case <-tail.Dying():
@@ -295,10 +294,8 @@ func (tail *Tail) tailFileSync() {
 			}
 
 			if tail.Follow && line != "" {
-				// this has the potential to never return the last line if
-				// it's not followed by a newline; seems a fair trade here
-				err := tail.seekTo(SeekInfo{Offset: offset, Whence: 0})
-				if err != nil {
+				tail.sendLine(line)
+				if err := tail.seekEnd(); err != nil {
 					tail.Kill(err)
 					return
 				}
@@ -336,7 +333,7 @@ func (tail *Tail) tailFileSync() {
 // reopened if ReOpen is true. Truncated files are always reopened.
 func (tail *Tail) waitForChanges() error {
 	if tail.changes == nil {
-		pos, err := tail.file.Seek(0, os.SEEK_CUR)
+		pos, err := tail.file.Seek(0, io.SeekCurrent)
 		if err != nil {
 			return err
 		}
@@ -360,10 +357,9 @@ func (tail *Tail) waitForChanges() error {
 			tail.Logger.Printf("Successfully reopened %s", tail.Filename)
 			tail.openReader()
 			return nil
-		} else {
-			tail.Logger.Printf("Stopping tail as file no longer exists: %s", tail.Filename)
-			return ErrStop
 		}
+		tail.Logger.Printf("Stopping tail as file no longer exists: %s", tail.Filename)
+		return ErrStop
 	case <-tail.changes.Truncated:
 		// Always reopen truncated files (Follow is true)
 		tail.Logger.Printf("Re-opening truncated file %s ...", tail.Filename)
@@ -376,20 +372,21 @@ func (tail *Tail) waitForChanges() error {
 	case <-tail.Dying():
 		return ErrStop
 	}
-	panic("unreachable")
 }
 
 func (tail *Tail) openReader() {
+	tail.lk.Lock()
 	if tail.MaxLineSize > 0 {
 		// add 2 to account for newline characters
 		tail.reader = bufio.NewReaderSize(tail.file, tail.MaxLineSize+2)
 	} else {
 		tail.reader = bufio.NewReader(tail.file)
 	}
+	tail.lk.Unlock()
 }
 
 func (tail *Tail) seekEnd() error {
-	return tail.seekTo(SeekInfo{Offset: 0, Whence: os.SEEK_END})
+	return tail.seekTo(SeekInfo{Offset: 0, Whence: io.SeekEnd})
 }
 
 func (tail *Tail) seekTo(pos SeekInfo) error {
@@ -414,13 +411,19 @@ func (tail *Tail) sendLine(line string) bool {
 	}
 
 	for _, line := range lines {
-		tail.Lines <- &Line{line, now, nil}
+		tail.lineNum++
+		offset, _ := tail.Tell()
+		select {
+		case tail.Lines <- &Line{line, tail.lineNum, SeekInfo{Offset: offset}, now, nil}:
+		case <-tail.Dying():
+			return true
+		}
 	}
 
 	if tail.Config.RateLimiter != nil {
 		ok := tail.Config.RateLimiter.Pour(uint16(len(lines)))
 		if !ok {
-			tail.Logger.Printf("Leaky bucket full (%v); entering 1s cooloff period.\n",
+			tail.Logger.Printf("Leaky bucket full (%v); entering 1s cooloff period.",
 				tail.Filename)
 			return false
 		}

+ 1 - 1
vendor/github.com/hpcloud/tail/tail_posix.go → vendor/github.com/nxadm/tail/tail_posix.go

@@ -1,4 +1,4 @@
-// +build linux darwin freebsd netbsd openbsd
+// +build !windows
 
 package tail
 

+ 1 - 1
vendor/github.com/hpcloud/tail/tail_windows.go → vendor/github.com/nxadm/tail/tail_windows.go

@@ -3,7 +3,7 @@
 package tail
 
 import (
-	"github.com/hpcloud/tail/winfile"
+	"github.com/nxadm/tail/winfile"
 	"os"
 )
 

+ 1 - 1
vendor/github.com/hpcloud/tail/util/util.go → vendor/github.com/nxadm/tail/util/util.go

@@ -18,7 +18,7 @@ var LOGGER = &Logger{log.New(os.Stderr, "", log.LstdFlags)}
 
 // fatal is like panic except it displays only the current goroutine's stack.
 func Fatal(format string, v ...interface{}) {
-	// https://github.com/hpcloud/log/blob/master/log.go#L45
+	// https://github.com/nxadm/log/blob/master/log.go#L45
 	LOGGER.Output(2, fmt.Sprintf("FATAL -- "+format, v...)+"\n"+string(debug.Stack()))
 	os.Exit(1)
 }

+ 0 - 0
vendor/github.com/hpcloud/tail/watch/filechanges.go → vendor/github.com/nxadm/tail/watch/filechanges.go


+ 2 - 2
vendor/github.com/hpcloud/tail/watch/inotify.go → vendor/github.com/nxadm/tail/watch/inotify.go

@@ -8,9 +8,9 @@ import (
 	"os"
 	"path/filepath"
 
-	"github.com/hpcloud/tail/util"
+	"github.com/nxadm/tail/util"
 
-	"github.com/fsnotify/fsnotify"
+    "github.com/fsnotify/fsnotify"
 	"gopkg.in/tomb.v1"
 )
 

+ 2 - 2
vendor/github.com/hpcloud/tail/watch/inotify_tracker.go → vendor/github.com/nxadm/tail/watch/inotify_tracker.go

@@ -10,9 +10,9 @@ import (
 	"sync"
 	"syscall"
 
-	"github.com/hpcloud/tail/util"
+	"github.com/nxadm/tail/util"
 
-	"github.com/fsnotify/fsnotify"
+    "github.com/fsnotify/fsnotify"
 )
 
 type InotifyTracker struct {

+ 1 - 1
vendor/github.com/hpcloud/tail/watch/polling.go → vendor/github.com/nxadm/tail/watch/polling.go

@@ -8,7 +8,7 @@ import (
 	"runtime"
 	"time"
 
-	"github.com/hpcloud/tail/util"
+	"github.com/nxadm/tail/util"
 	"gopkg.in/tomb.v1"
 )
 

+ 0 - 0
vendor/github.com/hpcloud/tail/watch/watch.go → vendor/github.com/nxadm/tail/watch/watch.go


+ 0 - 0
vendor/github.com/hpcloud/tail/winfile/winfile.go → vendor/github.com/nxadm/tail/winfile/winfile.go