Skip to content

Commit 6457a0b

Browse files
committed
#5 prepare macOS file tailer for multiple log file support
1 parent 49d9be3 commit 6457a0b

15 files changed

+1167
-361
lines changed

tailer/fileTailer.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,6 @@ func (f *fileTailer) Errors() chan Error {
4444
return f.errors
4545
}
4646

47-
func RunFseventFileTailer(path string, readall bool, failOnMissingFile bool, logger simpleLogger) Tailer {
48-
return runFileTailer(path, readall, failOnMissingFile, logger, NewFseventWatcher)
49-
}
50-
5147
func RunPollingFileTailer(path string, readall bool, failOnMissingFile bool, pollIntervall time.Duration, logger simpleLogger) Tailer {
5248
makeWatcher := func(abspath string, _ *File) (Watcher, error) {
5349
return NewPollingWatcher(abspath, pollIntervall)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright 2018 The grok_exporter Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
// +build !darwin
16+
17+
package tailer
18+
19+
// old implementation, darwin is already switched to the new implementation, the other OSes will follow
20+
func RunFseventFileTailer(path string, readall bool, failOnMissingFile bool, logger simpleLogger) Tailer {
21+
return runFileTailer(path, readall, failOnMissingFile, logger, NewFseventWatcher)
22+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// Copyright 2018 The grok_exporter Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package tailer
16+
17+
import (
18+
"fmt"
19+
"github.com/fstab/grok_exporter/tailer/fswatcher"
20+
)
21+
22+
type tailerWrapper struct {
23+
lines chan string
24+
errors chan Error
25+
done chan struct{}
26+
}
27+
28+
func (t *tailerWrapper) Close() {
29+
close(t.done)
30+
close(t.lines)
31+
close(t.errors)
32+
}
33+
34+
func (t *tailerWrapper) Lines() chan string {
35+
return t.lines
36+
}
37+
38+
func (t *tailerWrapper) Errors() chan Error {
39+
return t.errors
40+
}
41+
42+
// Switch to the new file tailer implementation which supports watching multiple files.
43+
// Once we switched for all supported operating systems, we can remove the old implementation and the wrapper.
44+
func RunFseventFileTailer(path string, readall bool, failOnMissingFile bool, _ interface{}) Tailer {
45+
result := &tailerWrapper{
46+
lines: make(chan string),
47+
errors: make(chan Error),
48+
done: make(chan struct{}),
49+
}
50+
51+
newTailer, err := fswatcher.Run([]string{path}, readall, failOnMissingFile)
52+
if err != nil {
53+
go func() {
54+
result.errors <- newError("failed to initialize file system watcher", err)
55+
}()
56+
return result
57+
}
58+
59+
go func() {
60+
for {
61+
select {
62+
case l := <-newTailer.Lines():
63+
fmt.Printf("*** forwarding line %q to wrapped tailer\n", l.Line)
64+
result.lines <- l.Line
65+
case e := <-newTailer.Errors():
66+
result.errors <- newError(e.Error(), e.Cause())
67+
result.Close()
68+
return
69+
case <-result.done:
70+
newTailer.Close()
71+
return
72+
}
73+
}
74+
}()
75+
return result
76+
}

tailer/fileTailer_darwin.go

Lines changed: 0 additions & 286 deletions
This file was deleted.

0 commit comments

Comments
 (0)