forked from airnandez/cluefs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtracer.go
109 lines (98 loc) · 2.19 KB
/
tracer.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
package main
import (
"encoding/csv"
"encoding/json"
"fmt"
"os"
"path/filepath"
)
type FsOperTracer interface {
String() string
SetTimeEnd()
MarshalCSV() []string
}
type Tracer interface {
Trace(op FsOperTracer)
}
type CSVTracer struct {
writer *csv.Writer
receptionChan chan FsOperTracer
}
func NewCSVTracer(filePath string) (*CSVTracer, error) {
destFile, err := openTraceDestination(filePath)
if err != nil {
return nil, err
}
tracer := CSVTracer{
writer: csv.NewWriter(destFile),
receptionChan: make(chan FsOperTracer, 1024),
}
// Start the event collector
go func() {
for op := range tracer.receptionChan {
tracer.writer.Write(op.MarshalCSV())
tracer.writer.Flush()
}
}()
return &tracer, nil
}
func (t *CSVTracer) Trace(op FsOperTracer) {
t.receptionChan <- op
}
type JSONTracer struct {
file *os.File
receptionChan chan FsOperTracer
}
func NewJSONTracer(filePath string) (*JSONTracer, error) {
destFile, err := openTraceDestination(filePath)
if err != nil {
return nil, err
}
tracer := JSONTracer{
file: destFile,
receptionChan: make(chan FsOperTracer, 1024),
}
// Start the event collector
go func() {
crlf := []byte{'\n'}
for op := range tracer.receptionChan {
if m, err := json.Marshal(op); err == nil {
tracer.file.Write(m)
tracer.file.Write(crlf)
}
}
}()
return &tracer, nil
}
func (t *JSONTracer) Trace(op FsOperTracer) {
t.receptionChan <- op
}
func NewTracer(kind, fileName string) (Tracer, error) {
var (
tracer Tracer
err error
)
switch kind {
default:
fallthrough
case "csv":
tracer, err = NewCSVTracer(fileName)
case "json":
tracer, err = NewJSONTracer(fileName)
}
return tracer, err
}
func openTraceDestination(filePath string) (*os.File, error) {
destFile := os.Stdout
if len(filePath) > 0 && filePath != "-" {
abspath, err := filepath.Abs(filePath)
if err != nil {
return nil, fmt.Errorf("could not retrieve absolute path for '%s' [%s]", filePath, err)
}
destFile, err = os.OpenFile(abspath, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0640)
if err != nil {
return nil, fmt.Errorf("could not open file '%s' for writing [%s]", filePath, err)
}
}
return destFile, nil
}