-
Notifications
You must be signed in to change notification settings - Fork 0
/
debug.go
55 lines (47 loc) · 1.21 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
package sense
import (
"fmt"
"log"
"net/http"
"strings"
"github.com/dnesting/sense/internal/senseutil"
"github.com/dnesting/sense/realtime"
"github.com/dnesting/sense/senseauth"
)
var debugLogger *log.Logger
type loggingTransport struct {
transport http.RoundTripper
}
func debug(args ...interface{}) {
if debugLogger != nil {
debugLogger.Output(2, strings.TrimRight(fmt.Sprint(args...), "\n"))
}
}
func (s *loggingTransport) RoundTrip(r *http.Request) (*http.Response, error) {
senseutil.DumpRequest(debugLogger, r)
tr := s.transport
if tr == nil {
tr = http.DefaultTransport
}
resp, err := tr.RoundTrip(r)
if err != nil {
debug("error:", err)
return nil, err
}
senseutil.DumpResponse(debugLogger, resp)
return resp, err
}
// SetDebug enables debug logging using the given logger and returns an
// [*http.Client] that wraps baseClient, logging requests and responses
// to the same logger. Passing nil will disable debug logging.
func SetDebug(l *log.Logger, baseClient *http.Client) *http.Client {
if baseClient == nil {
baseClient = http.DefaultClient
}
debugLogger = l
senseauth.SetDebug(l)
realtime.SetDebug(l)
return &http.Client{
Transport: &loggingTransport{baseClient.Transport},
}
}