-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathoutput.go
More file actions
59 lines (49 loc) · 1 KB
/
Copy pathoutput.go
File metadata and controls
59 lines (49 loc) · 1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package logger
import (
"io"
"os"
"sync"
"github.com/pkg/errors"
)
func newOutput(path string) (io.Writer, error) {
if len(path) > 0 {
if path == "dummy" { // for benchmarking
return &dummyWriter{}, nil
} else {
file, err := os.OpenFile(path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return nil, errors.Wrap(err, "open file")
}
return file, nil
// return &fileWriter{file: file}, nil
}
} else {
return os.Stderr, nil
// return &stdErrWriter{}, nil
}
}
type fileWriter struct {
file *os.File
lock sync.Mutex
}
func (w *fileWriter) Write(b []byte) (int, error) {
w.lock.Lock()
n, err := w.file.Write(b)
// w.file.Sync()
w.lock.Unlock()
return n, err
}
type stdErrWriter struct {
lock sync.Mutex
}
func (w *stdErrWriter) Write(b []byte) (int, error) {
w.lock.Lock()
n, err := os.Stderr.Write(b)
// os.Stderr.Sync()
w.lock.Unlock()
return n, err
}
type dummyWriter struct{}
func (d *dummyWriter) Write(b []byte) (int, error) {
return len(b), nil
}