-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_writer_manager.go
73 lines (63 loc) · 1.24 KB
/
file_writer_manager.go
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package loggo
import (
"path/filepath"
"sync"
"fmt"
)
var (
manager *FileWriterManager
)
type FileWriterManager struct {
m *sync.Map
}
func GetFileWriter(filename string) BufferWriter {
path, err := filepath.Abs(filename)
if err != nil {
panic(err)
}
return manager.getFileWriter(path)
}
func (manager *FileWriterManager) getFileWriter(path string) *FileWriter {
value, ok := manager.m.Load(path)
var fw *FileWriter
if !ok {
fw = newFileWriter(path, buffer_pool)
manager.m.Store(path, fw)
} else {
fw = value.(*FileWriter)
}
return fw
}
func Reopen() error {
return manager.reopen()
}
func (manager *FileWriterManager) reopen() error {
var reterr error
manager.m.Range(func(key, value interface{}) bool {
fw := value.(*FileWriter)
err := fw.Reopen()
if err != nil {
reterr = fmt.Errorf("%s reopen err: %v. \n", key.(string), err)
}
return true
})
return reterr
}
func Close() {
manager.Close()
}
func (manager *FileWriterManager) Close() {
manager.m.Range(func(key, value interface{}) bool {
fw := value.(*FileWriter)
fw.Close()
//if err != nil {
// reterr = fmt.Errorf("%s reopen err: %v. \n", key.(string), err)
//}
return true
})
}
func init() {
manager = &FileWriterManager{
m: &sync.Map{},
}
}