diff --git a/cmd/helpers/extractorBuilder.go b/cmd/helpers/extractorBuilder.go index f9751ad..72b3da0 100644 --- a/cmd/helpers/extractorBuilder.go +++ b/cmd/helpers/extractorBuilder.go @@ -33,13 +33,13 @@ 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() @@ -47,13 +47,13 @@ func BuildExtractorFromArguments(c *cli.Context) *extractor.Extractor { 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("") 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) @@ -61,7 +61,7 @@ func BuildExtractorFromArguments(c *cli.Context) *extractor.Extractor { 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) @@ -69,13 +69,13 @@ func BuildExtractorFromArguments(c *cli.Context) *extractor.Extractor { 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 } @@ -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 } diff --git a/cmd/helpers/log.go b/cmd/helpers/log.go index f07ae4e..9b4175b 100644 --- a/cmd/helpers/log.go +++ b/cmd/helpers/log.go @@ -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() + } +} diff --git a/cmd/helpers/readChannels.go b/cmd/helpers/readChannels.go index eb053d5..c284ffe 100644 --- a/cmd/helpers/readChannels.go +++ b/cmd/helpers/readChannels.go @@ -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 } @@ -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() @@ -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...) } diff --git a/cmd/helpers/updatingAggregator.go b/cmd/helpers/updatingAggregator.go index f8bea6b..2441093 100644 --- a/cmd/helpers/updatingAggregator.go +++ b/cmd/helpers/updatingAggregator.go @@ -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)