-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtransitionwriter.go
More file actions
49 lines (44 loc) · 839 Bytes
/
transitionwriter.go
File metadata and controls
49 lines (44 loc) · 839 Bytes
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
package main
import (
"bufio"
"log"
"os"
"sync"
"time"
)
type TransitionWriter struct {
fh *os.File
writer *bufio.Writer
lock sync.Mutex
writer_initialized bool
}
func (w *TransitionWriter) Init(filename string, quitFlag *bool) {
var err error
w.fh, err = os.Create(filename)
if err != nil {
log.Fatal(err)
}
w.writer = bufio.NewWriter(w.fh)
go func(w *TransitionWriter) {
for !*quitFlag {
w.lock.Lock()
w.writer.Flush()
w.lock.Unlock()
time.Sleep(500 * time.Millisecond)
}
}(w)
w.writer_initialized = true
}
func (w *TransitionWriter) WriteString(st string) {
if w.writer_initialized {
w.lock.Lock()
w.writer.WriteString(st)
w.lock.Unlock()
}
}
func (w *TransitionWriter) Close() {
if w.writer_initialized {
w.writer.Flush()
w.fh.Close()
}
}