-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.go
109 lines (88 loc) · 1.81 KB
/
file.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package main
import (
"fmt"
"time"
"os"
"io"
"bytes"
"log"
"bufio"
"github.com/robfig/cron/v3"
)
var position int64
var lines int
func ReadFile() {
position = 0
lines = 0
c := cron.New()
c.AddFunc(Timer, func() {
file, err := os.Open(Source)
if err != nil {
log.Fatal(err)
}
defer file.Close()
lineCount, err := LineCounter(file)
if err != nil {
log.Fatal(err)
}
if lineCount == lines {
position = 0
lines = 0
}
errr := LineReader(file, position)
if errr != nil {
fmt.Println("Scanner error:", err)
}
})
c.Start()
for {
time.Sleep(time.Second)
}
}
func LineCounter(r io.Reader) (int, error) {
var count int
const lineBreak = '\n'
buf := make([]byte, bufio.MaxScanTokenSize)
for {
bufferSize, err := r.Read(buf)
if err != nil && err != io.EOF {
return 0, err
}
var buffPosition int
for {
i := bytes.IndexByte(buf[buffPosition:], lineBreak)
if i == -1 || bufferSize == buffPosition {
break
}
buffPosition += i + 1
count++
}
if err == io.EOF {
break
}
}
return count, nil
}
func LineReader(input io.ReadSeeker, start int64) error {
if _, err := input.Seek(start, 0); err != nil {
return err
}
scanner := bufio.NewScanner(input)
pos := start
scanLines := func(data []byte, atEOF bool) (advance int, token []byte, err error) {
advance, token, err = bufio.ScanLines(data, atEOF)
pos += int64(advance)
return
}
scanner.Split(scanLines)
for scanner.Scan() {
// Toot next line
Toot(fmt.Sprintf("%s", scanner.Text()))
// Send to terminal for debug
//fmt.Printf("%s", scanner.Text())
position = pos
lines++
break
}
return scanner.Err()
}