pomerium/internal/log/multiwriter.go
dependabot[bot] 615c6257e6
chore(deps): bump github.com/spf13/viper from 1.16.0 to 1.18.2 (#4861)
* chore(deps): bump github.com/spf13/viper from 1.16.0 to 1.18.2

Bumps [github.com/spf13/viper](https://github.com/spf13/viper) from 1.16.0 to 1.18.2.
- [Release notes](https://github.com/spf13/viper/releases)
- [Commits](https://github.com/spf13/viper/compare/v1.16.0...v1.18.2)

---
updated-dependencies:
- dependency-name: github.com/spf13/viper
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>

* fix race

---------

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Caleb Doxsey <cdoxsey@pomerium.com>
2023-12-27 16:16:38 -07:00

48 lines
877 B
Go

package log
import (
"io"
"slices"
"sync"
)
// A MultiWriter dispatches writes to multiple writers.
type MultiWriter struct {
mu sync.Mutex
ws []io.Writer
}
// NewMultiWriter creates a new MultiWriter
func NewMultiWriter() *MultiWriter {
return &MultiWriter{}
}
// Add adds a writer to the multi writer.
func (m *MultiWriter) Add(w io.Writer) {
m.mu.Lock()
m.ws = append(m.ws, w)
m.mu.Unlock()
}
// Remove removes a writer from the multi writer.
func (m *MultiWriter) Remove(w io.Writer) {
m.mu.Lock()
m.ws = slices.DeleteFunc(m.ws, func(mw io.Writer) bool {
return mw == w
})
m.mu.Unlock()
}
// Write writes data to all the writers. The last count and error are returned.
func (m *MultiWriter) Write(data []byte) (int, error) {
var n int
var err error
m.mu.Lock()
for _, w := range m.ws {
n, err = w.Write(data)
}
m.mu.Unlock()
return n, err
}