This repository has been archived by the owner on Nov 25, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdebug.go
57 lines (48 loc) · 1.46 KB
/
debug.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
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"log"
"os"
"strconv"
"github.com/mafredri/cdp/rpcc"
)
func newLogCodec(prefix string) rpcc.DialOption {
logger := log.New(os.Stdout, fmt.Sprintf("rpcc(%s) ", prefix), log.LstdFlags)
return rpcc.WithCodec(func(conn io.ReadWriter) rpcc.Codec {
return &rpccLogCodec{conn: conn, log: logger}
})
}
// rpccLogCodec captures the output from writing RPC requests and reading
// responses on the connection. It implements rpcc.Codec via
// WriteRequest and ReadResponse.
type rpccLogCodec struct {
conn io.ReadWriter
log *log.Logger
}
// WriteRequest marshals v into a buffer, writes its contents onto the
// connection and logs it.
func (c *rpccLogCodec) WriteRequest(req *rpcc.Request) error {
var buf bytes.Buffer
if err := json.NewEncoder(&buf).Encode(req); err != nil {
return err
}
c.log.Println("=>", "rpc_id="+strconv.Itoa(int(req.ID)), "rpc_method="+req.Method, "data="+buf.String())
_, err := c.conn.Write(buf.Bytes())
if err != nil {
return err
}
return nil
}
// ReadResponse unmarshals from the connection into v whilst echoing
// what is read into a buffer for logging.
func (c *rpccLogCodec) ReadResponse(resp *rpcc.Response) error {
var buf bytes.Buffer
if err := json.NewDecoder(io.TeeReader(c.conn, &buf)).Decode(resp); err != nil {
return err
}
c.log.Println("<=", "rpc_id="+strconv.Itoa(int(resp.ID)), "rpc_event="+strconv.FormatBool(resp.Method != ""), "data="+buf.String())
return nil
}