This commit is contained in:
Sridhar Ratnakumar 2013-09-17 16:54:55 -07:00
parent 4fb4f2aa10
commit 45a47abe0e
2 changed files with 38 additions and 34 deletions

35
tail.go
View File

@ -20,8 +20,7 @@ var (
type Line struct { type Line struct {
Text string Text string
Time time.Time Time time.Time
Err error // If non-nil, this is an error from tail, and not a Err error // Error from tail
// log line itself.
} }
// SeekInfo represents arguments to `os.Seek` // SeekInfo represents arguments to `os.Seek`
@ -32,14 +31,16 @@ type SeekInfo struct {
// Config is used to specify how a file must be tailed. // Config is used to specify how a file must be tailed.
type Config struct { type Config struct {
// File-specifc
Location *SeekInfo // Seek to this location before tailing Location *SeekInfo // Seek to this location before tailing
Follow bool // Continue looking for new lines (tail -f)
ReOpen bool // Reopen recreated files (tail -F) ReOpen bool // Reopen recreated files (tail -F)
MustExist bool // Fail early if the file does not exist MustExist bool // Fail early if the file does not exist
Poll bool // Poll for file changes instead of using inotify Poll bool // Poll for file changes instead of using inotify
LimitRate int64 // Maximum read rate (lines per second)
// Generic IO
Follow bool // Continue looking for new lines (tail -f)
MaxLineSize int // If non-zero, split longer lines into multiple lines MaxLineSize int // If non-zero, split longer lines into multiple lines
LimitRate int64 // If non-zero, limit the rate of read log lines
// by this much per second.
} }
type Tail struct { type Tail struct {
@ -298,27 +299,3 @@ func (tail *Tail) sendLine(line []byte) bool {
return true return true
} }
// 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)
chunks := 1 + length/chunkSize
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
}

27
util.go Normal file
View File

@ -0,0 +1,27 @@
// Copyright (c) 2013 ActiveState Software Inc. All rights reserved.
package tail
// 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)
chunks := 1 + length/chunkSize
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
}