diff --git a/cmd/run.go b/cmd/run.go index 1d1b915..8cafce0 100644 --- a/cmd/run.go +++ b/cmd/run.go @@ -49,32 +49,34 @@ func runWithConfig(cmd *cobra.Command, args []string, config analyze.AnalyzerCon } }() - iterator, err := analyzer.OpenFileIterator(filenames[0]) - if err != nil { - return fmt.Errorf("failed to open file: %w", err) - } + var errCh chan error + if !config.Analyze { + go func() { + errCh <- analyzer.TailFile(filenames[0]) + }() - if !config.Analyze && !config.Daemon { - go tui.New(analyzer).Run() + if !config.Daemon { + go tui.New(analyzer).Run() + } + } else { + go func() { + errCh <- analyzer.AnalyzeFile(filenames[0]) + }() } + if config.Daemon { if err := systemd.NotifyReady(); err != nil { return fmt.Errorf("failed to notify systemd: %w", err) } } - err = analyzer.RunLoop(iterator) + err = <-errCh for i := 1; i < len(filenames); i++ { if err != nil { break } - iterator, err = analyzer.OpenFileIterator(filenames[i]) - if err != nil { - err = fmt.Errorf("failed to open file: %w", err) - break - } - err = analyzer.RunLoop(iterator) + err = analyzer.AnalyzeFile(filenames[i]) } if config.Analyze { diff --git a/pkg/analyze/analyze.go b/pkg/analyze/analyze.go index a173275..330263d 100644 --- a/pkg/analyze/analyze.go +++ b/pkg/analyze/analyze.go @@ -142,6 +142,23 @@ func (a *Analyzer) RunLoop(iter fileiter.Iterator) error { return nil } +func (a *Analyzer) AnalyzeFile(filename string) error { + f, err := os.Open(filename) + if err != nil { + return err + } + defer f.Close() + return a.RunLoop(fileiter.NewWithScanner(f)) +} + +func (a *Analyzer) TailFile(filename string) error { + iter, err := a.OpenTailIterator(filename) + if err != nil { + return err + } + return a.RunLoop(iter) +} + func (a *Analyzer) handleLine(line []byte) error { logItem, err := a.logParser.Parse(line) if err != nil { diff --git a/pkg/analyze/analyze_test.go b/pkg/analyze/analyze_test.go index 22015ba..fbade14 100644 --- a/pkg/analyze/analyze_test.go +++ b/pkg/analyze/analyze_test.go @@ -28,12 +28,10 @@ func benchmarkAnalyzeLoop(b *testing.B, parserStr string) { b.Fatal(err) } - t, err := a.OpenFileIterator(logPath) + err = a.AnalyzeFile(logPath) if err != nil { b.Fatal(err) } - - a.RunLoop(t) a.PrintTopValues(nil, SortBySize, "") } diff --git a/pkg/analyze/file.go b/pkg/analyze/file.go index 8e985ce..fc7b30b 100644 --- a/pkg/analyze/file.go +++ b/pkg/analyze/file.go @@ -1,7 +1,6 @@ package analyze import ( - "bufio" "io" "os" @@ -56,15 +55,3 @@ func (a *Analyzer) OpenTailIterator(filename string) (fileiter.Iterator, error) } return fileiter.NewWithTail(t), nil } - -func (a *Analyzer) OpenFileIterator(filename string) (fileiter.Iterator, error) { - if !a.Config.Analyze { - return a.OpenTailIterator(filename) - } else { - file, err := os.Open(filename) - if err != nil { - return nil, err - } - return fileiter.NewWithScanner(bufio.NewScanner(file)), nil - } -} diff --git a/pkg/fileiter/iterator.go b/pkg/fileiter/iterator.go index 23160fe..3552f36 100644 --- a/pkg/fileiter/iterator.go +++ b/pkg/fileiter/iterator.go @@ -2,6 +2,7 @@ package fileiter import ( "bufio" + "io" "github.com/nxadm/tail" ) @@ -14,8 +15,9 @@ type scannerIterator struct { scanner *bufio.Scanner } -func NewWithScanner(scanner *bufio.Scanner) Iterator { +func NewWithScanner(r io.Reader) Iterator { // Prepare a large buffer + scanner := bufio.NewScanner(r) scanner.Buffer(make([]byte, 1024*1024), 1024*1024) return &scannerIterator{scanner: scanner} }