-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwriter.go
157 lines (141 loc) · 3.7 KB
/
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
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package rotate
import (
"fmt"
"os"
"path/filepath"
"sync"
"time"
"github.com/kei2100/rotate/internal/file"
"github.com/kei2100/rotate/internal/state"
"github.com/kei2100/rotate/logger"
)
// NewWriter creates a *rotate.Writer
func NewWriter(dir, filename string, opts ...OptionFunc) (*Writer, error) {
var opt option
opt.apply(opts...)
filePath := filepath.Join(dir, filename)
f, err := file.OpenFile(filePath, os.O_WRONLY|os.O_CREATE|os.O_APPEND, opt.permission)
if err != nil {
return nil, err
}
fi, err := f.Stat()
if err != nil {
return nil, err
}
return &Writer{
f: f,
state: state.NewState(time.Now().Unix(), fi.Size()),
filePath: filePath,
opt: opt,
}, nil
}
// Writer is a rotating file writer
type Writer struct {
mu sync.RWMutex
f *os.File
state *state.State
filePath string
opt option
}
// Write implements io.Writer
func (w *Writer) Write(p []byte) (int, error) {
w.mu.RLock()
defer w.mu.RUnlock()
n, err := w.f.Write(p)
if err != nil {
return n, err
}
w.state.AddSize(int64(n))
if !w.opt.policy.NeedRotate(FileState{OpenedAt: w.state.OpenedAt(), Size: w.state.Size()}) {
return n, nil
}
if !w.state.CompareAndSwapAsRotating() {
return n, nil
}
go func(current *os.File, st *state.State, opt option) {
if err := pushAndShiftKeeps(w.filePath, opt.keeps); err != nil {
logger.Println(err)
logger.Println("rotate: wait for rotate until next writing")
st.CompareAndSwapAsNotRotating()
return
}
next, err := file.OpenFile(w.filePath, os.O_WRONLY|os.O_CREATE|os.O_EXCL, opt.permission)
if err != nil {
logger.Println(err)
logger.Println("rotate: wait for rotate until next writing")
st.CompareAndSwapAsNotRotating()
return
}
w.mu.Lock()
defer w.mu.Unlock()
if st.IsClosed() {
if err := next.Close(); err != nil {
logger.Printf("rotate: an error occurred while closing next file: %+v", err)
}
return
}
if err := current.Close(); err != nil {
logger.Printf("rotate: an error occurred while closing current file: %+v", err)
// not return
}
w.f = next
w.state = state.NewState(time.Now().Unix(), 0)
}(w.f, w.state, w.opt)
return n, nil
}
// Close closes the file and releases resources
func (w *Writer) Close() error {
w.mu.RLock()
w.state.StoreAsClosed()
err := w.f.Close()
w.mu.RUnlock()
return err
}
func formatRotatedPath(path string, num int) string {
return fmt.Sprintf("%s.%d", path, num)
}
// e.g. path "log", keeps 3
// - log > log.1 | log.1 > log.2 | log.2 > log.3 | log.3 > remove
// - log > log.1 | log.1 > log.2 | | log.3 > noop
// - | log.1 > noop | log.2 > noop | log.3 > noop
func pushAndShiftKeeps(path string, keeps int) error {
if _, err := os.Stat(path); err != nil {
if os.IsNotExist(err) {
return nil
}
return fmt.Errorf("rotate: failed to get stat %s: %+v", path, err)
}
if keeps < 0 {
keeps = 0
}
files := make([]string, 0, keeps+1)
// - [log.3 log.2 log.1]
// - [log.3 log.1]
for i := keeps; i > 0; i-- {
p := formatRotatedPath(path, i)
if _, err := os.Stat(p); err != nil {
if os.IsNotExist(err) {
continue
}
return fmt.Errorf("rotate: failed to get stat %s: %+v", p, err)
}
files = append(files, p)
}
// - [log.3 log.2 log.1 log]
// - [log.3 log.1 log]
files = append(files, path)
if len(files) > keeps {
if err := os.Remove(files[0]); err != nil && !os.IsNotExist(err) {
return fmt.Errorf("rotate: failed to remove %s", files[0])
}
// [log.2 log.1 log]
files = files[1:]
}
for i, old := range files {
nw := formatRotatedPath(path, len(files)-i)
if err := os.Rename(old, nw); err != nil && !os.IsNotExist(err) {
return fmt.Errorf("rotate: failed to rename %s to %s", old, nw)
}
}
return nil
}