replace the alarmist panic with Fatal
Fatal shows only a single goroutine stacktrace.
This commit is contained in:
parent
88298d7cf8
commit
56c4b221b8
5
tail.go
5
tail.go
|
@ -5,6 +5,7 @@ package tail
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/ActiveState/tail/util"
|
||||||
"github.com/ActiveState/tail/watch"
|
"github.com/ActiveState/tail/watch"
|
||||||
"io"
|
"io"
|
||||||
"launchpad.net/tomb"
|
"launchpad.net/tomb"
|
||||||
|
@ -63,7 +64,7 @@ type Tail struct {
|
||||||
// `Lines` channel.
|
// `Lines` channel.
|
||||||
func TailFile(filename string, config Config) (*Tail, error) {
|
func TailFile(filename string, config Config) (*Tail, error) {
|
||||||
if config.ReOpen && !config.Follow {
|
if config.ReOpen && !config.Follow {
|
||||||
panic("cannot set ReOpen without Follow.")
|
util.Fatal("cannot set ReOpen without Follow.")
|
||||||
}
|
}
|
||||||
|
|
||||||
t := &Tail{
|
t := &Tail{
|
||||||
|
@ -286,7 +287,7 @@ func (tail *Tail) sendLine(line []byte) bool {
|
||||||
|
|
||||||
// Split longer lines
|
// Split longer lines
|
||||||
if tail.MaxLineSize > 0 && len(line) > tail.MaxLineSize {
|
if tail.MaxLineSize > 0 && len(line) > tail.MaxLineSize {
|
||||||
lines = partitionString(
|
lines = util.PartitionString(
|
||||||
string(line), tail.MaxLineSize)
|
string(line), tail.MaxLineSize)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,10 +1,30 @@
|
||||||
// Copyright (c) 2013 ActiveState Software Inc. All rights reserved.
|
// Copyright (c) 2013 ActiveState Software Inc. All rights reserved.
|
||||||
|
|
||||||
package tail
|
package util
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
"runtime/debug"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Logger struct {
|
||||||
|
*log.Logger
|
||||||
|
}
|
||||||
|
|
||||||
|
var LOGGER = &Logger{log.New(os.Stderr, "", log.LstdFlags)}
|
||||||
|
|
||||||
|
// fatal is like panic except it displays only the current goroutine's stack.
|
||||||
|
func Fatal(format string, v ...interface{}) {
|
||||||
|
// https://github.com/ActiveState/log/blob/master/log.go#L45
|
||||||
|
LOGGER.Output(2, fmt.Sprintf("FATAL -- "+format, v...)+"\n"+string(debug.Stack()))
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
// partitionString partitions the string into chunks of given size,
|
// partitionString partitions the string into chunks of given size,
|
||||||
// with the last chunk of variable size.
|
// with the last chunk of variable size.
|
||||||
func partitionString(s string, chunkSize int) []string {
|
func PartitionString(s string, chunkSize int) []string {
|
||||||
if chunkSize <= 0 {
|
if chunkSize <= 0 {
|
||||||
panic("invalid chunkSize")
|
panic("invalid chunkSize")
|
||||||
}
|
}
|
|
@ -3,6 +3,7 @@
|
||||||
package watch
|
package watch
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/ActiveState/tail/util"
|
||||||
"github.com/howeyc/fsnotify"
|
"github.com/howeyc/fsnotify"
|
||||||
"launchpad.net/tomb"
|
"launchpad.net/tomb"
|
||||||
"os"
|
"os"
|
||||||
|
@ -61,11 +62,11 @@ func (fw *InotifyFileWatcher) ChangeEvents(t *tomb.Tomb, fi os.FileInfo) *FileCh
|
||||||
|
|
||||||
w, err := fsnotify.NewWatcher()
|
w, err := fsnotify.NewWatcher()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
util.Fatal("Error creating fsnotify watcher: %v", err)
|
||||||
}
|
}
|
||||||
err = w.Watch(fw.Filename)
|
err = w.Watch(fw.Filename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
util.Fatal("Error watching %v: %v", fw.Filename, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
fw.Size = fi.Size()
|
fw.Size = fi.Size()
|
||||||
|
@ -101,8 +102,8 @@ func (fw *InotifyFileWatcher) ChangeEvents(t *tomb.Tomb, fi os.FileInfo) *FileCh
|
||||||
changes.NotifyDeleted()
|
changes.NotifyDeleted()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
// XXX: no panic here
|
// XXX: report this error back to the user
|
||||||
panic(err)
|
util.Fatal("Failed to stat file %v: %v", fw.Filename, err)
|
||||||
}
|
}
|
||||||
fw.Size = fi.Size()
|
fw.Size = fi.Size()
|
||||||
|
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
package watch
|
package watch
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/ActiveState/tail/util"
|
||||||
"launchpad.net/tomb"
|
"launchpad.net/tomb"
|
||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
@ -43,7 +44,7 @@ func (fw *PollingFileWatcher) ChangeEvents(t *tomb.Tomb, origFi os.FileInfo) *Fi
|
||||||
var prevModTime time.Time
|
var prevModTime time.Time
|
||||||
|
|
||||||
// XXX: use tomb.Tomb to cleanly manage these goroutines. replace
|
// XXX: use tomb.Tomb to cleanly manage these goroutines. replace
|
||||||
// the panic (below) with tomb's Kill.
|
// the fatal (below) with tomb's Kill.
|
||||||
|
|
||||||
fw.Size = origFi.Size()
|
fw.Size = origFi.Size()
|
||||||
|
|
||||||
|
@ -66,8 +67,8 @@ func (fw *PollingFileWatcher) ChangeEvents(t *tomb.Tomb, origFi os.FileInfo) *Fi
|
||||||
changes.NotifyDeleted()
|
changes.NotifyDeleted()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
/// XXX: do not panic here.
|
// XXX: report this error back to the user
|
||||||
panic(err)
|
util.Fatal("Failed to stat file %v: %v", fw.Filename, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// File got moved/renamed?
|
// File got moved/renamed?
|
||||||
|
|
Loading…
Reference in New Issue