forked from juneym/gor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
output_file.go
51 lines (38 loc) · 809 Bytes
/
output_file.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
package main
import (
"encoding/gob"
"log"
"os"
"time"
)
type RawRequest struct {
Timestamp int64
Request []byte
}
type FileOutput struct {
path string
encoder *gob.Encoder
file *os.File
}
func NewFileOutput(path string) (o *FileOutput) {
o = new(FileOutput)
o.path = path
o.Init(path)
return
}
func (o *FileOutput) Init(path string) {
var err error
o.file, err = os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0660)
if err != nil {
log.Fatal(o, "Cannot open file %q. Error: %s", path, err)
}
o.encoder = gob.NewEncoder(o.file)
}
func (o *FileOutput) Write(data []byte) (n int, err error) {
raw := RawRequest{time.Now().UnixNano(), data}
o.encoder.Encode(raw)
return len(data), nil
}
func (o *FileOutput) String() string {
return "File output: " + o.path
}