File: concurrentWriter.go

package info (click to toggle)
golang-github-mgutz-logxi 1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 524 kB
  • sloc: makefile: 7
file content (25 lines) | stat: -rw-r--r-- 604 bytes parent folder | download
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
package log

import (
	"io"
	"sync"
)

// ConcurrentWriter is a concurrent safe wrapper around io.Writer
type ConcurrentWriter struct {
	writer io.Writer
	sync.Mutex
}

// NewConcurrentWriter crates a new concurrent writer wrapper around existing writer.
func NewConcurrentWriter(writer io.Writer) io.Writer {
	return &ConcurrentWriter{writer: writer}
}

func (cw *ConcurrentWriter) Write(p []byte) (n int, err error) {
	cw.Lock()
	defer cw.Unlock()
	// This is basically the same logic as in go's log.Output() which
	// doesn't look at the returned number of bytes returned
	return cw.writer.Write(p)
}