Skip to content

Commit

Permalink
Maintain log buffer and flush errors at the end
Browse files Browse the repository at this point in the history
  • Loading branch information
zix99 committed Nov 15, 2019
1 parent b332dee commit 006cf79
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 11 deletions.
26 changes: 19 additions & 7 deletions cmd/helpers/extractorBuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,49 +33,49 @@ func BuildExtractorFromArguments(c *cli.Context) *extractor.Extractor {
if ignoreSlice != nil && len(ignoreSlice) > 0 {
ignoreExp, err := extractor.NewIgnoreExpressions(ignoreSlice...)
if err != nil {
stderrLog.Panicln(err)
ErrLog.Panicln(err)
}
config.Ignore = ignoreExp
}

if batchSize < 1 {
stderrLog.Fatalf("Batch size must be >= 1, is %d\n", batchSize)
ErrLog.Fatalf("Batch size must be >= 1, is %d\n", batchSize)
}

fileglobs := c.Args()

if len(fileglobs) == 0 || fileglobs[0] == "-" { // Read from stdin
ret, err := extractor.New(extractor.ConvertReaderToStringChan(os.Stdin, batchSize), &config)
if err != nil {
stderrLog.Panicln(err)
ErrLog.Panicln(err)
}
StartFileReading("<stdin>")
return ret
} else if follow { // Read from source file
if gunzip {
stderrLog.Println("Cannot combine -f and -z")
ErrLog.Println("Cannot combine -f and -z")
}

tailChannels := make([]<-chan []extractor.BString, 0)
for _, filename := range globExpand(fileglobs, recursive) {
tail, err := tail.TailFile(filename, tail.Config{Follow: true, ReOpen: followReopen, Poll: followPoll})

if err != nil {
stderrLog.Fatal("Unable to open file: ", err)
ErrLog.Fatal("Unable to open file: ", err)
}
tailChannels = append(tailChannels, tailLineToChan(tail.Lines, batchSize))
StartFileReading(filename)
}

ret, err := extractor.New(extractor.CombineChannels(tailChannels...), &config)
if err != nil {
stderrLog.Panicln(err)
ErrLog.Panicln(err)
}
return ret
} else { // Read (no-follow) source file(s)
ret, err := extractor.New(openFilesToChan(globExpand(fileglobs, recursive), gunzip, concurrentReaders, batchSize), &config)
if err != nil {
stderrLog.Panicln(err)
ErrLog.Panicln(err)
}
return ret
}
Expand Down Expand Up @@ -144,6 +144,18 @@ func AdaptCommandForExtractor(command cli.Command) *cli.Command {
if command.ArgsUsage == "" {
command.ArgsUsage = DefaultArgumentDescriptor
}

// While this doesn't own the log, this is the last place
// that has the option to flush the log buffer to sderr
originalAfter := command.After
command.After = func(c *cli.Context) error {
DisableAndFlushLogBuffer()
if originalAfter != nil {
return originalAfter(c)
}
return nil
}

return &command
}

Expand Down
30 changes: 29 additions & 1 deletion cmd/helpers/log.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,36 @@
package helpers

import (
"bytes"
"log"
"os"
)

var stderrLog = log.New(os.Stderr, "[Log] ", 0)
var ErrLog *log.Logger

const logPrefix = "[Log] "

var logBuffer *bytes.Buffer

func resetLog() {
ErrLog = log.New(os.Stderr, "[Log] ", 0)
}

func init() {
resetLog()
}

func EnableLogBuffer() {
if logBuffer == nil {
logBuffer = new(bytes.Buffer)
ErrLog = log.New(logBuffer, logPrefix, 0)
}
}

func DisableAndFlushLogBuffer() {
if logBuffer != nil {
os.Stderr.Write(logBuffer.Bytes())
logBuffer = nil
resetLog()
}
}
6 changes: 3 additions & 3 deletions cmd/helpers/readChannels.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func openFileToReader(filename string, gunzip bool) (io.ReadCloser, error) {
if gunzip {
zfile, err := gzip.NewReader(file)
if err != nil {
stderrLog.Printf("Gunzip error for file %s: %v\n", filename, err)
ErrLog.Printf("Gunzip error for file %s: %v\n", filename, err)
} else {
file = zfile
}
Expand All @@ -79,7 +79,7 @@ func openFilesToChan(filenames []string, gunzip bool, concurrency int, batchSize
var file io.ReadCloser
file, err := openFileToReader(goFilename, gunzip)
if err != nil {
stderrLog.Printf("Error opening file %s: %v\n", goFilename, err)
ErrLog.Printf("Error opening file %s: %v\n", goFilename, err)
return
}
defer file.Close()
Expand Down Expand Up @@ -127,7 +127,7 @@ func globExpand(paths []string, recursive bool) []string {
} else {
expanded, err := filepath.Glob(p)
if err != nil {
stderrLog.Printf("Path error: %v\n", err)
ErrLog.Printf("Path error: %v\n", err)
} else {
out = append(out, expanded...)
}
Expand Down
1 change: 1 addition & 0 deletions cmd/helpers/updatingAggregator.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
func RunAggregationLoop(ext *extractor.Extractor, aggregator aggregation.Aggregator, writeOutput func()) {

defer multiterm.ResetCursor()
EnableLogBuffer()

// Updater sync variables
outputDone := make(chan bool)
Expand Down

0 comments on commit 006cf79

Please sign in to comment.