Skip to content

Commit 08c8adf

Browse files
author
Mateusz Serafin Gajewski
committedApr 27, 2017
Add -i = include request frames as they are sent
1 parent 0f201a1 commit 08c8adf

File tree

1 file changed

+37
-23
lines changed

1 file changed

+37
-23
lines changed
 

‎main.go

+37-23
Original file line numberDiff line numberDiff line change
@@ -15,36 +15,40 @@ import (
1515
"os"
1616
"path"
1717

18-
"github.com/gorilla/websocket"
19-
"github.com/fatih/color"
2018
"errors"
19+
20+
"github.com/fatih/color"
21+
"github.com/gorilla/websocket"
2122
)
2223

2324
var (
24-
flagListen = flag.String("l", "localhost:9223", "listen address")
25-
flagRemote = flag.String("r", "localhost:9222", "remote address")
26-
flagNoLog = flag.Bool("n", false, "disable logging to file")
27-
flagLogMask = flag.String("log", "logs/cdp-%s.log", "log file mask")
28-
flagEllipsis = flag.Bool("s", false, "shorten requests and responses")
29-
flagOnce = flag.Bool("once", false, "debug single session")
25+
flagListen = flag.String("l", "localhost:9223", "listen address")
26+
flagRemote = flag.String("r", "localhost:9222", "remote address")
27+
flagNoLog = flag.Bool("n", false, "disable logging to file")
28+
flagLogMask = flag.String("log", "logs/cdp-%s.log", "log file mask")
29+
flagEllipsis = flag.Bool("s", false, "shorten requests and responses")
30+
flagOnce = flag.Bool("once", false, "debug single session")
31+
flagShowRequests = flag.Bool("i", false, "include request frames as they are sent")
3032
)
3133

3234
var (
33-
responseColor = color.New(color.FgHiGreen).SprintfFunc()
34-
requestColor = color.New(color.FgGreen).SprintfFunc()
35-
eventsColor = color.New(color.FgHiRed).SprintfFunc()
36-
protocolColor = color.New(color.FgYellow).SprintfFunc()
37-
protocolError = color.New(color.FgHiYellow, color.BgRed).SprintfFunc()
38-
targetColor = color.New(color.FgHiWhite).SprintfFunc()
39-
methodColor = color.New(color.FgHiYellow).SprintfFunc()
40-
errorColor = color.New(color.BgRed, color.FgWhite).SprintfFunc()
35+
responseColor = color.New(color.FgHiGreen).SprintfFunc()
36+
requestColor = color.New(color.FgHiBlue).SprintFunc()
37+
requestReplyColor = color.New(color.FgGreen).SprintfFunc()
38+
eventsColor = color.New(color.FgHiRed).SprintfFunc()
39+
protocolColor = color.New(color.FgYellow).SprintfFunc()
40+
protocolError = color.New(color.FgHiYellow, color.BgRed).SprintfFunc()
41+
targetColor = color.New(color.FgHiWhite).SprintfFunc()
42+
methodColor = color.New(color.FgHiYellow).SprintfFunc()
43+
errorColor = color.New(color.BgRed, color.FgWhite).SprintfFunc()
4144
)
4245

4346
const (
4447
incomingBufferSize = 10 * 1024 * 1024
4548
outgoingBufferSize = 25 * 1024 * 1024
4649
ellipsisLength = 80
4750
requestReplyFormat = "%s % 48s(%s) = %s"
51+
requestFormat = "%s % 48s(%s)"
4852
eventFormat = "%s % 48s(%s)"
4953
)
5054

@@ -140,10 +144,11 @@ func main() {
140144
}
141145

142146
func dumpStream(logger *log.Logger, stream chan *protocolMessage) {
143-
logger.Printf("Legend: %s, %s, %s, %s",
147+
logger.Printf("Legend: %s, %s, %s, %s, %s",
144148
protocolColor("protocol informations"),
145149
eventsColor("received events"),
146-
requestColor("sent requests"),
150+
requestColor("sent request frames"),
151+
requestReplyColor("requests params"),
147152
responseColor("received responses."),
148153
)
149154

@@ -159,6 +164,11 @@ func dumpStream(logger *log.Logger, stream chan *protocolMessage) {
159164

160165
if protocolMessage, err := decodeMessage([]byte(asString(msg.Params["message"]))); err == nil {
161166
targetRequests[protocolMessage.Id] = protocolMessage
167+
168+
if *flagShowRequests {
169+
logger.Printf(requestFormat, targetColor("%s", msg.Params["targetId"]), methodColor(protocolMessage.Method), requestColor("%s", serialize(protocolMessage)))
170+
}
171+
162172
} else {
163173
logger.Printf(protocolColor("Could not deserialize message: %+v", err))
164174
}
@@ -175,9 +185,9 @@ func dumpStream(logger *log.Logger, stream chan *protocolMessage) {
175185
delete(targetRequests, protocolMessage.Id)
176186

177187
if protocolMessage.IsError() {
178-
logger.Printf(requestReplyFormat, targetColor("%s", msg.Params["targetId"]), methodColor(request.Method), requestColor(serialize(request.Params)), errorColor(serialize(protocolMessage.Error)))
188+
logger.Printf(requestReplyFormat, targetColor("%s", msg.Params["targetId"]), methodColor(request.Method), requestReplyColor(serialize(request.Params)), errorColor(serialize(protocolMessage.Error)))
179189
} else {
180-
logger.Printf(requestReplyFormat, targetColor("%s", msg.Params["targetId"]), methodColor(request.Method), requestColor(serialize(request.Params)), responseColor(serialize(protocolMessage.Result)))
190+
logger.Printf(requestReplyFormat, targetColor("%s", msg.Params["targetId"]), methodColor(request.Method), requestReplyColor(serialize(request.Params)), responseColor(serialize(protocolMessage.Result)))
181191
}
182192
} else {
183193
logger.Printf(protocolColor("Could not find target request with id: %d", protocolMessage.Id))
@@ -191,6 +201,10 @@ func dumpStream(logger *log.Logger, stream chan *protocolMessage) {
191201
} else {
192202
if msg.IsRequest() {
193203
requests[msg.Id] = msg
204+
205+
if *flagShowRequests {
206+
logger.Printf(requestFormat, targetColor(protocolTargetId), methodColor(msg.Method), requestColor(serialize(msg.Params)))
207+
}
194208
}
195209

196210
if msg.IsResponse() {
@@ -199,9 +213,9 @@ func dumpStream(logger *log.Logger, stream chan *protocolMessage) {
199213

200214
if request != nil {
201215
if msg.IsError() {
202-
logger.Printf(requestReplyFormat, targetColor(protocolTargetId), methodColor(request.Method), requestColor(serialize(request.Params)), errorColor(serialize(msg.Error)))
216+
logger.Printf(requestReplyFormat, targetColor(protocolTargetId), methodColor(request.Method), requestReplyColor(serialize(request.Params)), errorColor(serialize(msg.Error)))
203217
} else {
204-
logger.Printf(requestReplyFormat, targetColor(protocolTargetId), methodColor(request.Method), requestColor(serialize(request.Params)), responseColor(serialize(msg.Result)))
218+
logger.Printf(requestReplyFormat, targetColor(protocolTargetId), methodColor(request.Method), requestReplyColor(serialize(request.Params)), responseColor(serialize(msg.Result)))
205219
}
206220
}
207221
} else {
@@ -250,7 +264,7 @@ func proxyWS(ctxt context.Context, stream chan *protocolMessage, in, out *websoc
250264

251265
func checkVersion() (map[string]string, error) {
252266
cl := &http.Client{}
253-
req, err := http.NewRequest("GET", "http://" + *flagRemote+"/json/version", nil)
267+
req, err := http.NewRequest("GET", "http://"+*flagRemote+"/json/version", nil)
254268
if err != nil {
255269
return nil, err
256270
}

0 commit comments

Comments
 (0)
Please sign in to comment.