-
Notifications
You must be signed in to change notification settings - Fork 0
/
files.go
184 lines (173 loc) · 4.79 KB
/
files.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package files
import (
"errors"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
"sync"
"time"
fsnotify "github.com/fsnotify/fsnotify"
)
// A LogMessage is created when something is logged.
type LogMessage struct {
FilePath string // The file in which the message was logged.
Timestamp time.Time // The time when it was logged.
Text string // The text that was logged.
}
// The FolderLoader can be used to keep watching for changes in a folder.
// It allows reading logs as they come in, by line or just the entire files.
type FolderLoader struct {
LogFolders []string
fileBytesRead map[string]int64 // Map of bytes read for a file
close chan chan<- error // Channel that will receive close call
outputChannel chan LogMessage // Logs are written to this channel
isClosed bool // Set to true when Close() is first called
mu sync.Mutex // Map access
}
// FileError is an error related to a file, it will be appended to the message.
type FileError struct {
error
filePath string
}
// Error returns the error that occured starting with the file that failed.
func (err FileError) Error() string {
return "Path: " + err.filePath + "\n" + err.Error()
}
// NewFolderLoader creates a new FolderLoader.
func NewFolderLoader(logLocations []string) *FolderLoader {
// Create object, append "/" or "\" to log location if needed.
loader := &FolderLoader{
LogFolders: logLocations,
close: make(chan chan<- error, 1),
fileBytesRead: make(map[string]int64),
}
separator := string(os.PathSeparator)
for index, location := range loader.LogFolders {
path, err := filepath.Abs(location)
if err != nil {
log.Printf("Could not parse path '%s': %s", location, err)
}
if !strings.HasSuffix(location, separator) {
loader.LogFolders[index] = path + separator
} else {
loader.LogFolders[index] = path
}
}
return loader
}
// ReadFile reads the given log file if it exists and is in the scope of this reader.
func (loader *FolderLoader) ReadFile(filePath string) (string, error) {
path, err := filepath.Abs(filePath)
if err != nil {
return "", FileError{err, filePath}
}
contained := false
for _, location := range loader.LogFolders {
if strings.HasPrefix(path, location) {
contained = true
}
}
if !contained {
return "", FileError{errors.New("File is not within scope of current folders"), filePath}
}
content, err := ioutil.ReadFile(path)
if err != nil {
return "", FileError{err, filePath}
}
return string(content), nil
}
// Close stops and closes this loader. It also notifies all channels to be closed.
func (loader *FolderLoader) Close() error {
if loader.isClosed {
return nil
}
loader.isClosed = true
ch := make(chan error)
loader.close <- ch
return <-ch
}
// readLastText writes the last read text for a file to the output channel.
func (loader *FolderLoader) readLastText(filePath string) {
file, err := os.Open(filePath)
defer file.Close()
if err != nil {
log.Fatal(FileError{err, filePath})
}
info, err := file.Stat()
if err != nil {
log.Fatal(FileError{err, filePath})
}
// Check what we need to read from the file
loader.mu.Lock()
readBytes := loader.fileBytesRead[filePath]
bytesToRead := info.Size() - readBytes
// If the file became smaller it probably was deleted before
if bytesToRead < 0 {
loader.fileBytesRead[filePath] = info.Size()
readBytes = 0
bytesToRead = info.Size()
} else {
loader.fileBytesRead[filePath] = bytesToRead + readBytes
}
loader.mu.Unlock()
// Read from last location
_, err = file.Seek(readBytes, 0)
if err != nil {
log.Fatal(FileError{err, filePath})
}
byteContent := make([]byte, bytesToRead)
_, err = io.ReadAtLeast(file, byteContent, int(bytesToRead))
if err != nil {
log.Fatal(FileError{err, filePath})
}
text := string(byteContent)
if len(text) > 0 {
loader.outputChannel <- LogMessage{
FilePath: filePath,
Timestamp: time.Now(),
Text: text,
}
}
}
// StartWatching for logs and return a channel with the log output.
func (loader *FolderLoader) StartWatching() (chan LogMessage, error) {
watcher, err := fsnotify.NewWatcher()
if err != nil {
watcher.Close()
return nil, err
}
outputChannel := make(chan LogMessage, 50)
loader.outputChannel = outputChannel
go func() {
for {
select {
case event, ok := <-watcher.Events:
if !ok {
return
}
if event.Op&fsnotify.Write == fsnotify.Write {
filePath := event.Name
loader.readLastText(filePath)
}
case err, ok := <-watcher.Errors:
if !ok {
return
}
log.Println("error:", err)
case ch := <-loader.close:
close(outputChannel)
ch <- watcher.Close()
}
}
}()
for _, folder := range loader.LogFolders {
err := watcher.Add(folder)
if err != nil {
return nil, FileError{err, folder}
}
}
return outputChannel, nil
}