This commit is contained in:
2019-12-19 03:16:08 +00:00
parent f0257e1792
commit 9f159ebde5
4 changed files with 193 additions and 9 deletions
+85
View File
@@ -0,0 +1,85 @@
package eve
import (
"fmt"
"github.com/hpcloud/tail"
"github.com/radovskyb/watcher"
"log"
"time"
)
func Tail(file string) {
// https://godoc.org/github.com/hpcloud/tail#Config
t, err := tail.TailFile(
file,
tail.Config{
Follow: true,
})
if err != nil {
fmt.Println(err)
}
//totalLines := 0
//for _ = range t.Lines {
// totalLines ++
//}
//currentLine := 0
for line := range t.Lines {
//fmt.Printf("%v/%v", currentLine, totalLines)
//currentLine ++
//if currentLine > totalLines {
fmt.Printf("##%v##%v##\n", line.Time, line.Text)
//}
}
}
// sends newly created files for tail'ing
// WIP
func WatchPath(dir string) {
w := watcher.New()
w.FilterOps()
w.FilterOps(watcher.Create, watcher.Remove, watcher.Write)
//r := regexp.MustCompile("^abc$")
//w.AddFilterHook(watcher.RegexFilterHook(r, false))
// dispatch creation/removal events here
go func() {
for {
select {
case event := <-w.Event:
// fmt.Println(event)
fmt.Printf("%s (%s)\n", event.Path, event.Op)
case err := <-w.Error:
log.Fatalln(err)
case <-w.Closed:
return
}
}
}()
if err := w.Add(dir); err != nil {
log.Fatalln(err)
}
for path, f := range w.WatchedFiles() {
fmt.Printf("%s: %s\n", path, f.Name())
}
fmt.Println("=== startup complete ===")
// go func() {
// w.Wait()
// w.TriggerEvent(watcher.Create, nil)
// }()
if err := w.Start(time.Millisecond * 100); err != nil {
log.Fatalln(err)
}
}