refactor
This commit is contained in:
parent
4fb4f2aa10
commit
45a47abe0e
45
tail.go
45
tail.go
|
@ -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 {
|
||||||
Location *SeekInfo // Seek to this location before tailing
|
// File-specifc
|
||||||
Follow bool // Continue looking for new lines (tail -f)
|
Location *SeekInfo // Seek to this location before tailing
|
||||||
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
|
||||||
MaxLineSize int // If non-zero, split longer lines into multiple lines
|
LimitRate int64 // Maximum read rate (lines per second)
|
||||||
LimitRate int64 // If non-zero, limit the rate of read log lines
|
|
||||||
// by this much per second.
|
// Generic IO
|
||||||
|
Follow bool // Continue looking for new lines (tail -f)
|
||||||
|
MaxLineSize int // If non-zero, split longer lines into multiple lines
|
||||||
}
|
}
|
||||||
|
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
|
@ -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
|
||||||
|
}
|
Loading…
Reference in New Issue