-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
inspector.go
116 lines (96 loc) · 3.12 KB
/
inspector.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
114
115
116
// Copyright 2020 The go-language-server Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package inspector
import (
"strconv"
"time"
"go.uber.org/zap/zapcore"
)
// Logger represents a Language Server Protocol Inspector specification logger.
type Logger interface {
TraceRequest(serverID string, req *Payload, expected bool, queueLength int) error
TraceResponse(serverID string, req *Payload, meta Metadata) error
TraceRequestCompleted(serverID string, command string, reqSequence int, meta Metadata) error
TraceEvent(serverID string, event *Payload) error
}
// Metadata represents a request metadata.
type Metadata struct {
QueuingStartTime time.Time
}
// Payload represents a Language Server Protocol Inspector specification payload.
type Payload struct {
Time time.Time `json:"time"`
Msg string `json:"msg"`
MsgKind MessageKind `json:"msgKind"`
MsgType string `json:"msgType"`
MsgID string `json:"msgId,omitempty"`
MsgLatency time.Duration `json:"msgLatency,omitempty"`
Arg []interface{} `json:"arg"`
}
// compile time check whether the Payload implements zapcore.ObjectMarshaler interface.
var _ zapcore.ObjectMarshaler = (*Payload)(nil)
// MarshalLogObject implements zapcore.ObjectMarshaler.
func (p *Payload) MarshalLogObject(enc zapcore.ObjectEncoder) error {
enc.AddTime("time", p.Time)
enc.AddString("msg", p.Msg)
enc.AddString("msgKind", p.MsgKind.String())
enc.AddString("msgType", p.MsgType)
enc.AddString("msgId", p.MsgID)
enc.AddDuration("msgLatency", p.MsgLatency)
return enc.AddReflected("arg", p.Arg)
}
// LogFormat represents a Language Server Protocol Inspector log format.
type LogFormat uint8
const (
// TextFormat represents a text format of inspector log.
TextFormat LogFormat = 1 + iota
// JSONFormat represents a JSON format of inspector log.
JSONFormat
)
// String implements fmt.Stringer.
func (lf LogFormat) String() string {
switch lf {
case TextFormat:
return "text"
case JSONFormat:
return "json"
default:
return strconv.FormatUint(uint64(lf), 10)
}
}
// MessageKind represents a message kind.
type MessageKind uint8
const (
// SendNotification represents a send notification message kind.
SendNotification MessageKind = 1 + iota
// ReceiveNotification represents a receive notification message kind.
ReceiveNotification
// SendRequest represents a send request message kind.
SendRequest
// ReceiveRequest represents a receive request message kind.
ReceiveRequest
// SendResponse represents a send response message kind.
SendResponse
// ReceiveResponse represents a receive response message kind.
ReceiveResponse
)
// String implements fmt.Stringer.
func (ms MessageKind) String() string {
switch ms {
case SendNotification:
return "send-notification"
case ReceiveNotification:
return "recv-notification"
case SendRequest:
return "send-request"
case ReceiveRequest:
return "recv-request"
case SendResponse:
return "send-response"
case ReceiveResponse:
return "recv-response"
default:
return strconv.FormatUint(uint64(ms), 10)
}
}