-
-
Notifications
You must be signed in to change notification settings - Fork 398
/
Copy pathinterceptors.go
113 lines (101 loc) · 2.91 KB
/
interceptors.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// This file is part of arduino-cli.
//
// Copyright 2020 ARDUINO SA (http://www.arduino.cc/)
//
// This software is released under the GNU General Public License version 3,
// which covers the main part of arduino-cli.
// The terms of this license can be found at:
// https://www.gnu.org/licenses/gpl-3.0.en.html
//
// You can be released from the requirements of the above licenses by purchasing
// a commercial license. Buying such a license is mandatory if you want to
// modify or otherwise use the software for commercial activities involving the
// Arduino software without disclosing the source code of your own applications.
// To purchase a commercial license, send an email to license@arduino.cc.
package daemon
import (
"context"
"encoding/json"
"fmt"
"io"
"strings"
"sync/atomic"
"google.golang.org/grpc"
)
var debugStdOut io.Writer
var debugSeq uint32
var debugFilters []string
func log(isRequest bool, seq uint32, msg interface{}) {
prefix := fmt.Sprint(seq, " | ")
j, _ := json.MarshalIndent(msg, prefix, " ")
inOut := "RESP: "
if isRequest {
inOut = "REQ: "
}
fmt.Fprintln(debugStdOut, prefix+inOut+string(j))
}
func logError(seq uint32, err error) {
if err != nil {
fmt.Fprintln(debugStdOut, seq, "| ERROR: ", err)
}
}
func logSelector(method string) bool {
if len(debugFilters) == 0 {
return true
}
for _, filter := range debugFilters {
if strings.Contains(method, filter) {
return true
}
}
return false
}
func unaryLoggerInterceptor(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
if !logSelector(info.FullMethod) {
return handler(ctx, req)
}
seq := atomic.AddUint32(&debugSeq, 1)
fmt.Fprintln(debugStdOut, seq, "CALLED:", info.FullMethod)
log(true, seq, req)
resp, err := handler(ctx, req)
logError(seq, err)
log(false, seq, resp)
fmt.Fprintln(debugStdOut, seq, "CALL END")
fmt.Fprintln(debugStdOut)
return resp, err
}
func streamLoggerInterceptor(srv interface{}, stream grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
if !logSelector(info.FullMethod) {
return handler(srv, stream)
}
seq := atomic.AddUint32(&debugSeq, 1)
streamReq := ""
if info.IsClientStream {
streamReq = "STREAM_REQ "
}
if info.IsServerStream {
streamReq += "STREAM_RESP"
}
fmt.Fprintln(debugStdOut, seq, "CALLED:", info.FullMethod, streamReq)
err := handler(srv, &loggingServerStream{ServerStream: stream, seq: seq})
logError(seq, err)
fmt.Fprintln(debugStdOut, seq, "STREAM CLOSED")
fmt.Fprintln(debugStdOut)
return err
}
type loggingServerStream struct {
grpc.ServerStream
seq uint32
}
func (l *loggingServerStream) RecvMsg(m interface{}) error {
err := l.ServerStream.RecvMsg(m)
logError(l.seq, err)
log(true, l.seq, m)
return err
}
func (l *loggingServerStream) SendMsg(m interface{}) error {
err := l.ServerStream.SendMsg(m)
logError(l.seq, err)
log(false, l.seq, m)
return err
}