2013-01-08 04:54:49 +08:00
|
|
|
// Copyright (c) 2013 ActiveState Software Inc. All rights reserved.
|
|
|
|
|
2012-10-10 06:13:05 +08:00
|
|
|
package tail
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2012-10-14 02:44:47 +08:00
|
|
|
"fmt"
|
2013-05-29 10:33:52 +08:00
|
|
|
"github.com/ActiveState/tail/watch"
|
2012-10-10 06:13:05 +08:00
|
|
|
"io"
|
2012-10-13 07:28:22 +08:00
|
|
|
"launchpad.net/tomb"
|
2012-10-10 06:13:05 +08:00
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2013-05-30 04:57:02 +08:00
|
|
|
var (
|
|
|
|
ErrStop = fmt.Errorf("tail should now stop")
|
|
|
|
)
|
|
|
|
|
2012-10-10 06:13:05 +08:00
|
|
|
type Line struct {
|
2013-05-29 04:53:19 +08:00
|
|
|
Text string
|
|
|
|
Time time.Time
|
2012-10-10 06:13:05 +08:00
|
|
|
}
|
|
|
|
|
2013-03-11 06:17:00 +08:00
|
|
|
// Tail configuration
|
2012-10-13 05:32:04 +08:00
|
|
|
type Config struct {
|
2013-03-11 06:17:00 +08:00
|
|
|
Location int // Tail from last N lines (tail -n)
|
|
|
|
Follow bool // Continue looking for new lines (tail -f)
|
|
|
|
ReOpen bool // Reopen recreated files (tail -F)
|
|
|
|
MustExist bool // Fail early if the file does not exist
|
|
|
|
Poll bool // Poll for file changes instead of using inotify
|
|
|
|
MaxLineSize int // If non-zero, split longer lines into multiple lines
|
2012-10-13 05:32:04 +08:00
|
|
|
}
|
|
|
|
|
2012-10-10 06:13:05 +08:00
|
|
|
type Tail struct {
|
|
|
|
Filename string
|
|
|
|
Lines chan *Line
|
2012-10-13 05:32:04 +08:00
|
|
|
Config
|
2012-10-10 06:13:05 +08:00
|
|
|
|
2012-10-13 05:32:04 +08:00
|
|
|
file *os.File
|
|
|
|
reader *bufio.Reader
|
2013-05-29 07:34:36 +08:00
|
|
|
watcher watch.FileWatcher
|
2013-05-30 04:57:02 +08:00
|
|
|
changes chan bool
|
2012-10-10 06:13:05 +08:00
|
|
|
|
2012-10-13 07:28:22 +08:00
|
|
|
tomb.Tomb // provides: Done, Kill, Dying
|
2012-10-10 06:13:05 +08:00
|
|
|
}
|
|
|
|
|
2013-03-11 06:17:00 +08:00
|
|
|
// TailFile begins tailing the file with the specified
|
|
|
|
// configuration. Output stream is made available via the `Tail.Lines`
|
|
|
|
// channel. To handle errors during tailing, invoke the `Wait` method
|
|
|
|
// after finishing reading from the `Lines` channel.
|
2012-10-13 05:32:04 +08:00
|
|
|
func TailFile(filename string, config Config) (*Tail, error) {
|
|
|
|
if !(config.Location == 0 || config.Location == -1) {
|
2012-10-13 08:14:35 +08:00
|
|
|
panic("only 0/-1 values are supported for Location.")
|
2012-10-13 05:32:04 +08:00
|
|
|
}
|
|
|
|
|
2012-10-13 07:28:22 +08:00
|
|
|
if config.ReOpen && !config.Follow {
|
2012-10-13 08:14:35 +08:00
|
|
|
panic("cannot set ReOpen without Follow.")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !config.Follow {
|
|
|
|
panic("Follow=false is not supported.")
|
2012-10-13 07:28:22 +08:00
|
|
|
}
|
|
|
|
|
2012-10-10 06:13:05 +08:00
|
|
|
t := &Tail{
|
2012-10-13 07:28:22 +08:00
|
|
|
Filename: filename,
|
|
|
|
Lines: make(chan *Line),
|
|
|
|
Config: config}
|
2012-10-10 06:13:05 +08:00
|
|
|
|
2012-10-13 05:32:04 +08:00
|
|
|
if t.Poll {
|
2013-05-29 07:34:36 +08:00
|
|
|
t.watcher = watch.NewPollingFileWatcher(filename)
|
2012-10-12 12:29:06 +08:00
|
|
|
} else {
|
2013-05-29 07:34:36 +08:00
|
|
|
t.watcher = watch.NewInotifyFileWatcher(filename)
|
2012-10-12 12:29:06 +08:00
|
|
|
}
|
|
|
|
|
2012-10-13 05:32:04 +08:00
|
|
|
if t.MustExist {
|
|
|
|
var err error
|
|
|
|
t.file, err = os.Open(t.Filename)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
go t.tailFileSync()
|
2012-10-10 06:13:05 +08:00
|
|
|
|
2012-10-11 06:13:13 +08:00
|
|
|
return t, nil
|
2012-10-10 06:13:05 +08:00
|
|
|
}
|
|
|
|
|
2013-05-30 04:57:02 +08:00
|
|
|
// Stop stops the tailing activity.
|
2012-10-13 07:28:22 +08:00
|
|
|
func (tail *Tail) Stop() error {
|
|
|
|
tail.Kill(nil)
|
|
|
|
return tail.Wait()
|
2012-10-10 06:13:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (tail *Tail) close() {
|
|
|
|
close(tail.Lines)
|
|
|
|
if tail.file != nil {
|
|
|
|
tail.file.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-13 08:14:35 +08:00
|
|
|
func (tail *Tail) reopen() error {
|
2012-10-10 06:13:05 +08:00
|
|
|
if tail.file != nil {
|
|
|
|
tail.file.Close()
|
|
|
|
}
|
|
|
|
for {
|
|
|
|
var err error
|
|
|
|
tail.file, err = os.Open(tail.Filename)
|
|
|
|
if err != nil {
|
2012-10-13 05:32:04 +08:00
|
|
|
if os.IsNotExist(err) {
|
2012-10-13 08:28:04 +08:00
|
|
|
log.Printf("Waiting for %s to appear...", tail.Filename)
|
2013-05-29 10:33:52 +08:00
|
|
|
if err := tail.watcher.BlockUntilExists(tail.Tomb); err != nil {
|
2012-10-13 08:14:35 +08:00
|
|
|
return fmt.Errorf("Failed to detect creation of %s: %s", tail.Filename, err)
|
2012-10-10 06:13:05 +08:00
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
2012-10-13 08:14:35 +08:00
|
|
|
return fmt.Errorf("Unable to open file %s: %s", tail.Filename, err)
|
2012-10-10 06:13:05 +08:00
|
|
|
}
|
2012-10-13 05:32:04 +08:00
|
|
|
break
|
2012-10-10 06:13:05 +08:00
|
|
|
}
|
2012-10-13 08:14:35 +08:00
|
|
|
return nil
|
2012-10-10 06:13:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (tail *Tail) readLine() ([]byte, error) {
|
2012-10-14 03:50:27 +08:00
|
|
|
line, _, err := tail.reader.ReadLine()
|
2012-10-10 06:13:05 +08:00
|
|
|
return line, err
|
|
|
|
}
|
|
|
|
|
2012-10-13 05:32:04 +08:00
|
|
|
func (tail *Tail) tailFileSync() {
|
2012-10-13 07:28:22 +08:00
|
|
|
defer tail.Done()
|
2013-05-30 04:57:02 +08:00
|
|
|
defer tail.close()
|
2012-10-13 07:28:22 +08:00
|
|
|
|
2012-10-13 05:32:04 +08:00
|
|
|
if !tail.MustExist {
|
2013-05-30 04:57:02 +08:00
|
|
|
// deferred first open.
|
2012-10-13 08:14:35 +08:00
|
|
|
err := tail.reopen()
|
|
|
|
if err != nil {
|
|
|
|
tail.Kill(err)
|
|
|
|
return
|
|
|
|
}
|
2012-10-13 05:32:04 +08:00
|
|
|
}
|
2012-10-10 06:13:05 +08:00
|
|
|
|
2013-05-30 04:57:02 +08:00
|
|
|
// Seek to requested location on first open of the file.
|
2012-10-13 05:32:04 +08:00
|
|
|
if tail.Location == 0 {
|
2013-05-30 04:57:02 +08:00
|
|
|
_, err := tail.file.Seek(0, 2) // Seek to the file end
|
2012-10-10 06:13:05 +08:00
|
|
|
if err != nil {
|
2012-10-13 07:28:22 +08:00
|
|
|
tail.Killf("Seek error on %s: %s", tail.Filename, err)
|
2012-10-13 08:14:35 +08:00
|
|
|
return
|
2012-10-10 06:13:05 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-14 03:50:27 +08:00
|
|
|
tail.reader = bufio.NewReader(tail.file)
|
2012-10-10 06:13:05 +08:00
|
|
|
|
2013-05-30 04:57:02 +08:00
|
|
|
// Read line by line.
|
2012-10-10 06:13:05 +08:00
|
|
|
for {
|
|
|
|
line, err := tail.readLine()
|
|
|
|
|
2013-05-30 04:57:02 +08:00
|
|
|
switch(err) {
|
|
|
|
case nil:
|
2012-10-12 12:29:06 +08:00
|
|
|
if line != nil {
|
2013-05-30 04:57:02 +08:00
|
|
|
tail.sendLine(line)
|
2012-10-12 12:29:06 +08:00
|
|
|
}
|
2013-05-30 04:57:02 +08:00
|
|
|
case io.EOF:
|
|
|
|
// When EOF is reached, wait for more data to become
|
|
|
|
// available. Wait strategy is based on the `tail.watcher`
|
|
|
|
// implementation (inotify or polling).
|
|
|
|
err = tail.waitForChanges()
|
|
|
|
if err != nil {
|
|
|
|
if err != ErrStop {
|
|
|
|
tail.Kill(err)
|
2012-10-12 12:29:06 +08:00
|
|
|
}
|
2013-05-30 04:57:02 +08:00
|
|
|
return
|
2012-10-12 12:29:06 +08:00
|
|
|
}
|
2013-05-30 04:57:02 +08:00
|
|
|
default: // non-EOF error
|
|
|
|
tail.Killf("Error reading %s: %s", tail.Filename, err)
|
|
|
|
return
|
2012-10-10 06:13:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
2012-10-13 07:28:22 +08:00
|
|
|
case <-tail.Dying():
|
2012-10-10 06:13:05 +08:00
|
|
|
return
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-30 04:57:02 +08:00
|
|
|
// waitForChanges waits until the file has been appended, deleted,
|
|
|
|
// moved or truncated. When moved, deleted or truncated - the file
|
|
|
|
// will be re-opened if ReOpen is true.
|
|
|
|
func (tail *Tail) waitForChanges() error {
|
|
|
|
if tail.changes == nil {
|
|
|
|
st, err := tail.file.Stat()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
tail.changes = tail.watcher.ChangeEvents(tail.Tomb, st)
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case _, ok := <-tail.changes:
|
|
|
|
if !ok {
|
|
|
|
tail.changes = nil
|
|
|
|
|
|
|
|
// File got deleted/renamed/truncated.
|
|
|
|
if tail.ReOpen {
|
|
|
|
// XXX: no logging in a library?
|
|
|
|
log.Printf("Re-opening moved/deleted/truncated file %s ...", tail.Filename)
|
|
|
|
err := tail.reopen()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
log.Printf("Successfully reopened %s", tail.Filename)
|
|
|
|
tail.reader = bufio.NewReader(tail.file)
|
|
|
|
return nil
|
|
|
|
} else {
|
|
|
|
log.Printf("Stopping tail as file no longer exists: %s", tail.Filename)
|
|
|
|
return ErrStop
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case <-tail.Dying():
|
|
|
|
return ErrStop
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// sendLine sends the line(s) to Lines channel, splitting longer lines
|
|
|
|
// if necessary.
|
|
|
|
func (tail *Tail) sendLine(line []byte) {
|
|
|
|
now := time.Now()
|
|
|
|
lines := []string{string(line)}
|
|
|
|
|
|
|
|
// Split longer lins
|
|
|
|
if tail.MaxLineSize > 0 && len(line) > tail.MaxLineSize {
|
|
|
|
lines = partitionString(
|
|
|
|
string(line), tail.MaxLineSize)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, line := range lines {
|
|
|
|
tail.Lines <- &Line{line, now}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-10-14 03:50:27 +08:00
|
|
|
// partitionString partitions the string into chunks of given size,
|
|
|
|
// with the last chunk of variable size.
|
|
|
|
func partitionString(s string, chunkSize int) []string {
|
|
|
|
if chunkSize <= 0 {
|
|
|
|
panic("invalid chunkSize")
|
|
|
|
}
|
|
|
|
length := len(s)
|
2013-05-29 04:53:19 +08:00
|
|
|
chunks := 1 + length/chunkSize
|
2012-10-14 03:50:27 +08:00
|
|
|
start := 0
|
|
|
|
end := chunkSize
|
|
|
|
parts := make([]string, 0, chunks)
|
|
|
|
for {
|
|
|
|
if end > length {
|
|
|
|
end = length
|
|
|
|
}
|
|
|
|
parts = append(parts, s[start:end])
|
|
|
|
if end == length {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
start, end = end, end+chunkSize
|
|
|
|
}
|
|
|
|
return parts
|
|
|
|
}
|