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"
|
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"
|
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
|
watcher FileWatcher
|
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 {
|
2012-10-12 12:29:06 +08:00
|
|
|
t.watcher = NewPollingFileWatcher(filename)
|
|
|
|
} else {
|
|
|
|
t.watcher = NewInotifyFileWatcher(filename)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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 04:27:56 +08:00
|
|
|
if err := tail.watcher.BlockUntilExists(); 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()
|
|
|
|
|
2012-10-13 05:32:04 +08:00
|
|
|
if !tail.MustExist {
|
2012-10-13 08:14:35 +08:00
|
|
|
err := tail.reopen()
|
|
|
|
if err != nil {
|
|
|
|
tail.close()
|
|
|
|
tail.Kill(err)
|
|
|
|
return
|
|
|
|
}
|
2012-10-13 05:32:04 +08:00
|
|
|
}
|
2012-10-10 06:13:05 +08:00
|
|
|
|
2012-10-12 12:29:06 +08:00
|
|
|
var changes chan bool
|
|
|
|
|
2012-10-13 08:14:35 +08:00
|
|
|
// Note: seeking to end happens only at the beginning of tail;
|
|
|
|
// never during subsequent re-opens.
|
2012-10-13 05:32:04 +08:00
|
|
|
if tail.Location == 0 {
|
2012-10-10 06:13:05 +08:00
|
|
|
_, err := tail.file.Seek(0, 2) // seek to end of the file
|
|
|
|
if err != nil {
|
2012-10-13 08:14:35 +08:00
|
|
|
tail.close()
|
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
|
|
|
|
|
|
|
for {
|
|
|
|
line, err := tail.readLine()
|
|
|
|
|
2012-10-12 12:29:06 +08:00
|
|
|
if err == nil {
|
|
|
|
if line != nil {
|
2012-10-30 10:53:48 +08:00
|
|
|
now := time.Now()
|
2012-10-14 03:50:27 +08:00
|
|
|
if tail.MaxLineSize > 0 && len(line) > tail.MaxLineSize {
|
|
|
|
for _, line := range partitionString(string(line), tail.MaxLineSize) {
|
|
|
|
tail.Lines <- &Line{line, now}
|
|
|
|
}
|
2013-05-29 04:53:19 +08:00
|
|
|
} else {
|
2012-10-14 03:50:27 +08:00
|
|
|
tail.Lines <- &Line{string(line), now}
|
|
|
|
}
|
2012-10-12 12:29:06 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err != io.EOF {
|
|
|
|
tail.close()
|
2012-10-13 07:28:22 +08:00
|
|
|
tail.Killf("Error reading %s: %s", tail.Filename, err)
|
2012-10-12 12:29:06 +08:00
|
|
|
return
|
|
|
|
}
|
2012-10-10 06:13:05 +08:00
|
|
|
|
2012-10-12 12:29:06 +08:00
|
|
|
// When end of file is reached, wait for more data to
|
|
|
|
// become available. Wait strategy is based on the
|
|
|
|
// `tail.watcher` implementation (inotify or polling).
|
|
|
|
if err == io.EOF {
|
|
|
|
if changes == nil {
|
2013-05-28 06:21:02 +08:00
|
|
|
st, err := tail.file.Stat()
|
|
|
|
if err != nil {
|
|
|
|
tail.close()
|
|
|
|
tail.Kill(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
changes = tail.watcher.ChangeEvents(st)
|
2012-10-12 12:29:06 +08:00
|
|
|
}
|
2012-10-10 06:13:05 +08:00
|
|
|
|
2012-10-12 12:47:58 +08:00
|
|
|
select {
|
|
|
|
case _, ok := <-changes:
|
|
|
|
if !ok {
|
2013-05-28 06:21:02 +08:00
|
|
|
changes = nil // XXX: how to kill changes' goroutine?
|
|
|
|
|
2013-05-29 04:27:56 +08:00
|
|
|
// File got deleted/renamed/truncated.
|
2012-10-13 05:32:04 +08:00
|
|
|
if tail.ReOpen {
|
2012-10-13 08:14:35 +08:00
|
|
|
// TODO: no logging in a library?
|
2013-05-11 02:18:00 +08:00
|
|
|
log.Printf("Re-opening moved/deleted/truncated file %s ...", tail.Filename)
|
2012-10-13 08:14:35 +08:00
|
|
|
err := tail.reopen()
|
|
|
|
if err != nil {
|
|
|
|
tail.close()
|
|
|
|
tail.Kill(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
log.Printf("Successfully reopened %s", tail.Filename)
|
2012-10-14 03:50:27 +08:00
|
|
|
tail.reader = bufio.NewReader(tail.file)
|
2013-05-29 04:53:19 +08:00
|
|
|
|
2012-10-12 12:47:58 +08:00
|
|
|
continue
|
|
|
|
} else {
|
2012-10-13 08:14:35 +08:00
|
|
|
log.Printf("Finishing because file has been moved/deleted: %s", tail.Filename)
|
2012-10-12 12:47:58 +08:00
|
|
|
tail.close()
|
|
|
|
return
|
|
|
|
}
|
2012-10-12 12:29:06 +08:00
|
|
|
}
|
2012-10-13 07:28:22 +08:00
|
|
|
case <-tail.Dying():
|
|
|
|
tail.close()
|
2012-10-12 12:47:58 +08:00
|
|
|
return
|
2012-10-12 12:29:06 +08:00
|
|
|
}
|
|
|
|
}
|
2012-10-10 06:13:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
2012-10-13 07:28:22 +08:00
|
|
|
case <-tail.Dying():
|
|
|
|
tail.close()
|
2012-10-10 06:13:05 +08:00
|
|
|
return
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|