-
-
Notifications
You must be signed in to change notification settings - Fork 369
Expand file tree
/
Copy pathdebug.go
More file actions
78 lines (64 loc) · 1.64 KB
/
debug.go
File metadata and controls
78 lines (64 loc) · 1.64 KB
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
package debug
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"net/http/httputil"
"os"
)
var (
// Verbose determines if debugging output is displayed to the user
Verbose bool
output io.Writer = os.Stderr
)
// Println conditionally outputs a message to Stderr
func Println(args ...interface{}) {
if Verbose {
fmt.Fprintln(output, args...)
}
}
// Printf conditionally outputs a formatted message to Stderr
func Printf(format string, args ...interface{}) {
if Verbose {
fmt.Fprintf(output, format, args...)
}
}
// DumpRequest dumps out the provided http.Request
func DumpRequest(req *http.Request) {
if !Verbose {
return
}
var bodyCopy bytes.Buffer
body := io.TeeReader(req.Body, &bodyCopy)
req.Body = ioutil.NopCloser(body)
dump, err := httputil.DumpRequest(req, req.ContentLength > 0)
if err != nil {
log.Fatal(err)
}
Println("\n========================= BEGIN DumpRequest =========================")
Println(string(dump))
Println("========================= END DumpRequest =========================")
Println("")
req.Body = ioutil.NopCloser(&bodyCopy)
}
// DumpResponse dumps out the provided http.Response
func DumpResponse(res *http.Response) {
if !Verbose {
return
}
var bodyCopy bytes.Buffer
body := io.TeeReader(res.Body, &bodyCopy)
res.Body = ioutil.NopCloser(body)
dump, err := httputil.DumpResponse(res, res.ContentLength > 0)
if err != nil {
log.Fatal(err)
}
Println("\n========================= BEGIN DumpResponse =========================")
Println(string(dump))
Println("========================= END DumpResponse =========================")
Println("")
res.Body = ioutil.NopCloser(body)
}