2024-08-07 10:41:00 +07:00
|
|
|
package log
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
2024-08-27 09:29:42 +07:00
|
|
|
type WriteLocker interface {
|
2024-08-07 10:41:00 +07:00
|
|
|
io.Writer
|
2024-08-27 09:29:42 +07:00
|
|
|
sync.Locker
|
2024-08-07 10:41:00 +07:00
|
|
|
}
|
|
|
|
|
2024-08-27 09:29:42 +07:00
|
|
|
type writeLock struct {
|
2024-08-07 10:41:00 +07:00
|
|
|
sync.Mutex
|
2024-08-27 09:29:42 +07:00
|
|
|
io.Writer
|
2024-08-07 10:41:00 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Lock wraps a WriteSyncer in a mutex to make it safe for concurrent use. In
|
|
|
|
// particular, *os.Files must be locked before use.
|
2024-08-27 09:29:42 +07:00
|
|
|
//
|
|
|
|
// If w is already a WriteLocker, it will be returned as is.
|
|
|
|
func Lock(w io.Writer) WriteLocker {
|
|
|
|
if wl, ok := w.(WriteLocker); ok {
|
2024-08-07 10:41:00 +07:00
|
|
|
// no need to layer on another lock
|
2024-08-27 09:29:42 +07:00
|
|
|
return wl
|
2024-08-07 10:41:00 +07:00
|
|
|
}
|
2024-08-27 09:29:42 +07:00
|
|
|
return &writeLock{Writer: w}
|
2024-08-07 10:41:00 +07:00
|
|
|
}
|