-
Notifications
You must be signed in to change notification settings - Fork 23
/
handler_file.go
193 lines (175 loc) · 4.05 KB
/
handler_file.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
package logging
import (
"bytes"
"io"
"os"
"path/filepath"
)
func ReadN(reader io.Reader, b []byte) (int, error) {
length := len(b)
i := 0
for i < length {
n, err := reader.Read(b[i:])
i += n
if err != nil {
return i, err
}
}
return i, nil
}
func WriteN(writer io.Writer, b []byte) (int, error) {
length := len(b)
i := 0
for i < length {
n, err := writer.Write(b[i:])
i += n
if err != nil {
return i, err
}
}
return i, nil
}
// A class wraps os.File to the stream interface.
type FileStream struct {
File *os.File
BufferSize int
Buffer *bytes.Buffer
Offset int64
}
func NewFileStream(f *os.File, bufferSize int) *FileStream {
var buf *bytes.Buffer
if bufferSize > 0 {
buf = bytes.NewBuffer(make([]byte, 0, bufferSize))
}
return &FileStream{
File: f,
BufferSize: bufferSize,
Buffer: buf,
}
}
func (self *FileStream) Tell() (int64, error) {
if self.Offset == 0 {
fileInfo, err := self.File.Stat()
if err != nil {
return 0, err
}
self.Offset = fileInfo.Size()
if self.BufferSize > 0 {
self.Offset += int64(self.Buffer.Len())
}
}
return self.Offset, nil
}
func (self *FileStream) Write(s string) error {
// NOTICE: For performance optimization, don't sync inner buffer to
// disk everytime it writes something.
if self.BufferSize > 0 {
length := len(s)
if self.Buffer.Len()+length > self.BufferSize {
self.doFlushBuffer()
if length > self.BufferSize {
n, err := WriteN(self.File, []byte(s))
self.Offset += int64(n)
return err
}
}
_, err := self.Buffer.Write([]byte(s))
return err
} else {
n, err := WriteN(self.File, []byte(s))
self.Offset += int64(n)
return err
}
}
func (self *FileStream) doFlushBuffer() error {
if self.Buffer.Len() > 0 {
n, err := WriteN(self.File, self.Buffer.Bytes())
self.Offset += int64(n)
if err != nil {
return err
}
self.Buffer.Reset()
}
return nil
}
func (self *FileStream) Flush() error {
if self.BufferSize > 0 {
return self.doFlushBuffer()
}
return nil
}
func (self *FileStream) Close() error {
self.Flush()
return self.File.Close()
}
// A handler class which writes formatted logging records to disk files.
type FileHandler struct {
*StreamHandler
filepath string
mode int
bufferSize int
}
// Open the specified file and use it as the stream for logging.
func NewFileHandler(filename string, mode int, bufferSize int) (*FileHandler, error) {
// keep the absolute path, otherwise derived classes which use this
// may come a cropper when the current directory changes.
filepath, err := filepath.Abs(filename)
if err != nil {
return nil, err
}
handler := NewStreamHandler(filepath, LevelNotset, nil)
object := &FileHandler{
StreamHandler: handler,
filepath: filepath,
mode: mode,
bufferSize: bufferSize,
}
if err = object.Open(); err != nil {
return nil, err
}
Closer.RemoveHandler(object.StreamHandler)
Closer.AddHandler(object)
return object, nil
}
// Return the absolute path of logging file.
func (self *FileHandler) GetFilePath() string {
return self.filepath
}
// Open the current base file with the (original) mode and encoding,
// and set it to the underlying stream handler.
// Return non-nil error if error happens.
func (self *FileHandler) Open() error {
var file *os.File
var err error
for {
file, err = os.OpenFile(
self.filepath, os.O_WRONLY|os.O_CREATE|self.mode, 0666)
if err == nil {
break
}
// try to create all the parent directories for specified log file
// if it doesn't exist
if os.IsNotExist(err) {
err2 := os.MkdirAll(filepath.Dir(self.filepath), 0755)
if err2 != nil {
return err
}
continue
}
return err
}
stream := NewFileStream(file, self.bufferSize)
self.StreamHandler.SetStream(stream)
return nil
}
// Emit a record.
func (self *FileHandler) Emit(record *LogRecord) error {
return self.StreamHandler.Emit2(self, record)
}
func (self *FileHandler) Handle(record *LogRecord) int {
return self.Handle2(self, record)
}
// Close this file handler.
func (self *FileHandler) Close() {
self.StreamHandler.Close2()
}