1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
12 // logScanner is an io.Writer that calls ReportFunc(pattern) the first
13 // time one of the Patterns appears in the data. Patterns must not
15 type logScanner struct {
17 ReportFunc func(pattern, text string)
22 func (s *logScanner) Write(p []byte) (int, error) {
24 // We only call reportFunc once. Once we've called it
25 // there's no need to buffer/search subsequent writes.
28 split := bytes.LastIndexByte(p, '\n')
32 s.buf.Write(p[:split+1])
34 for _, pattern := range s.Patterns {
35 if found := strings.Index(txt, pattern); found >= 0 {
36 // Report the entire line where the pattern
38 txt = txt[strings.LastIndexByte(txt[:found], '\n')+1:]
39 if end := strings.IndexByte(txt, '\n'); end >= 0 {
42 s.ReportFunc(pattern, txt)
51 n, err := s.buf.Write(p[split+1:])
52 return n + split + 1, err