Files

132 lines
2.5 KiB
Go

package eve
import (
"fmt"
"github.com/hpcloud/tail"
"github.com/radovskyb/watcher"
"log"
"os"
"strings"
"time"
)
func Tail(file string) {
// https://godoc.org/github.com/hpcloud/tail#Config
//t, err := follower.New(file, follower.Config{
// Whence: io.SeekEnd,
// //Offset: 0,
// Reopen: true,
//})
//t, err := scroller.NewScroller(file)
stats, err := os.Stat(file)
if err != nil {
fmt.Println(err)
}
size := stats.Size()
t, err := tail.TailFile(
file,
tail.Config{
Follow: true,
Location: &tail.SeekInfo{Offset: size, Whence: 0},
})
if err != nil {
fmt.Println(err)
}
//totalLines := 0
//for _ = range t.Lines {
// totalLines ++
//}
//currentLine := 0
for line := range t.Lines {
BasicLineParse(line.Text)
//fmt.Printf("%v/%v", currentLine, totalLines)
//currentLine ++
//if currentLine > totalLines {
// fmt.Printf("##%v##%v##\n", line.Time, line.Text)
//}
}
}
// clean the eve log format a little
func BasicLineParse(input string) {
//// Make a Regex to say we only want letters and numbers
//reg, err := regexp.Compile("[^a-zA-Z0-9 ./+-_=]+")
//if err != nil {
// log.Fatal(err)
//}
//processedString := reg.ReplaceAllString(input, "")
splitBySpaces := strings.Split(input, " ")
//runedString := []rune(splitBySpaces)
// count how many characters.. minus the 20 at the end
// EG:
// "Rookie Help_20191218_161504.txt"
// becomes: "Rookie Help"
//chars := len(c) - 20
//myShortString := string(c[0:chars])
//fmt.Printf("#%v# |%v| @%v@\n\n",a[1], a[2], a[3:])
fmt.Printf("%s\n", splitBySpaces[4:])
//return "asdasd"
}
// 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)
}
}