-
Notifications
You must be signed in to change notification settings - Fork 8
/
writer.go
217 lines (183 loc) · 4.32 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package cronowriter
import (
"io"
"os"
"path/filepath"
"sync"
"time"
"github.com/lestrrat-go/strftime"
)
type (
// A CronoWriter writes message to a set of output files.
CronoWriter struct {
pattern *strftime.Strftime // given pattern
path string // current file path
symlink *strftime.Strftime // symbolic link to current file path
fp *os.File // current file pointer
loc *time.Location
mux sync.Locker
log logger
init bool // if true, open the file when New() method is called
}
// A Option with CronoWriter.
Option func(*CronoWriter)
)
var (
_ io.WriteCloser = (*CronoWriter)(nil) // check if object implements interface
now = time.Now // for test
)
// New returns a CronoWriter with the given pattern and options.
func New(pattern string, options ...Option) (*CronoWriter, error) {
p, err := strftime.New(pattern)
if err != nil {
return nil, err
}
c := &CronoWriter{
pattern: p,
path: "",
symlink: nil,
fp: nil,
loc: time.Local,
mux: new(sync.Mutex), // default mutex enable
log: &nopLogger{},
init: false,
}
for _, option := range options {
option(c)
}
if c.init {
if _, err := c.Write([]byte("")); err != nil {
return nil, err
}
}
return c, nil
}
// MustNew is a convenience function equivalent to New that panics on failure
// instead of returning an error.
func MustNew(pattern string, options ...Option) *CronoWriter {
c, err := New(pattern, options...)
if err != nil {
panic(err)
}
return c
}
// WithLocation set the location to loc.
func WithLocation(loc *time.Location) Option {
return func(c *CronoWriter) {
c.loc = loc
}
}
// WithSymlink enables its creates a symbolic link to the specify pattern.
func WithSymlink(pattern string) Option {
return func(c *CronoWriter) {
p, err := strftime.New(pattern)
if err != nil {
panic(err)
}
c.symlink = p
}
}
// WithMutex enables its uses sync.Mutex when file writing.
func WithMutex() Option {
return func(c *CronoWriter) {
c.mux = new(sync.Mutex)
}
}
// WithNopMutex disables its uses sync.Mutex when file writing.
func WithNopMutex() Option {
return func(c *CronoWriter) {
c.mux = new(nopMutex)
}
}
// WithDebug enables output stdout and stderr.
func WithDebug() Option {
return func(c *CronoWriter) {
c.log = newDebugLogger()
}
}
// WithStdout enables output always stdout.
func WithStdout() Option {
return func(c *CronoWriter) {
c.log = newStdoutLogger()
}
}
// WithStderr enables output always stderr.
func WithStderr() Option {
return func(c *CronoWriter) {
c.log = newStderrLogger()
}
}
// WithInit enables its creates output file when CronoWriter initialize.
func WithInit() Option {
return func(c *CronoWriter) {
c.init = true
}
}
// Write writes to the file and rotate files automatically based on current date and time.
func (c *CronoWriter) Write(b []byte) (int, error) {
c.mux.Lock()
defer c.mux.Unlock()
t := now().In(c.loc)
path := c.pattern.FormatString(t)
if c.path != path {
// close file
go func(fp *os.File) {
if fp == nil {
return
}
fp.Close()
}(c.fp)
dir := filepath.Dir(path)
if err := os.MkdirAll(dir, os.ModePerm); err != nil {
return c.write(nil, err)
}
fp, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644)
if err != nil {
return c.write(nil, err)
}
c.createSymlink(t, path)
c.path = path
c.fp = fp
}
return c.write(b, nil)
}
// Path returns the current writing file path.
func (c *CronoWriter) Path() string {
c.mux.Lock()
defer c.mux.Unlock()
return c.path
}
func (c *CronoWriter) createSymlink(t time.Time, path string) {
if c.symlink == nil {
return
}
symlink := c.symlink.FormatString(t)
if symlink == path {
c.log.Error("Can't create symlink. Already file exists.")
return // ignore error
}
if _, err := os.Stat(symlink); err == nil {
if err := os.Remove(symlink); err != nil {
c.log.Error(err)
return // ignore error
}
}
if err := os.Symlink(path, symlink); err != nil {
c.log.Error(err)
return // ignore error
}
}
// Close closes file.
func (c *CronoWriter) Close() error {
c.mux.Lock()
defer c.mux.Unlock()
return c.fp.Close()
}
func (c *CronoWriter) write(b []byte, err error) (int, error) {
if err != nil {
c.log.Error(err)
return 0, err
}
c.log.Write(b)
return c.fp.Write(b)
}