From 6a5092ff5ca4c469712c2def6b4fd157be1b42ef Mon Sep 17 00:00:00 2001 From: Sridhar Ratnakumar Date: Wed, 10 Oct 2012 15:13:13 -0700 Subject: [PATCH] Bug #95787: warn and ignore tailing app logs if their base dir (container) goes away fast .. but not for system logs as we expect /s/logs/ to exist. --- tail.go | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/tail.go b/tail.go index 057d96b..519a051 100644 --- a/tail.go +++ b/tail.go @@ -32,20 +32,24 @@ type Tail struct { // TailFile channels the lines of a logfile along with timestamp. If // end is true, channel only newly added lines. If retry is true, tail // the file name (not descriptor) and retry on file open/read errors. -func TailFile(filename string, maxlinesize int, end bool, retry bool) *Tail { +func TailFile(filename string, maxlinesize int, end bool, retry bool) (*Tail, error) { + watcher, err := fileCreateWatcher(filename) + if err != nil { + return nil, err + } t := &Tail{ filename, make(chan *Line), maxlinesize, nil, nil, - fileCreateWatcher(filename), + watcher, make(chan bool), make(chan bool)} go t.tailFileSync(end, retry) - return t + return t, nil } func (tail *Tail) Stop() { @@ -166,19 +170,19 @@ func (tail *Tail) tailFileSync(end bool, retry bool) { } // returns the watcher for file create events -func fileCreateWatcher(filename string) *fsnotify.Watcher { +func fileCreateWatcher(filename string) (*fsnotify.Watcher, error) { watcher, err := fsnotify.NewWatcher() if err != nil { - panic(fmt.Sprintf("fsnotify error: %s", err)) + return nil, err } // watch on parent directory because the file may not exit. err = watcher.WatchFlags(filepath.Dir(filename), fsnotify.FSN_CREATE) if err != nil { - panic(fmt.Sprintf("fsnotify error on file: %s", err)) + return nil, err } - return watcher + return watcher, nil } // get current time in unix timestamp