|
| 1 | +// This file is part of arduino-cli. |
| 2 | +// |
| 3 | +// Copyright 2020 ARDUINO SA (http://www.arduino.cc/) |
| 4 | +// |
| 5 | +// This software is released under the GNU General Public License version 3, |
| 6 | +// which covers the main part of arduino-cli. |
| 7 | +// The terms of this license can be found at: |
| 8 | +// https://www.gnu.org/licenses/gpl-3.0.en.html |
| 9 | +// |
| 10 | +// You can be released from the requirements of the above licenses by purchasing |
| 11 | +// a commercial license. Buying such a license is mandatory if you want to |
| 12 | +// modify or otherwise use the software for commercial activities involving the |
| 13 | +// Arduino software without disclosing the source code of your own applications. |
| 14 | +// To purchase a commercial license, send an email to license@arduino.cc. |
| 15 | + |
| 16 | +package daemon |
| 17 | + |
| 18 | +import ( |
| 19 | + "context" |
| 20 | + "encoding/json" |
| 21 | + "fmt" |
| 22 | + |
| 23 | + "google.golang.org/grpc" |
| 24 | +) |
| 25 | + |
| 26 | +func log(isRequest bool, msg interface{}) { |
| 27 | + j, _ := json.MarshalIndent(msg, "| ", " ") |
| 28 | + inOut := map[bool]string{true: "REQ: ", false: "RESP: "} |
| 29 | + fmt.Println("| " + inOut[isRequest] + string(j)) |
| 30 | +} |
| 31 | + |
| 32 | +func logError(err error) { |
| 33 | + if err != nil { |
| 34 | + fmt.Println("| ERROR: ", err) |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +func unaryLoggerInterceptor(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) { |
| 39 | + fmt.Println("CALLED:", info.FullMethod) |
| 40 | + log(true, req) |
| 41 | + resp, err := handler(ctx, req) |
| 42 | + logError(err) |
| 43 | + log(false, resp) |
| 44 | + fmt.Println() |
| 45 | + return resp, err |
| 46 | +} |
| 47 | + |
| 48 | +func streamLoggerInterceptor(srv interface{}, stream grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error { |
| 49 | + streamReq := "" |
| 50 | + if info.IsClientStream { |
| 51 | + streamReq = "STREAM_REQ " |
| 52 | + } |
| 53 | + if info.IsServerStream { |
| 54 | + streamReq += "STREAM_RESP" |
| 55 | + } |
| 56 | + fmt.Println("CALLED:", info.FullMethod, streamReq) |
| 57 | + err := handler(srv, &loggingServerStream{ServerStream: stream}) |
| 58 | + logError(err) |
| 59 | + fmt.Println() |
| 60 | + return err |
| 61 | +} |
| 62 | + |
| 63 | +type loggingServerStream struct { |
| 64 | + grpc.ServerStream |
| 65 | +} |
| 66 | + |
| 67 | +func (l *loggingServerStream) RecvMsg(m interface{}) error { |
| 68 | + err := l.ServerStream.RecvMsg(m) |
| 69 | + logError(err) |
| 70 | + log(true, m) |
| 71 | + return err |
| 72 | +} |
| 73 | + |
| 74 | +func (l *loggingServerStream) SendMsg(m interface{}) error { |
| 75 | + err := l.ServerStream.SendMsg(m) |
| 76 | + logError(err) |
| 77 | + log(false, m) |
| 78 | + return err |
| 79 | +} |
0 commit comments