wip: log
This commit is contained in:
parent
6eada2d348
commit
9f7c004105
@ -1,26 +0,0 @@
|
||||
package log
|
||||
|
||||
import "sync"
|
||||
|
||||
type MapWriter struct {
|
||||
writers map[string]*Writer
|
||||
mtx *sync.Mutex
|
||||
}
|
||||
|
||||
func (m *MapWriter) GetWriter(key string) *Writer {
|
||||
m.mtx.Lock()
|
||||
defer m.mtx.Unlock()
|
||||
|
||||
return m.writers[key]
|
||||
}
|
||||
|
||||
func (m *MapWriter) Add(key string, w *Writer) {
|
||||
if w == nil || len(key) == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
m.mtx.Lock()
|
||||
defer m.mtx.Unlock()
|
||||
|
||||
m.writers[key] = w
|
||||
}
|
||||
@ -8,20 +8,16 @@ import (
|
||||
type Writer struct {
|
||||
writer io.Writer
|
||||
mtx *sync.Mutex
|
||||
wg *sync.WaitGroup
|
||||
} //TODO: create a writer implementation using this struct, ensure sync and prevent abrupt truncation when stopping
|
||||
|
||||
func NewWriter(w io.Writer) *Writer {
|
||||
return &Writer{
|
||||
writer: w,
|
||||
mtx: &sync.Mutex{},
|
||||
wg: &sync.WaitGroup{},
|
||||
}
|
||||
}
|
||||
|
||||
func (w *Writer) Write(b []byte) (int, error) {
|
||||
defer w.wg.Done()
|
||||
|
||||
w.mtx.Lock()
|
||||
defer w.mtx.Unlock()
|
||||
return w.writer.Write(b)
|
||||
@ -32,5 +28,4 @@ func (w *Writer) Close() {
|
||||
_ = wc.Close()
|
||||
}
|
||||
|
||||
w.wg.Wait()
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user