-
Notifications
You must be signed in to change notification settings - Fork 86
/
file_log_writer.go
65 lines (49 loc) · 1.12 KB
/
file_log_writer.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
package worker
import (
"os"
"time"
gocontext "context"
)
type fileLogWriter struct {
ctx gocontext.Context
logFile string
fd *os.File
timer *time.Timer
timeout time.Duration
}
func newFileLogWriter(ctx gocontext.Context, logFile string, timeout time.Duration) (LogWriter, error) {
fd, err := os.Create(logFile)
if err != nil {
return nil, err
}
return &fileLogWriter{
ctx: ctx,
logFile: logFile,
fd: fd,
timer: time.NewTimer(time.Hour),
timeout: timeout,
}, nil
}
func (w *fileLogWriter) Write(b []byte) (int, error) {
return w.fd.Write(b)
}
func (w *fileLogWriter) Close() error {
return w.fd.Close()
}
func (w *fileLogWriter) SetMaxLogLength(n int) {}
func (w *fileLogWriter) SetJobStarted(meta *JobStartedMeta) {}
func (w *fileLogWriter) SetCancelFunc(cancel gocontext.CancelFunc) {}
func (w *fileLogWriter) MaxLengthReached() bool {
return false
}
func (w *fileLogWriter) Timeout() <-chan time.Time {
return w.timer.C
}
func (w *fileLogWriter) WriteAndClose(b []byte) (int, error) {
n, err := w.Write(b)
if err != nil {
return n, err
}
err = w.Close()
return n, err
}