-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogger.go
More file actions
44 lines (38 loc) · 818 Bytes
/
logger.go
File metadata and controls
44 lines (38 loc) · 818 Bytes
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
package l2
import (
"encoding/hex"
"fmt"
"log"
)
// Construct a logger which logs all frames.
func NewLogger(d FrameReader) FrameReader {
return logger{d}
}
// Logs all frames which transit it.
type logger struct {
D FrameReader
}
func (l logger) ReadFrame() (EthFrame, error) {
for {
p, err := l.D.ReadFrame()
if err == nil {
PrintFrame(fmt.Sprint(l.D), p)
} else {
log.Print("Err reading frame:", err)
}
return p, err
}
}
func (l logger) String() string {
return "Logger{" + fmt.Sprint(l.D) + "}"
}
// Utility function to pretty print a ethernet frame plus a header
func PrintFrame(name string, data []byte) {
E := EthFrame(data)
log.Printf("%s: %s->%s %d bytes protocol %d",
name,
hex.EncodeToString(E.Source()),
hex.EncodeToString(E.Destination()),
len(data),
E.Type())
}