-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlesManager.go
129 lines (114 loc) · 2.78 KB
/
handlesManager.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package storageAdapterFs
import (
"io/ioutil"
"log"
"os"
"path/filepath"
"sync"
"time"
)
type openFileHandle struct {
handle *fileHandle
timeout *time.Timer
}
type handlesManager struct {
config Config
files map[string]*openFileHandle
mutex *sync.RWMutex
}
func newHandlesManager(config Config) handlesManager {
return handlesManager{
config: config,
files: map[string]*openFileHandle{},
mutex: &sync.RWMutex{},
}
}
func (m *handlesManager) Write(stream string, content string) {
m.mutex.RLock()
handle, ok := m.files[stream]
m.mutex.RUnlock()
if ok {
handle.handle.Append([]byte(content))
handle.timeout.Reset(m.config.OpenHandleTimeout)
} else {
handle, err := m.CreateOutput(stream)
if err != nil {
log.Printf("cant create log file handle: %s", err)
} else {
handle.handle.Append([]byte(content))
}
}
}
func (m *handlesManager) CreateOutput(stream string) (openFileHandle, error) {
path := constructLogFilePath(stream, m.config)
handle, err := newFileHandle(path, m.config.FilePermissions)
if err != nil {
return openFileHandle{}, err
}
timer := time.NewTimer(m.config.OpenHandleTimeout)
fileHandle := openFileHandle{
handle: handle,
timeout: timer,
}
m.mutex.Lock()
m.files[stream] = &fileHandle
m.mutex.Unlock()
go func(stream string) {
<-timer.C
err := handle.Close()
if err != nil {
log.Printf("error while closing log file: %s", err)
}
m.mutex.Lock()
delete(m.files, stream)
m.mutex.Unlock()
}(stream)
return fileHandle, err
}
func (m *handlesManager) CloseAll() {
m.mutex.Lock()
for _, handle := range m.files {
handle.timeout.Stop()
err := handle.handle.Close()
if err != nil {
log.Printf("error while closing log file: %s", err)
}
}
m.files = map[string]*openFileHandle{}
m.mutex.Unlock()
}
func (m *handlesManager) ListLogFiles(stream string) []string {
path := constructLogFilePath(stream, m.config)
dir := filepath.Dir(path)
// ensure stream directory
if _, err := os.Stat(dir); os.IsNotExist(err) {
return []string{}
}
files, err := ioutil.ReadDir(dir)
if err != nil {
return []string{}
}
fileNames := []string{}
for _, file := range files {
if !file.IsDir() {
fileNames = append(fileNames, file.Name())
}
}
return fileNames
}
func (m *handlesManager) GetLogs(stream string, fileName string) ([]byte, error) {
folder := m.config.BasePath
if m.config.GroupStreamsIntoFolders {
folder = filepath.Join(m.config.BasePath, stream)
}
path := filepath.Join(folder, fileName)
return ioutil.ReadFile(path)
}
func constructLogFilePath(stream string, config Config) string {
now := time.Now().Format(config.DateFormat)
folder := config.BasePath
if config.GroupStreamsIntoFolders {
folder = filepath.Join(config.BasePath, stream)
}
return filepath.Join(folder, stream+"_"+now+".log")
}