diff options
Diffstat (limited to 'vendor/gopkg.in/clog.v1/file.go')
-rw-r--r-- | vendor/gopkg.in/clog.v1/file.go | 41 |
1 files changed, 38 insertions, 3 deletions
diff --git a/vendor/gopkg.in/clog.v1/file.go b/vendor/gopkg.in/clog.v1/file.go index 27a36efa..a0157d40 100644 --- a/vendor/gopkg.in/clog.v1/file.go +++ b/vendor/gopkg.in/clog.v1/file.go @@ -17,6 +17,7 @@ package clog import ( "bytes" "fmt" + "io" "io/ioutil" "log" "os" @@ -58,6 +59,9 @@ type FileConfig struct { } type file struct { + // Indicates whether object is been used in standalone mode. + standalone bool + *log.Logger Adapter @@ -77,6 +81,21 @@ func newFile() Logger { } } +// NewFileWriter returns an io.Writer for synchronized file logger in standalone mode. +func NewFileWriter(filename string, cfg FileRotationConfig) (io.Writer, error) { + f := &file{ + standalone: true, + } + if err := f.Init(FileConfig{ + Filename: filename, + FileRotationConfig: cfg, + }); err != nil { + return nil, err + } + + return f, nil +} + func (f *file) Level() LEVEL { return f.level } var newLineBytes = []byte("\n") @@ -196,7 +215,9 @@ func (f *file) Init(v interface{}) (err error) { f.initRotate() } - f.msgChan = make(chan *Message, cfg.BufferSize) + if !f.standalone { + f.msgChan = make(chan *Message, cfg.BufferSize) + } return nil } @@ -205,11 +226,15 @@ func (f *file) ExchangeChans(errorChan chan<- error) chan *Message { return f.msgChan } -func (f *file) write(msg *Message) { +func (f *file) write(msg *Message) int { f.Logger.Print(msg.Body) + bytesWrote := len(msg.Body) + if !f.standalone { + bytesWrote += LOG_PREFIX_LENGTH + } if f.rotate.Rotate { - f.currentSize += int64(LOG_PREFIX_LENGTH + len(msg.Body)) + f.currentSize += int64(bytesWrote) f.currentLines++ // TODO: should I care if log message itself contains new lines? var ( @@ -243,6 +268,16 @@ func (f *file) write(msg *Message) { f.currentLines = 0 } } + return bytesWrote +} + +var _ io.Writer = new(file) + +// Write implements method of io.Writer interface. +func (f *file) Write(p []byte) (int, error) { + return f.write(&Message{ + Body: string(p), + }), nil } func (f *file) Start() { |