mirror of
https://github.com/superseriousbusiness/gotosocial.git
synced 2024-11-26 13:46:39 +00:00
30 lines
521 B
Go
30 lines
521 B
Go
package logger
|
|
|
|
import (
|
|
"io"
|
|
"io/ioutil"
|
|
"sync"
|
|
)
|
|
|
|
// AddSafety wraps an io.Writer to provide mutex locking protection
|
|
func AddSafety(w io.Writer) io.Writer {
|
|
if w == nil {
|
|
w = ioutil.Discard
|
|
} else if sw, ok := w.(*safeWriter); ok {
|
|
return sw
|
|
}
|
|
return &safeWriter{wr: w}
|
|
}
|
|
|
|
// safeWriter wraps an io.Writer to provide mutex locking on write
|
|
type safeWriter struct {
|
|
wr io.Writer
|
|
mu sync.Mutex
|
|
}
|
|
|
|
func (w *safeWriter) Write(b []byte) (int, error) {
|
|
w.mu.Lock()
|
|
defer w.mu.Unlock()
|
|
return w.wr.Write(b)
|
|
}
|