-
Notifications
You must be signed in to change notification settings - Fork 20
/
output.go
64 lines (53 loc) · 1.77 KB
/
output.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
package funnel
import (
"bufio"
"io"
"log/syslog"
"github.com/spf13/viper"
)
// OutputWriter interface is to be implemented by all remote target writers
// It embeds the io.Writer and has basic function calls which should be needed by all writers
// Can be modified later if needed
type OutputWriter interface {
io.Writer
Flush() error
Close() error
}
// UnregisteredOutputError holds the error if some target was passed from the config
// which was not registered
type UnregisteredOutputError struct {
target string
}
func (e *UnregisteredOutputError) Error() string {
return "Output " + e.target + " was not registered from any module"
}
// OutputFactory is a function type which holds the output registry
type OutputFactory func(v *viper.Viper, logger *syslog.Writer) (OutputWriter, error)
var registeredOutputs = make(map[string]OutputFactory)
// RegisterNewWriter is called by the init function from every output
// Adds the constructor to the registry
func RegisterNewWriter(name string, factory OutputFactory) {
registeredOutputs[name] = factory
}
// GetOutputWriter gets the constructor by extracting the target.
// Then returns the corresponding output writer by calling the constructor
func GetOutputWriter(v *viper.Viper, logger *syslog.Writer) (OutputWriter, error) {
target := v.GetString(Target)
if target == "file" {
return nil, nil
}
w, ok := registeredOutputs[target]
if !ok {
return nil, &UnregisteredOutputError{target}
}
return w(v, logger)
}
// FileOutput is just an embed type which adds the Close method to buffered writer to satisfy the OutputWriter interface
// XXX: Might need to implement this in a better way
type FileOutput struct {
*bufio.Writer
}
// Close function is just a no-op. Its never called.
func (f *FileOutput) Close() error {
return nil
}