You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
31 lines
457 B
Go
31 lines
457 B
Go
package logjson
|
|
|
|
import (
|
|
"io"
|
|
"os"
|
|
"sync"
|
|
)
|
|
|
|
var (
|
|
DefaultStderrSyncWriter = NewSyncWriter(os.Stderr)
|
|
)
|
|
|
|
// NewSyncWriter 返回写互斥的 io.Writer
|
|
func NewSyncWriter(writer io.Writer) io.Writer {
|
|
return &syncWriter{
|
|
writer: writer,
|
|
lock: sync.Mutex{},
|
|
}
|
|
}
|
|
|
|
type syncWriter struct {
|
|
writer io.Writer
|
|
lock sync.Mutex
|
|
}
|
|
|
|
func (w *syncWriter) Write(p []byte) (int, error) {
|
|
w.lock.Lock()
|
|
defer w.lock.Unlock()
|
|
return w.writer.Write(p)
|
|
}
|