-
Notifications
You must be signed in to change notification settings - Fork 0
/
postailer_example_test.go
67 lines (59 loc) · 1.14 KB
/
postailer_example_test.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
// +build !windows
package postailer_test
import (
"bufio"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"github.com/Songmu/postailer"
)
func ExamplePostailer_Read() {
appendFile := func(fname, content string) error {
f, _ := os.OpenFile(fname, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644)
f.WriteString(content)
return f.Close()
}
tmpd, _ := ioutil.TempDir("", "example")
if tmpd != "" {
defer os.RemoveAll(tmpd)
}
logfile := filepath.Join(tmpd, "log.log")
posfile := logfile + ".json"
appendFile(logfile, "1\n22\n")
{
pt, _ := postailer.Open(logfile, posfile)
s := bufio.NewScanner(pt)
for s.Scan() {
fmt.Println(s.Text())
}
pt.Close()
}
appendFile(logfile, "333\n")
{
pt, _ := postailer.Open(logfile, posfile)
s := bufio.NewScanner(pt)
for s.Scan() {
fmt.Println(s.Text())
}
pt.Close()
}
appendFile(logfile, "4444\n")
os.Rename(logfile, logfile+".bak")
appendFile(logfile, "newfile:1\nnewfile:22")
{
pt, _ := postailer.Open(logfile, posfile)
s := bufio.NewScanner(pt)
for s.Scan() {
fmt.Println(s.Text())
}
pt.Close()
}
// Output:
// 1
// 22
// 333
// 4444
// newfile:1
// newfile:22
}