-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathdumper.go
90 lines (80 loc) · 2.17 KB
/
dumper.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
package streams
import (
"fmt"
"io"
"log"
"os"
"github.com/arduino/go-paths-helper"
)
// GlobalLogDirectory is the directory where logs are created
var GlobalLogDirectory *paths.Path
// LogReadWriteCloserAs return a proxy for the given upstream io.ReadWriteCloser
// that forward and logs all read/write/close operations on the given filename
// that is created in the GlobalLogDirectory.
func LogReadWriteCloserAs(upstream io.ReadWriteCloser, filename string) io.ReadWriteCloser {
return &dumper{
upstream: upstream,
logfile: OpenLogFileAs(filename),
}
}
// LogReadWriteCloserToFile return a proxy for the given upstream io.ReadWriteCloser
// that forward and logs all read/write/close operations on the given file.
func LogReadWriteCloserToFile(upstream io.ReadWriteCloser, file *os.File) io.ReadWriteCloser {
return &dumper{
upstream: upstream,
logfile: file,
}
}
// OpenLogFileAs creates a log file in GlobalLogDirectory.
func OpenLogFileAs(filename string) *os.File {
path := GlobalLogDirectory.Join(filename)
res, err := path.Append()
if err != nil {
log.Fatalf("Error opening log file: %s", err)
} else {
abs, _ := path.Abs()
log.Printf("logging to %s", abs)
}
res.WriteString("\n\n\n\n\n\n\nStarted logging.\n")
return res
}
type dumper struct {
upstream io.ReadWriteCloser
logfile *os.File
reading bool
writing bool
}
func (d *dumper) Read(buff []byte) (int, error) {
n, err := d.upstream.Read(buff)
if err != nil {
d.logfile.Write([]byte(fmt.Sprintf("<<< Read Error: %s\n", err)))
} else {
if !d.reading {
d.reading = true
d.writing = false
d.logfile.Write([]byte("\n<<<\n"))
}
d.logfile.Write(buff[:n])
}
return n, err
}
func (d *dumper) Write(buff []byte) (int, error) {
n, err := d.upstream.Write(buff)
if err != nil {
_, _ = d.logfile.Write([]byte(fmt.Sprintf(">>> Write Error: %s\n", err)))
} else {
if !d.writing {
d.writing = true
d.reading = false
d.logfile.Write([]byte("\n>>>\n"))
}
_, _ = d.logfile.Write(buff[:n])
}
return n, err
}
func (d *dumper) Close() error {
err := d.upstream.Close()
_, _ = d.logfile.Write([]byte(fmt.Sprintf("--- Stream closed, err=%s\n", err)))
_ = d.logfile.Close()
return err
}